Skip to content

Commit

Permalink
Add PeerList.Remove to remove peers
Browse files Browse the repository at this point in the history
This removes a peer from a specific peer list. It does not affect any
existing connections to the peer.

If the peer is not reference from any other peer lists, and has no
connections, then it will get GC'd.
  • Loading branch information
prashantv committed Jan 16, 2016
1 parent 91f873e commit 7b1c504
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
27 changes: 27 additions & 0 deletions peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ var (
// ErrNoPeers indicates that there are no peers.
ErrNoPeers = errors.New("no peers available")

// ErrPeerNotFound indicates that the specified peer was not found.
ErrPeerNotFound = errors.New("peer not found")

peerRng = NewRand(time.Now().UnixNano())
)

Expand Down Expand Up @@ -122,6 +125,23 @@ func (l *PeerList) Get(prevSelected map[string]struct{}) (*Peer, error) {
return peer, nil
}

// Remove removes a peer from the peer list. It returns an error if the peer cannot be found.
// Remove does not affect connections to the peer in any way.
func (l *PeerList) Remove(hostPort string) error {
l.Lock()
defer l.Unlock()

p, ok := l.peersByHostPort[hostPort]
if !ok {
return ErrPeerNotFound
}

p.delSC()
delete(l.peersByHostPort, hostPort)
l.peerHeap.removePeer(p)

return nil
}
func (l *PeerList) choosePeer(prevSelected map[string]struct{}, avoidHost bool) *Peer {
var psPopList []*peerScore
var ps *peerScore
Expand Down Expand Up @@ -327,6 +347,13 @@ func (p *Peer) addSC() {
p.Unlock()
}

// delSC removes a reference to a peer from a subchannel (e.g. peer list).
func (p *Peer) delSC() {
p.Lock()
p.scCount--
p.Unlock()
}

// canRemove returns whether this peer can be safely removed from the root peer list.
func (p *Peer) canRemove() bool {
p.RLock()
Expand Down
32 changes: 28 additions & 4 deletions peer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,32 @@ func TestOutboundPeerNotAdded(t *testing.T) {
})
}

func TestRemovePeerNotFound(t *testing.T) {
peers := testutils.NewClient(t, nil).Peers()
peers.Add("1.1.1.1:1")
assert.Error(t, peers.Remove("not-found"), "Remove should fa")
assert.NoError(t, peers.Remove("1.1.1.1:1"), "Remove shouldn't fail for existing peer")
}

func TestPeerRemovedFromRootPeers(t *testing.T) {
tests := []struct {
addHostPort bool
expectFound bool
addHostPort bool
removeHostPort bool
expectFound bool
}{
{true, true},
{false, false},
{
addHostPort: true,
expectFound: true,
},
{
addHostPort: true,
removeHostPort: true,
expectFound: false,
},
{
addHostPort: false,
expectFound: false,
},
}

ctx, cancel := NewContext(time.Second)
Expand All @@ -203,6 +222,11 @@ func TestPeerRemovedFromRootPeers(t *testing.T) {

assert.NoError(t, ch.Ping(ctx, hostPort), "Ping failed")

if tt.removeHostPort {
require.NoError(t, server.Peers().Remove(clientHP), "Failed to remove peer")

}

waitTillInboundEmpty(t, server, clientHP, func() {
ch.Close()
})
Expand Down

0 comments on commit 7b1c504

Please sign in to comment.