Retry bootstrapping in case of failure

This commit is contained in:
Yuriy Glukhov 2019-04-04 15:35:30 +03:00 committed by zah
parent ac766bf7b3
commit b84c228364
1 changed files with 15 additions and 5 deletions

View File

@ -416,11 +416,21 @@ proc resolve*(k: KademliaProtocol, id: NodeId): Future[Node] {.async.} =
for n in closest:
if n.id == id: return n
proc bootstrap*(k: KademliaProtocol, bootstrapNodes: seq[Node]) {.async.} =
let bonded = await all(bootstrapNodes.mapIt(k.bond(it)))
if true notin bonded:
info "Failed to bond with bootstrap nodes "
return
proc bootstrap*(k: KademliaProtocol, bootstrapNodes: seq[Node], retries = 0) {.async.} =
## Bond with bootstrap nodes and do initial lookup. Retry `retries` times
## in case of failure, or indefinitely if `retries` is 0.
var numTries = 0
while true:
let bonded = await all(bootstrapNodes.mapIt(k.bond(it)))
if true notin bonded:
info "Failed to bond with bootstrap nodes"
inc numTries
if retries == 0 or numTries < retries:
info "Retrying"
else:
return
else:
break
discard await k.lookupRandom()
proc recvPong*(k: KademliaProtocol, n: Node, token: seq[byte]) =