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 {
}
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
......
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