Skip to content
This repository has been archived by the owner on Jul 6, 2018. It is now read-only.

Commit

Permalink
add public RemoveAddress API
Browse files Browse the repository at this point in the history
after discussion with @ebuchman (#10 (comment))
  • Loading branch information
melekes committed Jan 20, 2017
1 parent 4d04534 commit 6562d9b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
10 changes: 8 additions & 2 deletions addrbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,15 +248,21 @@ func (a *AddrBook) MarkAttempt(addr *NetAddress) {
ka.markAttempt()
}

// MarkBad currently just ejects the address. In the future, consider
// blacklisting.
func (a *AddrBook) MarkBad(addr *NetAddress) {
a.RemoveAddress(addr)
}

// RemoveAddress removes the address from the book.
func (a *AddrBook) RemoveAddress(addr *NetAddress) {
a.mtx.Lock()
defer a.mtx.Unlock()
ka := a.addrLookup[addr.String()]
if ka == nil {
return
}
// We currently just eject the address.
// In the future, consider blacklisting.
log.Info("Remove address from book", "addr", addr)
a.removeFromAllBuckets(ka)
}

Expand Down
18 changes: 18 additions & 0 deletions addrbook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"io/ioutil"
"math/rand"
"testing"

"github.com/stretchr/testify/assert"
)

const addrBookStrict = true
Expand Down Expand Up @@ -161,3 +163,19 @@ func TestPromoteToOld(t *testing.T) {
selection := book.GetSelection()
t.Logf("selection: %v", selection)
}

func TestAddrBookRemoveAddress(t *testing.T) {
fname := createTempFileName("addrbook_test")
book := NewAddrBook(fname, true)

addr := randIPv4Address()
book.AddAddress(addr, addr)
assert.Equal(t, 1, book.Size())

book.RemoveAddress(addr)
assert.Equal(t, 0, book.Size())

nonExistingAddr := randIPv4Address()
book.RemoveAddress(nonExistingAddr)
assert.Equal(t, 0, book.Size())
}
8 changes: 6 additions & 2 deletions pex_reactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,14 @@ func (r *PEXReactor) AddPeer(p *Peer) {
}

// RemovePeer implements Reactor by removing peer from the address book.
//
// The peer will be proposed to us by other peers (PexAddrsMessage) or himself
// and we will add him again upon successful connection. Note that other peers
// will remove him too. The peer will need to send first requests to others by
// himself (he will have an addrbook or the seeds).
func (r *PEXReactor) RemovePeer(p *Peer, reason interface{}) {
addr := NewNetAddressString(p.ListenAddr)
// addr will be ejected from the book
r.book.MarkBad(addr)
r.book.RemoveAddress(addr)
}

// Receive implements Reactor by handling incoming PEX messages.
Expand Down

0 comments on commit 6562d9b

Please sign in to comment.