Prevents duplicate nodes from entering todo-list

This commit is contained in:
thatben 2025-02-13 09:33:13 +01:00
parent a7367c2205
commit 4ffbf1f421
No known key found for this signature in database
GPG Key ID: 62C543548433D43E
4 changed files with 18 additions and 4 deletions

View File

@ -71,6 +71,8 @@ proc storeNodeIsNew(s: NodeStore, nid: Nid): Future[?!bool] {.async.} =
let entry = NodeEntry(id: nid, lastVisit: 0)
?await s.store.put(key, entry)
info "New node", nodeId = $nid
return success(not exists)
proc fireNewNodesDiscovered(s: NodeStore, nids: seq[Nid]): Future[?!void] {.async.} =

View File

@ -18,7 +18,6 @@ type TimeTracker* = ref object of Component
dht: Dht
proc checkForExpiredNodes(t: TimeTracker): Future[?!void] {.async: (raises: []).} =
trace "Checking for expired nodes..."
let expiry =
(Moment.now().epochSeconds - (t.state.config.revisitDelayMins * 60)).uint64
@ -29,12 +28,17 @@ proc checkForExpiredNodes(t: TimeTracker): Future[?!void] {.async: (raises: []).
return success()
?await t.nodestore.iterateAll(checkNode)
?await t.state.events.nodesExpired.fire(expired)
if expired.len > 0:
trace "Found expired nodes", expired = expired.len
?await t.state.events.nodesExpired.fire(expired)
return success()
proc raiseRoutingTableNodes(t: TimeTracker): Future[?!void] {.async: (raises: []).} =
trace "Raising routing table nodes..."
let nids = t.dht.getRoutingTableNodeIds()
trace "Raising routing table nodes", nodes = nids.len
if err =? (await t.state.events.nodesFound.fire(nids)).errorOption:
return failure(err)
return success()

View File

@ -26,7 +26,8 @@ type TodoList* = ref object of Component
proc addNodes(t: TodoList, nids: seq[Nid]) =
for nid in nids:
t.nids.add(nid)
if nid notin t.nids:
t.nids.add(nid)
t.metrics.setTodoNodes(t.nids.len)

View File

@ -62,6 +62,13 @@ suite "TodoList":
check:
metrics.todo == 1
test "does not store duplicates":
await fireNewNodesDiscoveredEvent(@[nid])
await fireNodesExpiredEvent(@[nid])
check:
metrics.todo == 1
test "pop on empty todo list waits until item is added":
let popFuture = todo.pop()
check: