rename addOrAwait to getWantHandle (#97)

This commit is contained in:
Dmitriy Ryajov 2022-05-12 14:09:40 -06:00 committed by GitHub
parent 46197f957b
commit cf062670f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 25 additions and 3 deletions

View File

@ -8,6 +8,7 @@
## those terms.
import std/tables
import std/sequtils
import pkg/questionable
import pkg/chronicles
@ -19,18 +20,22 @@ import ../blocktype
logScope:
topics = "dagger blockexc pendingblocks"
const
DefaultBlockTimeout* = 10.minutes
type
PendingBlocksManager* = ref object of RootObj
blocks*: Table[Cid, Future[Block]] # pending Block requests
proc addOrAwait*(
proc getWantHandle*(
p: PendingBlocksManager,
cid: Cid): Future[Block] {.async.} =
cid: Cid,
timeout = DefaultBlockTimeout): Future[Block] {.async.} =
## Add an event for a block
##
if cid notin p.blocks:
p.blocks[cid] = newFuture[Block]()
p.blocks[cid] = newFuture[Block]().wait(timeout)
trace "Adding pending future for block", cid
try:
@ -43,6 +48,12 @@ proc addOrAwait*(
finally:
p.blocks.del(cid)
proc addOrAwait*(
p: PendingBlocksManager,
cid: Cid,
timeout = DefaultBlockTimeout): Future[Block] {.deprecated: "Use getWantHandle".} =
p.getWantHandle(cid, timeout)
proc resolve*(
p: PendingBlocksManager,
blocks: seq[Block]) =
@ -66,6 +77,17 @@ proc contains*(
p: PendingBlocksManager,
cid: Cid): bool = p.pending(cid)
iterator wantList*(p: PendingBlocksManager): Cid =
for k in p.blocks.keys:
yield k
iterator wantHandles*(p: PendingBlocksManager): Future[Block] =
for v in p.blocks.values:
yield v
func len*(p: PendingBlocksManager): int =
p.blocks.len
func new*(T: type PendingBlocksManager): T =
T(
blocks: initTable[Cid, Future[Block]]()