Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

opt(kademlia): prune periodically #4774

Merged
merged 1 commit into from
Aug 26, 2024
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
21 changes: 19 additions & 2 deletions pkg/topology/kademlia/kademlia.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ const (
defaultBootNodeOverSaturationPeers = 20
defaultShortRetry = 30 * time.Second
defaultTimeToRetry = 2 * defaultShortRetry
defaultPruneWakeup = 5 * time.Minute
defaultBroadcastBinSize = 2
)

Expand Down Expand Up @@ -92,6 +93,7 @@ type Options struct {
BitSuffixLength *int
TimeToRetry *time.Duration
ShortRetry *time.Duration
PruneWakeup *time.Duration
SaturationPeers *int
OverSaturationPeers *int
BootnodeOverSaturationPeers *int
Expand All @@ -111,6 +113,7 @@ type kadOptions struct {

TimeToRetry time.Duration
ShortRetry time.Duration
PruneWakeup time.Duration
BitSuffixLength int // additional depth of common prefix for bin
SaturationPeers int
OverSaturationPeers int
Expand All @@ -131,6 +134,7 @@ func newKadOptions(o Options) kadOptions {
// copy or use default
TimeToRetry: defaultValDuration(o.TimeToRetry, defaultTimeToRetry),
ShortRetry: defaultValDuration(o.ShortRetry, defaultShortRetry),
PruneWakeup: defaultValDuration(o.PruneWakeup, defaultPruneWakeup),
BitSuffixLength: defaultValInt(o.BitSuffixLength, defaultBitSuffixLength),
SaturationPeers: defaultValInt(o.SaturationPeers, defaultSaturationPeers),
OverSaturationPeers: defaultValInt(o.OverSaturationPeers, defaultOverSaturationPeers),
Expand Down Expand Up @@ -529,6 +533,21 @@ func (k *Kad) manage() {
balanceChan := make(chan *peerConnInfo)
go k.connectionAttemptsHandler(ctx, &wg, neighbourhoodChan, balanceChan)

k.wg.Add(1)
go func() {
defer k.wg.Done()
for {
select {
case <-k.halt:
return
case <-k.quit:
return
case <-time.After(k.opt.PruneWakeup):
k.opt.PruneFunc(k.neighborhoodDepth())
}
}
}()

k.wg.Add(1)
go func() {
defer k.wg.Done()
Expand Down Expand Up @@ -615,8 +634,6 @@ func (k *Kad) manage() {

depth := k.neighborhoodDepth()

k.opt.PruneFunc(depth)

loggerV1.Debug("connector finished", "elapsed", time.Since(start), "old_depth", oldDepth, "new_depth", depth)

k.metrics.CurrentDepth.Set(float64(depth))
Expand Down
2 changes: 2 additions & 0 deletions pkg/topology/kademlia/kademlia_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1264,6 +1264,7 @@ func TestOutofDepthPrune(t *testing.T) {

saturationPeers = 4
overSaturationPeers = 16
pruneWakeup = time.Millisecond * 100
pruneFuncImpl *func(uint8)
pruneMux = sync.Mutex{}
pruneFunc = func(depth uint8) {
Expand All @@ -1278,6 +1279,7 @@ func TestOutofDepthPrune(t *testing.T) {
OverSaturationPeers: ptrInt(overSaturationPeers),
PruneFunc: pruneFunc,
ExcludeFunc: defaultExcludeFunc,
PruneWakeup: &pruneWakeup,
})
)

Expand Down
Loading