log mac expiry

for symmetry with mac discovery
and w/o polluting the mac_cache code with logging
This commit is contained in:
Matthias Radestock committed 2014-09-03 23:43:40 +01:00
1 parent a7e44b44dc
commit 9706934d89
2 files changed
+10 -4

No files matched your search

+6 -3
View File
@@ -18,12 +18,14 @@ type MacCache struct {
table map[uint64]*MacCacheEntry table map[uint64]*MacCacheEntry
maxAge time.Duration maxAge time.Duration
expiryTimer *time.Timer expiryTimer *time.Timer
onExpiry func(net.HardwareAddr, *Peer)
} }
func NewMacCache(maxAge time.Duration) *MacCache { func NewMacCache(maxAge time.Duration, onExpiry func(net.HardwareAddr, *Peer)) *MacCache {
res := &MacCache{ res := &MacCache{
table: make(map[uint64]*MacCacheEntry), table: make(map[uint64]*MacCacheEntry),
maxAge: maxAge} maxAge: maxAge,
onExpiry: onExpiry}
res.setExpiryTimer() res.setExpiryTimer()
return res return res
} }
@@ -102,6 +104,7 @@ func (cache *MacCache) expire() {
for key, entry := range cache.table { for key, entry := range cache.table {
if now.After(entry.lastSeen.Add(cache.maxAge)) { if now.After(entry.lastSeen.Add(cache.maxAge)) {
delete(cache.table, key) delete(cache.table, key)
cache.onExpiry(intmac(key), entry.peer)
} }
} }
cache.setExpiryTimer() cache.setExpiryTimer()
+4 -1
View File
@@ -15,9 +15,12 @@ import (
const macMaxAge = 10 * time.Minute const macMaxAge = 10 * time.Minute
func NewRouter(iface *net.Interface, name PeerName, password []byte, connLimit int, bufSz int, logFrame func(string, []byte, *layers.Ethernet)) *Router { func NewRouter(iface *net.Interface, name PeerName, password []byte, connLimit int, bufSz int, logFrame func(string, []byte, *layers.Ethernet)) *Router {
onMacExpiry := func(mac net.HardwareAddr, peer *Peer) {
log.Println("Expired MAC", mac, "at", peer.Name)
}
router := &Router{ router := &Router{
Iface: iface, Iface: iface,
Macs: NewMacCache(macMaxAge), Macs: NewMacCache(macMaxAge, onMacExpiry),
Peers: NewPeerCache(), Peers: NewPeerCache(),
ConnLimit: connLimit, ConnLimit: connLimit,
BufSz: bufSz, BufSz: bufSz,