mirror of
https://github.com/libp2p/go-libp2p.git
synced 2025-10-05 16:17:12 +08:00
add listing method to backoff for eventual api access
This commit is contained in:
@@ -157,8 +157,8 @@ type dialbackoff struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type backoffPeer struct {
|
type backoffPeer struct {
|
||||||
tries int
|
Tries int
|
||||||
until time.Time
|
Until time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *dialbackoff) init() {
|
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
|
// Backoff returns whether the client should backoff from dialing
|
||||||
// peer p
|
// peer p
|
||||||
func (db *dialbackoff) Backoff(p peer.ID) (backoff bool) {
|
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()
|
defer db.lock.Unlock()
|
||||||
db.init()
|
db.init()
|
||||||
bp, found := db.entries[p]
|
bp, found := db.entries[p]
|
||||||
if found && time.Now().Before(bp.until) {
|
if found && time.Now().Before(bp.Until) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -194,18 +205,18 @@ func (db *dialbackoff) AddBackoff(p peer.ID) {
|
|||||||
bp, ok := db.entries[p]
|
bp, ok := db.entries[p]
|
||||||
if !ok {
|
if !ok {
|
||||||
db.entries[p] = &backoffPeer{
|
db.entries[p] = &backoffPeer{
|
||||||
tries: 1,
|
Tries: 1,
|
||||||
until: time.Now().Add(baseBackoffTime),
|
Until: time.Now().Add(baseBackoffTime),
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
expTimeAdd := time.Second * time.Duration(bp.tries*bp.tries)
|
expTimeAdd := time.Second * time.Duration(bp.Tries*bp.Tries)
|
||||||
if expTimeAdd > maxBackoffTime {
|
if expTimeAdd > maxBackoffTime {
|
||||||
expTimeAdd = maxBackoffTime
|
expTimeAdd = maxBackoffTime
|
||||||
}
|
}
|
||||||
bp.until = time.Now().Add(baseBackoffTime + expTimeAdd)
|
bp.Until = time.Now().Add(baseBackoffTime + expTimeAdd)
|
||||||
bp.tries++
|
bp.Tries++
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clear removes a backoff record. Clients should call this after a
|
// Clear removes a backoff record. Clients should call this after a
|
||||||
|
Reference in New Issue
Block a user