From c84dbe7e9210da76659167c2b69a10cddccb80cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BF=97=E5=AE=87?= Date: Thu, 28 May 2020 02:16:08 +1200 Subject: [PATCH] Changes as suggested by @Darkren and @nkryuchkov * Replaced goto statement with for loop in transport.Manager --- pkg/transport/manager.go | 50 +++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/pkg/transport/manager.go b/pkg/transport/manager.go index 42302d088c..0bcf1a67ac 100644 --- a/pkg/transport/manager.go +++ b/pkg/transport/manager.go @@ -216,37 +216,39 @@ func (tm *Manager) SaveTransport(ctx context.Context, remote cipher.PubKey, tpTy return nil, io.ErrClosedPipe } -TrySaveTp: - mTp, err := tm.saveTransport(remote, tpType) - if err != nil { - return nil, err - } + for { + mTp, err := tm.saveTransport(remote, tpType) + if err != nil { + return nil, err + } - if err = mTp.Dial(ctx); err != nil { - // This occurs when an old tp is returned by 'tm.saveTransport', meaning a tp of the same transport ID was just - // deleted (and has not yet fully closed). Hence, we should close and delete the old tp and try again. - if err == ErrNotServing { - if closeErr := mTp.Close(); closeErr != nil { - tm.Logger.WithError(err).Warn("Closing mTp returns non-nil error.") + if err = mTp.Dial(ctx); err != nil { + // This occurs when an old tp is returned by 'tm.saveTransport', meaning a tp of the same transport ID was + // just deleted (and has not yet fully closed). Hence, we should close and delete the old tp and try again. + if err == ErrNotServing { + if closeErr := mTp.Close(); closeErr != nil { + tm.Logger.WithError(err).Warn("Closing mTp returns non-nil error.") + } + delete(tm.tps, mTp.Entry.ID) + continue } - delete(tm.tps, mTp.Entry.ID) - goto TrySaveTp - } - // This occurs when the tp type is STCP and the requested remote PK is not associated with an IP address in the - // STCP table. There is no point in retrying as a connection would be impossible, so we just return an error. - if isSTCPTableError(remote, err) { - if closeErr := mTp.Close(); closeErr != nil { - tm.Logger.WithError(err).Warn("Closing mTp returns non-nil error.") + // This occurs when the tp type is STCP and the requested remote PK is not associated with an IP address in + // the STCP table. There is no point in retrying as a connection would be impossible, so we just return an + // error. + if isSTCPTableError(remote, err) { + if closeErr := mTp.Close(); closeErr != nil { + tm.Logger.WithError(err).Warn("Closing mTp returns non-nil error.") + } + delete(tm.tps, mTp.Entry.ID) + return nil, err } - delete(tm.tps, mTp.Entry.ID) - return nil, err + + tm.Logger.WithError(err).Warn("Underlying transport connection is not established, will retry later.") } - tm.Logger.WithError(err).Warn("Underlying transport connection is not established, will retry later.") + return mTp, nil } - - return mTp, nil } // isSTCPPKError returns true if the error is a STCP table error.