-
Notifications
You must be signed in to change notification settings - Fork 68
fix(kad): add configurable option to purge stale entries on bucketrefreshInterval #2139
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -18,8 +18,14 @@ proc new*( | |||||||||||||||||||||||||||||||||||||||
| replication = DefaultReplication, | ||||||||||||||||||||||||||||||||||||||||
| hasher: Opt[XorDHasher] = NoneHasher, | ||||||||||||||||||||||||||||||||||||||||
| maxBuckets: int = DefaultMaxBuckets, | ||||||||||||||||||||||||||||||||||||||||
| purgeStaleEntries: bool = false, | ||||||||||||||||||||||||||||||||||||||||
| ): T = | ||||||||||||||||||||||||||||||||||||||||
| RoutingTableConfig(replication: replication, hasher: hasher, maxBuckets: maxBuckets) | ||||||||||||||||||||||||||||||||||||||||
| RoutingTableConfig( | ||||||||||||||||||||||||||||||||||||||||
| replication: replication, | ||||||||||||||||||||||||||||||||||||||||
| hasher: hasher, | ||||||||||||||||||||||||||||||||||||||||
| maxBuckets: maxBuckets, | ||||||||||||||||||||||||||||||||||||||||
| purgeStaleEntries: purgeStaleEntries, | ||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| proc `$`*(rt: RoutingTable): string = | ||||||||||||||||||||||||||||||||||||||||
| "selfId(" & $rt.selfId & ") buckets(" & $rt.buckets & ")" | ||||||||||||||||||||||||||||||||||||||||
|
|
@@ -134,6 +140,26 @@ proc findClosestPeerIds*(rtable: RoutingTable, targetId: Key, count: int): seq[P | |||||||||||||||||||||||||||||||||||||||
| .filterIt(it.isOk) | ||||||||||||||||||||||||||||||||||||||||
| .mapIt(it.value()) | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| proc purgeExpired*(rtable: var RoutingTable) = | ||||||||||||||||||||||||||||||||||||||||
| ## Remove entries from all buckets that have not been refreshed within | ||||||||||||||||||||||||||||||||||||||||
| ## DefaultBucketStaleTime. No-op if purgeStaleEntries is false. | ||||||||||||||||||||||||||||||||||||||||
| if not rtable.config.purgeStaleEntries: | ||||||||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| let now = Moment.now() | ||||||||||||||||||||||||||||||||||||||||
| var totalPurged = 0 | ||||||||||||||||||||||||||||||||||||||||
| for i in 0 ..< rtable.buckets.len: | ||||||||||||||||||||||||||||||||||||||||
| let before = rtable.buckets[i].peers.len | ||||||||||||||||||||||||||||||||||||||||
| rtable.buckets[i].peers.keepItIf(now - it.lastSeen <= DefaultBucketStaleTime) | ||||||||||||||||||||||||||||||||||||||||
| let purged = before - rtable.buckets[i].peers.len | ||||||||||||||||||||||||||||||||||||||||
| if purged > 0: | ||||||||||||||||||||||||||||||||||||||||
| debug "Purged stale routing table entries", bucketIdx = i, count = purged | ||||||||||||||||||||||||||||||||||||||||
| totalPurged += purged | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| if totalPurged > 0: | ||||||||||||||||||||||||||||||||||||||||
| kad_routing_table_replacements.inc(totalPurged) | ||||||||||||||||||||||||||||||||||||||||
| updateRoutingTableMetrics(rtable) | ||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+152
to
+161
|
||||||||||||||||||||||||||||||||||||||||
| let before = rtable.buckets[i].peers.len | |
| rtable.buckets[i].peers.keepItIf(now - it.lastSeen <= DefaultBucketStaleTime) | |
| let purged = before - rtable.buckets[i].peers.len | |
| if purged > 0: | |
| debug "Purged stale routing table entries", bucketIdx = i, count = purged | |
| totalPurged += purged | |
| if totalPurged > 0: | |
| kad_routing_table_replacements.inc(totalPurged) | |
| updateRoutingTableMetrics(rtable) | |
| let before = rtable.buckets[i].peers.len | |
| rtable.buckets[i].peers.keepItIf(now - it.lastSeen <= DefaultBucketStaleTime) | |
| let purged = before - rtable.buckets[i].peers.len | |
| if purged > 0: | |
| debug "Purged stale routing table entries", bucketIdx = i, count = purged | |
| totalPurged += purged | |
| if totalPurged > 0: | |
| updateRoutingTableMetrics(rtable) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There appears to be a discrepancy between the PR description and the linked issue. The linked issue #2134 discusses "KadDHT put values never expire" and mentions expiring KV pairs stored via putValue messages. However, this PR addresses stale routing table entries (peers), not KV pairs. While both are valid concerns about DHT bloat, they are different problems. Please verify that this PR is solving the intended problem or update the issue reference accordingly.