storj/pkg/kademlia/replacement_cache.go
Jennifer Li Johnson b5447c6608
Routing table tests (#1270)
This PR includes a new package called testrouting, which implements a very algorithmically slow but hopefully easy-to-keep-operationally-correct in-memory routing table. The routing table also supports writing out its current structure as a DOT graph for visualization. testrouting is primarily meant to help in coming up with generic routing table integration tests.

This PR also includes a new routing table integration test suite that runs against all current routing table implementations. Our existing routing table passes a lot of the tests, but not all of them, still debugging why. I have confirmed the tests should pass with the visualization graphs though.
2019-02-22 13:39:29 -05:00

35 lines
867 B
Go

// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package kademlia
import (
"storj.io/storj/pkg/pb"
)
func (rt *RoutingTable) addToReplacementCache(kadBucketID bucketID, node *pb.Node) {
rt.rcMutex.Lock()
defer rt.rcMutex.Unlock()
nodes := rt.replacementCache[kadBucketID]
nodes = append(nodes, node)
if len(nodes) > rt.rcBucketSize {
copy(nodes, nodes[1:])
nodes = nodes[:len(nodes)-1]
}
rt.replacementCache[kadBucketID] = nodes
}
func (rt *RoutingTable) removeFromReplacementCache(kadBucketID bucketID, node *pb.Node) {
rt.rcMutex.Lock()
defer rt.rcMutex.Unlock()
nodes := rt.replacementCache[kadBucketID]
for i, n := range nodes {
if n.Id == node.Id && n.Address.GetAddress() == node.Address.GetAddress() {
nodes = append(nodes[:i], nodes[i+1:]...)
break
}
}
rt.replacementCache[kadBucketID] = nodes
}