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: for n in closest:
if n.id == id: return n if n.id == id: return n
proc bootstrap*(k: KademliaProtocol, bootstrapNodes: seq[Node]) {.async.} = proc bootstrap*(k: KademliaProtocol, bootstrapNodes: seq[Node], retries = 0) {.async.} =
let bonded = await all(bootstrapNodes.mapIt(k.bond(it))) ## Bond with bootstrap nodes and do initial lookup. Retry `retries` times
if true notin bonded: ## in case of failure, or indefinitely if `retries` is 0.
info "Failed to bond with bootstrap nodes " var numTries = 0
return 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() discard await k.lookupRandom()
proc recvPong*(k: KademliaProtocol, n: Node, token: seq[byte]) = proc recvPong*(k: KademliaProtocol, n: Node, token: seq[byte]) =