refactor: rename PeerCache to Peers

It was never much of a cache, and that aspect of it is certainly not
its defining characteristic.
This commit is contained in:
Matthias Radestock committed 2014-12-24 19:23:43 +00:00
1 parent e8f836ad85
commit 2b2f47c031
4 files changed
+66 -67

No files matched your search

+12 -13
View File
@@ -96,19 +96,18 @@ Router:
directly connected neighbours via TCP. This network update message directly connected neighbours via TCP. This network update message
contains the relevant changes to the network topology due to the contains the relevant changes to the network topology due to the
connection change. When such a message is received by a TCP connection change. When such a message is received by a TCP
receiver threads, they invoke the peers cache to apply the update receiver threads, they apply the update to the local model of the
to the local model of the network topology. This may fail for a network topology. This may fail for a number of reasons (for
number of reasons (for example the update may contain reference to example the update may contain reference to peers that we have no
peers that we have no prior knowledge of. In this case, we ignore prior knowledge of. In this case, we ignore the update and send
the update and send back to the peer from whom we received the back to the peer from whom we received the update a request for
update a request for the complete network topology), or it may the complete network topology), or it may apply and elicit some
apply and elicit some changes to our model. If it does elicit some changes to our model. If it does elicit some changes then we send
changes then we send out an updated update message to all our out an updated update message to all our peers. In this way the
peers. In this way the changes are passed quickly from peer to changes are passed quickly from peer to peer based on the
peer based on the established connections and stop being sent once established connections and stop being sent once a received update
a received update causes no changes to the peer's topology causes no changes to the peer's topology model. Changes are
model. Changes are additive and care is taken to ensure that no additive and care is taken to ensure that no loops can occur.
loops can occur.
Routing Routing
+52 -52
View File
@@ -8,55 +8,55 @@ import (
"sync" "sync"
) )
type PeerCache struct { type Peers struct {
sync.RWMutex sync.RWMutex
table map[PeerName]*Peer table map[PeerName]*Peer
onGC func(*Peer) onGC func(*Peer)
} }
func NewPeerCache(onGC func(*Peer)) *PeerCache { func NewPeers(onGC func(*Peer)) *Peers {
return &PeerCache{table: make(map[PeerName]*Peer), onGC: onGC} return &Peers{table: make(map[PeerName]*Peer), onGC: onGC}
} }
func (cache *PeerCache) FetchWithDefault(peer *Peer) *Peer { func (peers *Peers) FetchWithDefault(peer *Peer) *Peer {
cache.RLock() peers.RLock()
res, found := cache.fetchAlias(peer) res, found := peers.fetchAlias(peer)
cache.RUnlock() peers.RUnlock()
if found { if found {
return res return res
} }
cache.Lock() peers.Lock()
defer cache.Unlock() defer peers.Unlock()
res, found = cache.fetchAlias(peer) res, found = peers.fetchAlias(peer)
if found { if found {
return res return res
} }
cache.table[peer.Name] = peer peers.table[peer.Name] = peer
peer.IncrementLocalRefCount() peer.IncrementLocalRefCount()
return peer return peer
} }
func (cache *PeerCache) Fetch(name PeerName) (*Peer, bool) { func (peers *Peers) Fetch(name PeerName) (*Peer, bool) {
cache.RLock() peers.RLock()
defer cache.RUnlock() defer peers.RUnlock()
peer, found := cache.table[name] peer, found := peers.table[name]
return peer, found // GRRR, why can't I inline this!? return peer, found // GRRR, why can't I inline this!?
} }
func (cache *PeerCache) ForEach(fun func(PeerName, *Peer)) { func (peers *Peers) ForEach(fun func(PeerName, *Peer)) {
cache.RLock() peers.RLock()
defer cache.RUnlock() defer peers.RUnlock()
for name, peer := range cache.table { for name, peer := range peers.table {
fun(name, peer) fun(name, peer)
} }
} }
func (cache *PeerCache) ApplyUpdate(update []byte, router *Router) ([]byte, error) { func (peers *Peers) ApplyUpdate(update []byte, router *Router) ([]byte, error) {
cache.Lock() peers.Lock()
newPeers, decodedUpdate, decodedConns, err := cache.decodeUpdate(update, router) newPeers, decodedUpdate, decodedConns, err := peers.decodeUpdate(update, router)
if err != nil { if err != nil {
cache.Unlock() peers.Unlock()
return nil, err return nil, err
} }
@@ -64,18 +64,18 @@ func (cache *PeerCache) ApplyUpdate(update []byte, router *Router) ([]byte, erro
// have no knowledge of. We can now apply the update. Start by // have no knowledge of. We can now apply the update. Start by
// adding in any new peers into the cache. // adding in any new peers into the cache.
for name, newPeer := range newPeers { for name, newPeer := range newPeers {
cache.table[name] = newPeer peers.table[name] = newPeer
} }
// Now apply the updates // Now apply the updates
newUpdate := cache.applyUpdate(decodedUpdate, decodedConns, router) newUpdate := peers.applyUpdate(decodedUpdate, decodedConns, router)
for _, peerRemoved := range cache.garbageCollect(router.Ourself) { for _, peerRemoved := range peers.garbageCollect(router.Ourself) {
delete(newUpdate, peerRemoved.Name) delete(newUpdate, peerRemoved.Name)
} }
// Don't need to hold cache lock any longer // Don't need to hold peers lock any longer
cache.Unlock() peers.Unlock()
if len(newUpdate) > 0 { if len(newUpdate) > 0 {
router.ConnectionMaker.Refresh() router.ConnectionMaker.Refresh()
@@ -84,10 +84,10 @@ func (cache *PeerCache) ApplyUpdate(update []byte, router *Router) ([]byte, erro
return encodePeersMap(newUpdate), nil return encodePeersMap(newUpdate), nil
} }
func (cache *PeerCache) EncodeAllPeers() []byte { func (peers *Peers) EncodeAllPeers() []byte {
cache.RLock() peers.RLock()
defer cache.RUnlock() defer peers.RUnlock()
return encodePeersMap(cache.table) return encodePeersMap(peers.table)
} }
func EncodePeers(peers ...*Peer) []byte { func EncodePeers(peers ...*Peer) []byte {
@@ -99,15 +99,15 @@ func EncodePeers(peers ...*Peer) []byte {
return buf.Bytes() return buf.Bytes()
} }
func (cache *PeerCache) GarbageCollect(router *Router) []*Peer { func (peers *Peers) GarbageCollect(router *Router) []*Peer {
cache.Lock() peers.Lock()
defer cache.Unlock() defer peers.Unlock()
return cache.garbageCollect(router.Ourself) return peers.garbageCollect(router.Ourself)
} }
func (cache *PeerCache) String() string { func (peers *Peers) String() string {
var buf bytes.Buffer var buf bytes.Buffer
cache.ForEach(func(name PeerName, peer *Peer) { peers.ForEach(func(name PeerName, peer *Peer) {
buf.WriteString(fmt.Sprint(peer, "\n")) buf.WriteString(fmt.Sprint(peer, "\n"))
peer.ForEachConnection(func(remoteName PeerName, conn Connection) { peer.ForEachConnection(func(remoteName PeerName, conn Connection) {
buf.WriteString(fmt.Sprintf(" -> %v [%v]\n", remoteName, conn.RemoteTCPAddr())) buf.WriteString(fmt.Sprintf(" -> %v [%v]\n", remoteName, conn.RemoteTCPAddr()))
@@ -116,8 +116,8 @@ func (cache *PeerCache) String() string {
return buf.String() return buf.String()
} }
func (cache *PeerCache) fetchAlias(peer *Peer) (*Peer, bool) { func (peers *Peers) fetchAlias(peer *Peer) (*Peer, bool) {
if existingPeer, found := cache.table[peer.Name]; found { if existingPeer, found := peers.table[peer.Name]; found {
if existingPeer.UID == peer.UID { if existingPeer.UID == peer.UID {
existingPeer.IncrementLocalRefCount() existingPeer.IncrementLocalRefCount()
return existingPeer, true return existingPeer, true
@@ -128,13 +128,13 @@ func (cache *PeerCache) fetchAlias(peer *Peer) (*Peer, bool) {
return nil, false return nil, false
} }
func (cache *PeerCache) garbageCollect(ourself *Peer) []*Peer { func (peers *Peers) garbageCollect(ourself *Peer) []*Peer {
removed := []*Peer{} removed := []*Peer{}
for name, peer := range cache.table { for name, peer := range peers.table {
found, _ := ourself.Routes(peer, false) found, _ := ourself.Routes(peer, false)
if !found && !peer.IsLocallyReferenced() { if !found && !peer.IsLocallyReferenced() {
cache.onGC(peer) peers.onGC(peer)
delete(cache.table, name) delete(peers.table, name)
ourself.Router.Macs.Delete(peer) ourself.Router.Macs.Delete(peer)
removed = append(removed, peer) removed = append(removed, peer)
} }
@@ -151,7 +151,7 @@ func encodePeersMap(peers map[PeerName]*Peer) []byte {
return buf.Bytes() return buf.Bytes()
} }
func (cache *PeerCache) decodeUpdate(update []byte, router *Router) (newPeers map[PeerName]*Peer, decodedUpdate []*Peer, decodedConns [][]byte, err error) { func (peers *Peers) decodeUpdate(update []byte, router *Router) (newPeers map[PeerName]*Peer, decodedUpdate []*Peer, decodedConns [][]byte, err error) {
newPeers = make(map[PeerName]*Peer) newPeers = make(map[PeerName]*Peer)
decodedUpdate = []*Peer{} decodedUpdate = []*Peer{}
decodedConns = [][]byte{} decodedConns = [][]byte{}
@@ -172,7 +172,7 @@ func (cache *PeerCache) decodeUpdate(update []byte, router *Router) (newPeers ma
newPeer := NewPeer(name, uid, version, router) newPeer := NewPeer(name, uid, version, router)
decodedUpdate = append(decodedUpdate, newPeer) decodedUpdate = append(decodedUpdate, newPeer)
decodedConns = append(decodedConns, connsBuf) decodedConns = append(decodedConns, connsBuf)
existingPeer, found := cache.table[name] existingPeer, found := peers.table[name]
if !found { if !found {
newPeers[name] = newPeer newPeers[name] = newPeer
} else if existingPeer.UID != newPeer.UID { } else if existingPeer.UID != newPeer.UID {
@@ -188,7 +188,7 @@ func (cache *PeerCache) decodeUpdate(update []byte, router *Router) (newPeers ma
if _, found := newPeers[remoteName]; found { if _, found := newPeers[remoteName]; found {
return return
} }
if _, found := cache.table[remoteName]; found { if _, found := peers.table[remoteName]; found {
return return
} }
// Update refers to a peer which we have no knowledge // Update refers to a peer which we have no knowledge
@@ -203,13 +203,13 @@ func (cache *PeerCache) decodeUpdate(update []byte, router *Router) (newPeers ma
return return
} }
func (cache *PeerCache) applyUpdate(decodedUpdate []*Peer, decodedConns [][]byte, router *Router) map[PeerName]*Peer { func (peers *Peers) applyUpdate(decodedUpdate []*Peer, decodedConns [][]byte, router *Router) map[PeerName]*Peer {
newUpdate := make(map[PeerName]*Peer) newUpdate := make(map[PeerName]*Peer)
for idx, newPeer := range decodedUpdate { for idx, newPeer := range decodedUpdate {
connsBuf := decodedConns[idx] connsBuf := decodedConns[idx]
name := newPeer.Name name := newPeer.Name
// guaranteed to find peer in the cache.table // guaranteed to find peer in the peers.table
peer := cache.table[name] peer := peers.table[name]
if peer != newPeer { if peer != newPeer {
if peer.Version() > newPeer.Version() { if peer.Version() > newPeer.Version() {
// we know more about this one than the update. If // we know more about this one than the update. If
@@ -237,8 +237,8 @@ func (cache *PeerCache) applyUpdate(decodedUpdate []*Peer, decodedConns [][]byte
// No - we know that peer is not ourself, so the only prospect // No - we know that peer is not ourself, so the only prospect
// for an update would be someone else calling // for an update would be someone else calling
// router.Peers.ApplyUpdate. But ApplyUpdate takes the Lock on // router.Peers.ApplyUpdate. But ApplyUpdate takes the Lock on
// the peers cache, so there can be no race here. // the router.Peers, so there can be no race here.
conns := readConnsMap(peer, connsBuf, cache.table) conns := readConnsMap(peer, connsBuf, peers.table)
peer.SetVersionAndConnections(newPeer.Version(), conns) peer.SetVersionAndConnections(newPeer.Version(), conns)
newUpdate[name] = peer newUpdate[name] = peer
} }
+1 -1
View File
@@ -27,7 +27,7 @@ func NewRouter(iface *net.Interface, name PeerName, password []byte, connLimit i
router := &Router{ router := &Router{
Iface: iface, Iface: iface,
Macs: NewMacCache(macMaxAge, onMacExpiry), Macs: NewMacCache(macMaxAge, onMacExpiry),
Peers: NewPeerCache(onPeerGC), Peers: NewPeers(onPeerGC),
ConnLimit: connLimit, ConnLimit: connLimit,
BufSz: bufSz, BufSz: bufSz,
LogFrame: logFrame} LogFrame: logFrame}
+1 -1
View File
@@ -15,7 +15,7 @@ type Router struct {
Ourself *Peer Ourself *Peer
Iface *net.Interface Iface *net.Interface
Macs *MacCache Macs *MacCache
Peers *PeerCache Peers *Peers
UDPListener *net.UDPConn UDPListener *net.UDPConn
Routes *Routes Routes *Routes
ConnectionMaker *ConnectionMaker ConnectionMaker *ConnectionMaker