Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions gossip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ func TestGossipTopology(t *testing.T) {
// Drop the connection from 1 to 3
r1.DeleteTestGossipConnection(r3)
sendPendingGossip(r1, r2, r3)
forcePendingGC(r1, r2, r3)
checkTopology(t, r1, r1.tp(r2), r2.tp(r1))
checkTopology(t, r2, r1.tp(r2), r2.tp(r1))
// r3 still thinks r1 has a connection to it
Expand Down
13 changes: 13 additions & 0 deletions peer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,16 @@ func TestPeerRoutes(t *testing.T) {
func TestPeerForEachConnectedPeer(t *testing.T) {
t.Skip("TODO")
}

func forcePendingGC(routers ...*Router) {
for _, router := range routers {
router.Peers.Lock()
if router.Peers.pendingGC {
var pending peersPendingNotifications
router.Peers.garbageCollect(&pending)
router.Peers.unlockAndNotify(&pending)
} else {
router.Peers.Unlock()
}
}
}
30 changes: 29 additions & 1 deletion peers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import (
"io"
"math/rand"
"sync"
"time"
)

const (
gcInterval = 1 * time.Second
)

// Peers collects all of the known peers in the mesh, including ourself.
Expand All @@ -18,6 +23,8 @@ type Peers struct {

// Called when the mapping from short IDs to peers changes
onInvalidateShortIDs []func()
timer *time.Timer
pendingGC bool
}

type shortIDPeers struct {
Expand Down Expand Up @@ -60,8 +67,11 @@ func newPeers(ourself *localPeer) *Peers {
ourself: ourself,
byName: make(map[PeerName]*Peer),
byShortID: make(map[PeerShortID]shortIDPeers),
timer: time.NewTimer(gcInterval),
}
peers.fetchWithDefault(ourself.Peer)
peers.timer.Stop()
go peers.actorLoop()
return peers
}

Expand Down Expand Up @@ -339,6 +349,18 @@ func (peers *Peers) forEach(fun func(*Peer)) {
}
}

func (peers *Peers) actorLoop() {
for {
select {
case <-peers.timer.C:
peers.GarbageCollect()
peers.Lock()
peers.pendingGC = false
peers.Unlock()
}
}
}

// Merge an incoming update with our own topology.
//
// We add peers hitherto unknown to us, and update peers for which the
Expand All @@ -363,7 +385,6 @@ func (peers *Peers) applyUpdate(update []byte) (peerNameSet, peerNameSet, error)

// Now apply the updates
newUpdate := peers.applyDecodedUpdate(decodedUpdate, decodedConns, &pending)
peers.garbageCollect(&pending)
for _, peerRemoved := range pending.removed {
delete(newUpdate, peerRemoved.Name)
}
Expand All @@ -373,6 +394,13 @@ func (peers *Peers) applyUpdate(update []byte) (peerNameSet, peerNameSet, error)
updateNames[peer.Name] = struct{}{}
}

if !peers.pendingGC {
// schedule a GarbageCollect() to run after gcInterval time period
// corresponding to all topology updates received during the period
peers.timer.Reset(gcInterval)
peers.pendingGC = true
}

return updateNames, newUpdate, nil
}

Expand Down