2025-02-07 16:48:47 +01:00
|
|
|
import pkg/chronicles
|
|
|
|
|
import pkg/chronos
|
|
|
|
|
import pkg/questionable/results
|
|
|
|
|
|
2025-02-11 16:31:23 +01:00
|
|
|
import ./nodestore
|
2025-02-12 14:25:54 +01:00
|
|
|
import ../services/dht
|
2025-02-13 10:49:50 +01:00
|
|
|
import ../services/clock
|
2025-02-10 14:49:30 +01:00
|
|
|
import ../component
|
2025-02-10 15:34:41 +01:00
|
|
|
import ../state
|
2025-02-11 16:31:23 +01:00
|
|
|
import ../types
|
|
|
|
|
import ../utils/asyncdataevent
|
2025-02-07 16:48:47 +01:00
|
|
|
|
|
|
|
|
logScope:
|
|
|
|
|
topics = "timetracker"
|
|
|
|
|
|
2025-02-10 14:49:30 +01:00
|
|
|
type TimeTracker* = ref object of Component
|
2025-02-11 16:31:23 +01:00
|
|
|
state: State
|
|
|
|
|
nodestore: NodeStore
|
2025-02-12 14:25:54 +01:00
|
|
|
dht: Dht
|
2025-02-13 10:49:50 +01:00
|
|
|
clock: Clock
|
2025-02-07 16:48:47 +01:00
|
|
|
|
2025-02-13 14:55:45 +01:00
|
|
|
proc checkRevisitsAndExpiry(t: TimeTracker): Future[?!void] {.async: (raises: []).} =
|
|
|
|
|
let
|
|
|
|
|
revisitThreshold = t.clock.now() - (t.state.config.revisitDelayMins * 60).uint64
|
|
|
|
|
expiryThreshold = t.clock.now() - (t.state.config.expiryDelayMins * 60).uint64
|
|
|
|
|
|
|
|
|
|
var
|
|
|
|
|
toRevisit = newSeq[Nid]()
|
|
|
|
|
toDelete = newSeq[Nid]()
|
2025-02-07 16:48:47 +01:00
|
|
|
|
2025-02-11 16:31:23 +01:00
|
|
|
proc checkNode(item: NodeEntry): Future[?!void] {.async: (raises: []), gcsafe.} =
|
2025-02-13 14:55:45 +01:00
|
|
|
if item.lastVisit < revisitThreshold:
|
|
|
|
|
toRevisit.add(item.id)
|
|
|
|
|
if item.firstInactive > 0 and item.firstInactive < expiryThreshold:
|
|
|
|
|
toDelete.add(item.id)
|
2025-02-11 16:31:23 +01:00
|
|
|
return success()
|
2025-02-07 16:48:47 +01:00
|
|
|
|
2025-02-11 16:31:23 +01:00
|
|
|
?await t.nodestore.iterateAll(checkNode)
|
2025-02-13 09:33:13 +01:00
|
|
|
|
2025-02-13 14:55:45 +01:00
|
|
|
if toRevisit.len > 0:
|
|
|
|
|
trace "Found nodes to revisit", toRevisit = toRevisit.len
|
|
|
|
|
?await t.state.events.nodesToRevisit.fire(toRevisit)
|
|
|
|
|
|
|
|
|
|
if toDelete.len > 0:
|
|
|
|
|
trace "Found expired node records to delete", toDelete = toDelete.len
|
|
|
|
|
?await t.nodestore.deleteEntries(toDelete)
|
2025-02-13 09:33:13 +01:00
|
|
|
|
2025-02-11 16:31:23 +01:00
|
|
|
return success()
|
2025-02-10 12:56:09 +01:00
|
|
|
|
2025-02-12 14:25:54 +01:00
|
|
|
proc raiseRoutingTableNodes(t: TimeTracker): Future[?!void] {.async: (raises: []).} =
|
|
|
|
|
let nids = t.dht.getRoutingTableNodeIds()
|
2025-02-13 09:33:13 +01:00
|
|
|
trace "Raising routing table nodes", nodes = nids.len
|
|
|
|
|
|
2025-02-12 14:25:54 +01:00
|
|
|
if err =? (await t.state.events.nodesFound.fire(nids)).errorOption:
|
2025-02-13 14:55:45 +01:00
|
|
|
error "failed to raise nodesFound event", err = err.msg
|
2025-02-12 14:25:54 +01:00
|
|
|
return failure(err)
|
|
|
|
|
return success()
|
|
|
|
|
|
2025-06-02 14:30:28 +02:00
|
|
|
method start*(t: TimeTracker): Future[?!void] {.async: (raises: [CancelledError]).} =
|
2025-03-19 15:52:50 +01:00
|
|
|
info "starting..."
|
2025-02-07 16:48:47 +01:00
|
|
|
|
2025-02-13 14:55:45 +01:00
|
|
|
proc onCheckRevisitAndExpiry(): Future[?!void] {.async: (raises: []), gcsafe.} =
|
|
|
|
|
await t.checkRevisitsAndExpiry()
|
2025-02-07 16:48:47 +01:00
|
|
|
|
2025-02-13 12:35:19 +01:00
|
|
|
proc onRoutingTable(): Future[?!void] {.async: (raises: []), gcsafe.} =
|
|
|
|
|
await t.raiseRoutingTableNodes()
|
2025-02-07 16:48:47 +01:00
|
|
|
|
2025-02-13 14:55:45 +01:00
|
|
|
await t.state.whileRunning(
|
|
|
|
|
onCheckRevisitAndExpiry, t.state.config.checkDelayMins.minutes
|
|
|
|
|
)
|
2025-02-13 12:35:19 +01:00
|
|
|
await t.state.whileRunning(onRoutingTable, 30.minutes)
|
2025-02-07 16:48:47 +01:00
|
|
|
return success()
|
|
|
|
|
|
2025-02-12 14:25:54 +01:00
|
|
|
proc new*(
|
2025-02-13 10:49:50 +01:00
|
|
|
T: type TimeTracker, state: State, nodestore: NodeStore, dht: Dht, clock: Clock
|
2025-02-12 14:25:54 +01:00
|
|
|
): TimeTracker =
|
2025-02-13 10:49:50 +01:00
|
|
|
TimeTracker(state: state, nodestore: nodestore, dht: dht, clock: clock)
|