Commit 1b3d1588 authored by Jeromy's avatar Jeromy
Browse files

add listing method to backoff for eventual api access

parent 40a5c58c
...@@ -157,8 +157,8 @@ type dialbackoff struct { ...@@ -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() { ...@@ -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) { ...@@ -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) { ...@@ -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
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment