Skip to content
This repository has been archived by the owner on May 26, 2022. It is now read-only.

Commit

Permalink
backoff: rm init().
Browse files Browse the repository at this point in the history
  • Loading branch information
raulk committed Nov 9, 2018
1 parent fa40242 commit f4e2dfe
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions dial/backoff.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ type Backoff struct {
}

func NewBackoff() Preparer {
return &Backoff{}
return &Backoff{
entries: make(map[peer.ID]*backoffPeer),
}
}

func (db *Backoff) Prepare(req *Request) {
Expand All @@ -87,18 +89,12 @@ type backoffPeer struct {
until time.Time
}

func (db *Backoff) init() {
if db.entries == nil {
db.entries = make(map[peer.ID]*backoffPeer)
}
}

// Backoff returns whether the client should backoff from dialing
// peer p
func (db *Backoff) Backoff(p peer.ID) (backoff bool) {
db.lock.Lock()
defer db.lock.Unlock()
db.init()

bp, found := db.entries[p]
if found && time.Now().Before(bp.until) {
return true
Expand All @@ -119,7 +115,7 @@ func (db *Backoff) Backoff(p peer.ID) (backoff bool) {
func (db *Backoff) AddBackoff(p peer.ID) {
db.lock.Lock()
defer db.lock.Unlock()
db.init()

bp, ok := db.entries[p]
if !ok {
db.entries[p] = &backoffPeer{
Expand All @@ -142,6 +138,6 @@ func (db *Backoff) AddBackoff(p peer.ID) {
func (db *Backoff) ClearBackoff(p peer.ID) {
db.lock.Lock()
defer db.lock.Unlock()
db.init()

delete(db.entries, p)
}

0 comments on commit f4e2dfe

Please sign in to comment.