rename addOrAwait to getWantHandle (#97)
This commit is contained in:
parent
46197f957b
commit
cf062670f6
|
@ -8,6 +8,7 @@
|
||||||
## those terms.
|
## those terms.
|
||||||
|
|
||||||
import std/tables
|
import std/tables
|
||||||
|
import std/sequtils
|
||||||
|
|
||||||
import pkg/questionable
|
import pkg/questionable
|
||||||
import pkg/chronicles
|
import pkg/chronicles
|
||||||
|
@ -19,18 +20,22 @@ import ../blocktype
|
||||||
logScope:
|
logScope:
|
||||||
topics = "dagger blockexc pendingblocks"
|
topics = "dagger blockexc pendingblocks"
|
||||||
|
|
||||||
|
const
|
||||||
|
DefaultBlockTimeout* = 10.minutes
|
||||||
|
|
||||||
type
|
type
|
||||||
PendingBlocksManager* = ref object of RootObj
|
PendingBlocksManager* = ref object of RootObj
|
||||||
blocks*: Table[Cid, Future[Block]] # pending Block requests
|
blocks*: Table[Cid, Future[Block]] # pending Block requests
|
||||||
|
|
||||||
proc addOrAwait*(
|
proc getWantHandle*(
|
||||||
p: PendingBlocksManager,
|
p: PendingBlocksManager,
|
||||||
cid: Cid): Future[Block] {.async.} =
|
cid: Cid,
|
||||||
|
timeout = DefaultBlockTimeout): Future[Block] {.async.} =
|
||||||
## Add an event for a block
|
## Add an event for a block
|
||||||
##
|
##
|
||||||
|
|
||||||
if cid notin p.blocks:
|
if cid notin p.blocks:
|
||||||
p.blocks[cid] = newFuture[Block]()
|
p.blocks[cid] = newFuture[Block]().wait(timeout)
|
||||||
trace "Adding pending future for block", cid
|
trace "Adding pending future for block", cid
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -43,6 +48,12 @@ proc addOrAwait*(
|
||||||
finally:
|
finally:
|
||||||
p.blocks.del(cid)
|
p.blocks.del(cid)
|
||||||
|
|
||||||
|
proc addOrAwait*(
|
||||||
|
p: PendingBlocksManager,
|
||||||
|
cid: Cid,
|
||||||
|
timeout = DefaultBlockTimeout): Future[Block] {.deprecated: "Use getWantHandle".} =
|
||||||
|
p.getWantHandle(cid, timeout)
|
||||||
|
|
||||||
proc resolve*(
|
proc resolve*(
|
||||||
p: PendingBlocksManager,
|
p: PendingBlocksManager,
|
||||||
blocks: seq[Block]) =
|
blocks: seq[Block]) =
|
||||||
|
@ -66,6 +77,17 @@ proc contains*(
|
||||||
p: PendingBlocksManager,
|
p: PendingBlocksManager,
|
||||||
cid: Cid): bool = p.pending(cid)
|
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 =
|
func new*(T: type PendingBlocksManager): T =
|
||||||
T(
|
T(
|
||||||
blocks: initTable[Cid, Future[Block]]()
|
blocks: initTable[Cid, Future[Block]]()
|
||||||
|
|
Loading…
Reference in New Issue