diff --git a/router/connection.go b/router/connection.go index fedd0fe1..3026989f 100644 --- a/router/connection.go +++ b/router/connection.go @@ -2,6 +2,7 @@ package router import ( "encoding/binary" + "errors" "fmt" "net" "strconv" @@ -28,6 +29,8 @@ const ( TieBreakTied ) +var ErrConnectToSelf = errors.New("Cannot connect to ourself") + type RemoteConnection struct { local *Peer remote *Peer @@ -437,7 +440,7 @@ func (conn *LocalConnection) registerRemote(remote *Peer, acceptNewPeer bool) er } if conn.remote == conn.local { - return fmt.Errorf("Cannot connect to ourself") + return ErrConnectToSelf } return nil diff --git a/router/connection_maker.go b/router/connection_maker.go index 9e8c2ebc..9b5ebfdc 100644 --- a/router/connection_maker.go +++ b/router/connection_maker.go @@ -231,6 +231,9 @@ func (cm *ConnectionMaker) connectToTargets(validTarget map[string]struct{}, dir delete(cm.targets, address) continue } + if target.tryAfter.IsZero() { + continue + } switch duration := target.tryAfter.Sub(now); { case duration <= 0: target.attempting = true @@ -259,6 +262,11 @@ func (t *Target) tryNow() { // The delay at the nth retry is a random value in the range // [i-i/2,i+i/2], where i = InitialInterval * 1.5^(n-1). func (t *Target) retry() { + if t.lastError == ErrConnectToSelf { + t.tryAfter = time.Time{} + t.tryInterval = MaxInterval + return + } t.tryAfter = time.Now().Add(t.tryInterval/2 + time.Duration(rand.Int63n(int64(t.tryInterval)))) t.tryInterval = t.tryInterval * 3 / 2 if t.tryInterval > MaxInterval { diff --git a/router/status.go b/router/status.go index 84a8ee86..833c24d1 100644 --- a/router/status.go +++ b/router/status.go @@ -197,7 +197,11 @@ func NewLocalConnectionStatusSlice(cm *ConnectionMaker) []LocalConnectionStatus info = target.lastError.Error() default: state = "failed" - info = target.lastError.Error() + ", retry: " + target.tryAfter.String() + retry := "never" + if !target.tryAfter.IsZero() { + retry = target.tryAfter.String() + } + info = target.lastError.Error() + ", retry: " + retry } slice = append(slice, LocalConnectionStatus{address, true, state, info}) }