Adds heartbeat loop and loopmeasure to task handler for discovery.nim

This commit is contained in:
benbierens 2023-07-03 14:53:53 +02:00
parent 30ba4bd3d4
commit 91bb6daa19
No known key found for this signature in database
GPG Key ID: FE44815D96D0A1AA
3 changed files with 17 additions and 2 deletions

View File

@ -54,6 +54,7 @@ type
advertiseQueue*: AsyncQueue[Cid] # Advertise queue advertiseQueue*: AsyncQueue[Cid] # Advertise queue
advertiseTasks*: seq[Future[void]] # Advertise tasks advertiseTasks*: seq[Future[void]] # Advertise tasks
discoveryLoop*: Future[void] # Discovery loop task handle discoveryLoop*: Future[void] # Discovery loop task handle
heartbeatLoop*: Future[void]
discoveryQueue*: AsyncQueue[Cid] # Discovery queue discoveryQueue*: AsyncQueue[Cid] # Discovery queue
discoveryTasks*: seq[Future[void]] # Discovery tasks discoveryTasks*: seq[Future[void]] # Discovery tasks
minPeersPerBlock*: int # Max number of peers with block minPeersPerBlock*: int # Max number of peers with block
@ -78,6 +79,10 @@ proc discoveryQueueLoop(b: DiscoveryEngine) {.async.} =
trace "About to sleep discovery loop" trace "About to sleep discovery loop"
await sleepAsync(b.discoveryLoopSleep) await sleepAsync(b.discoveryLoopSleep)
proc heartbeatLoop(b: DiscoveryEngine) {.async.} =
while b.discEngineRunning:
await sleepAsync(1.seconds)
proc advertiseQueueLoop*(b: DiscoveryEngine) {.async.} = proc advertiseQueueLoop*(b: DiscoveryEngine) {.async.} =
while b.discEngineRunning: while b.discEngineRunning:
if cids =? await b.localStore.listBlocks(blockType = b.advertiseType): if cids =? await b.localStore.listBlocks(blockType = b.advertiseType):
@ -206,6 +211,7 @@ proc start*(b: DiscoveryEngine) {.async.} =
b.advertiseLoop = advertiseQueueLoop(b) b.advertiseLoop = advertiseQueueLoop(b)
b.discoveryLoop = discoveryQueueLoop(b) b.discoveryLoop = discoveryQueueLoop(b)
b.heartbeatLoop = heartbeatLoop(b)
proc stop*(b: DiscoveryEngine) {.async.} = proc stop*(b: DiscoveryEngine) {.async.} =
## Stop the discovery engine ## Stop the discovery engine
@ -239,6 +245,9 @@ proc stop*(b: DiscoveryEngine) {.async.} =
await b.discoveryLoop.cancelAndWait() await b.discoveryLoop.cancelAndWait()
trace "Discovery loop stopped" trace "Discovery loop stopped"
if not b.heartbeatLoop.isNil and not b.heartbeatLoop.finished:
await b.heartbeatLoop.cancelAndWait()
trace "Discovery engine stopped" trace "Discovery engine stopped"
proc new*( proc new*(
@ -256,7 +265,7 @@ proc new*(
advertiseType = BlockType.Both advertiseType = BlockType.Both
): DiscoveryEngine = ): DiscoveryEngine =
## Create a discovery engine instance for advertising services ## Create a discovery engine instance for advertising services
## ##
DiscoveryEngine( DiscoveryEngine(
localStore: localStore, localStore: localStore,
peers: peers, peers: peers,

View File

@ -20,6 +20,7 @@ import pkg/stint
import ../../stores/blockstore import ../../stores/blockstore
import ../../blocktype as bt import ../../blocktype as bt
import ../../utils import ../../utils
import ../../loopmeasure
import ../protobuf/blockexc import ../protobuf/blockexc
import ../protobuf/presence import ../protobuf/presence
@ -64,6 +65,7 @@ type
wallet*: WalletRef # Nitro wallet for micropayments wallet*: WalletRef # Nitro wallet for micropayments
pricing*: ?Pricing # Optional bandwidth pricing pricing*: ?Pricing # Optional bandwidth pricing
discovery*: DiscoveryEngine discovery*: DiscoveryEngine
loopMeasure*: LoopMeasure
Pricing* = object Pricing* = object
address*: EthAddress address*: EthAddress
@ -491,7 +493,9 @@ proc blockexcTaskRunner(b: BlockExcEngine) {.async.} =
peerCtx = await b.taskQueue.pop() peerCtx = await b.taskQueue.pop()
trace "Got new task from queue", peerId = peerCtx.id trace "Got new task from queue", peerId = peerCtx.id
b.loopMeasure.loopArm()
await b.taskHandler(peerCtx) await b.taskHandler(peerCtx)
b.loopMeasure.loopDisarm("blockexcTaskRunner")
trace "Exiting blockexc task runner" trace "Exiting blockexc task runner"
@ -503,6 +507,7 @@ proc new*(
discovery: DiscoveryEngine, discovery: DiscoveryEngine,
peerStore: PeerCtxStore, peerStore: PeerCtxStore,
pendingBlocks: PendingBlocksManager, pendingBlocks: PendingBlocksManager,
loopMeasure: LoopMeasure,
concurrentTasks = DefaultConcurrentTasks, concurrentTasks = DefaultConcurrentTasks,
peersPerRequest = DefaultMaxPeersPerRequest peersPerRequest = DefaultMaxPeersPerRequest
): BlockExcEngine = ): BlockExcEngine =
@ -514,6 +519,7 @@ proc new*(
localStore: localStore, localStore: localStore,
peers: peerStore, peers: peerStore,
pendingBlocks: pendingBlocks, pendingBlocks: pendingBlocks,
loopMeasure: loopMeasure,
peersPerRequest: peersPerRequest, peersPerRequest: peersPerRequest,
network: network, network: network,
wallet: wallet, wallet: wallet,

View File

@ -227,7 +227,7 @@ proc new*(
peerStore = PeerCtxStore.new() peerStore = PeerCtxStore.new()
pendingBlocks = PendingBlocksManager.new() pendingBlocks = PendingBlocksManager.new()
blockDiscovery = DiscoveryEngine.new(repoStore, peerStore, network, discovery, pendingBlocks) blockDiscovery = DiscoveryEngine.new(repoStore, peerStore, network, discovery, pendingBlocks)
engine = BlockExcEngine.new(repoStore, wallet, network, blockDiscovery, peerStore, pendingBlocks) engine = BlockExcEngine.new(repoStore, wallet, network, blockDiscovery, peerStore, pendingBlocks, loopMeasure)
store = NetworkStore.new(engine, repoStore) store = NetworkStore.new(engine, repoStore)
erasure = Erasure.new(store, leoEncoderProvider, leoDecoderProvider) erasure = Erasure.new(store, leoEncoderProvider, leoDecoderProvider)
codexNode = CodexNodeRef.new(switch, store, engine, erasure, discovery) codexNode = CodexNodeRef.new(switch, store, engine, erasure, discovery)