Skip to content

Commit

Permalink
Update ManagedTransport to handle deregister better.
Browse files Browse the repository at this point in the history
  • Loading branch information
志宇 committed Jan 29, 2020
1 parent ccb2ff4 commit f02c2db
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions pkg/transport/managed_transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,9 @@ func (mt *ManagedTransport) dial(ctx context.Context) error {
// redial only actually dials if transport is still registered in transport discovery.
// The 'retry' output specifies whether we can retry dial on failure.
func (mt *ManagedTransport) redial(ctx context.Context) (retry bool, err error) {
if !mt.isServing() {
return false, ErrNotServing
}

if _, err = mt.dc.GetTransportByID(ctx, mt.Entry.ID); err != nil {

Expand All @@ -273,6 +276,10 @@ func (mt *ManagedTransport) redial(ctx context.Context) (retry bool, err error)
}

func (mt *ManagedTransport) getConn() *snet.Conn {
if !mt.isServing() {
return nil
}

mt.connMx.Lock()
conn := mt.conn
mt.connMx.Unlock()
Expand Down Expand Up @@ -308,6 +315,10 @@ func (mt *ManagedTransport) setIfConnNil(ctx context.Context, conn *snet.Conn) e
}

func (mt *ManagedTransport) clearConn(ctx context.Context) {
if !mt.isServing() {
return
}

if mt.conn != nil {
if err := mt.conn.Close(); err != nil {
log.WithError(err).Warn("Failed to close connection")
Expand All @@ -316,6 +327,7 @@ func (mt *ManagedTransport) clearConn(ctx context.Context) {
}
if _, err := mt.dc.UpdateStatuses(ctx, &Status{ID: mt.Entry.ID, IsUp: false}); err != nil {
mt.log.Warnf("Failed to update transport status: %s", err)
return
}
mt.log.Infoln("Status updated: DOWN")
}
Expand All @@ -325,10 +337,6 @@ func (mt *ManagedTransport) WritePacket(ctx context.Context, packet routing.Pack
mt.connMx.Lock()
defer mt.connMx.Unlock()

if !mt.isServing() {
return ErrNotServing
}

if mt.conn == nil {
if _, err := mt.redial(ctx); err != nil {
return fmt.Errorf("failed to redial underlying connection: %v", err)
Expand Down

0 comments on commit f02c2db

Please sign in to comment.