diff --git a/p2p/net/swarm/swarm_dial.go b/p2p/net/swarm/swarm_dial.go index 67747212478b9c926174cbed13c64b07dfb3248a..e6cbc64ec617fc2d2aba8277789e9681f6bbf555 100644 --- a/p2p/net/swarm/swarm_dial.go +++ b/p2p/net/swarm/swarm_dial.go @@ -157,8 +157,8 @@ type dialbackoff struct { } type backoffPeer struct { - tries int - until time.Time + Tries int + Until time.Time } func (db *dialbackoff) init() { @@ -167,6 +167,17 @@ func (db *dialbackoff) init() { } } +func (db *dialbackoff) Listing() map[peer.ID]*backoffPeer { + db.lock.Lock() + defer db.lock.Unlock() + out := make(map[peer.ID]*backoffPeer) + for k, v := range db.entries { + p := *v + out[k] = &p + } + return out +} + // Backoff returns whether the client should backoff from dialing // peer p func (db *dialbackoff) Backoff(p peer.ID) (backoff bool) { @@ -174,7 +185,7 @@ func (db *dialbackoff) Backoff(p peer.ID) (backoff bool) { defer db.lock.Unlock() db.init() bp, found := db.entries[p] - if found && time.Now().Before(bp.until) { + if found && time.Now().Before(bp.Until) { return true } @@ -194,18 +205,18 @@ func (db *dialbackoff) AddBackoff(p peer.ID) { bp, ok := db.entries[p] if !ok { db.entries[p] = &backoffPeer{ - tries: 1, - until: time.Now().Add(baseBackoffTime), + Tries: 1, + Until: time.Now().Add(baseBackoffTime), } return } - expTimeAdd := time.Second * time.Duration(bp.tries*bp.tries) + expTimeAdd := time.Second * time.Duration(bp.Tries*bp.Tries) if expTimeAdd > maxBackoffTime { expTimeAdd = maxBackoffTime } - bp.until = time.Now().Add(baseBackoffTime + expTimeAdd) - bp.tries++ + bp.Until = time.Now().Add(baseBackoffTime + expTimeAdd) + bp.Tries++ } // Clear removes a backoff record. Clients should call this after a