2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-08-17 20:11:46 +01:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package kademlia
|
|
|
|
|
|
|
|
import (
|
2018-09-18 05:39:06 +01:00
|
|
|
"storj.io/storj/pkg/pb"
|
2018-08-17 20:11:46 +01:00
|
|
|
)
|
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
func (rt *RoutingTable) addToReplacementCache(kadBucketID bucketID, node *pb.Node) {
|
2019-02-22 18:39:29 +00:00
|
|
|
rt.rcMutex.Lock()
|
|
|
|
defer rt.rcMutex.Unlock()
|
2018-11-29 18:39:27 +00:00
|
|
|
nodes := rt.replacementCache[kadBucketID]
|
2018-08-17 20:11:46 +01:00
|
|
|
nodes = append(nodes, node)
|
2019-02-22 18:39:29 +00:00
|
|
|
|
2018-08-17 20:11:46 +01:00
|
|
|
if len(nodes) > rt.rcBucketSize {
|
|
|
|
copy(nodes, nodes[1:])
|
|
|
|
nodes = nodes[:len(nodes)-1]
|
|
|
|
}
|
2018-11-29 18:39:27 +00:00
|
|
|
rt.replacementCache[kadBucketID] = nodes
|
2018-08-17 20:11:46 +01:00
|
|
|
}
|
2019-02-22 18:39:29 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|