Fix transport's switch start (#609)

* fix switch start

* don't fail eagerly on transport start

* handlecancel

* raise exc
This commit is contained in:
Tanguy Cizain 2021-07-19 12:51:27 +02:00 committed by GitHub
parent 1d4f7c7a43
commit cb94baf9c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 27 additions and 19 deletions

View File

@ -203,25 +203,6 @@ proc accept(s: Switch, transport: Transport) {.async.} = # noraises
await conn.close()
return
proc start*(s: Switch): Future[seq[Future[void]]] {.async, gcsafe.} =
trace "starting switch for peer", peerInfo = s.peerInfo
var startFuts: seq[Future[void]]
for t in s.transports: # for each transport
for i, a in s.peerInfo.addrs:
if t.handles(a): # check if it handles the multiaddr
var server = t.start(a)
s.peerInfo.addrs[i] = t.ma # update peer's address
s.acceptFuts.add(s.accept(t))
startFuts.add(server)
proc peerIdentifiedHandler(peerInfo: PeerInfo, event: PeerEvent) {.async.} =
s.peerStore.replace(peerInfo)
s.connManager.addPeerEventHandler(peerIdentifiedHandler, PeerEventKind.Identified)
debug "Started libp2p node", peer = s.peerInfo
return startFuts # listen for incoming connections
proc stop*(s: Switch) {.async.} =
trace "Stopping switch"
@ -250,6 +231,33 @@ proc stop*(s: Switch) {.async.} =
trace "Switch stopped"
proc start*(s: Switch): Future[seq[Future[void]]] {.async, gcsafe.} =
trace "starting switch for peer", peerInfo = s.peerInfo
var startFuts: seq[Future[void]]
for t in s.transports: # for each transport
for i, a in s.peerInfo.addrs:
if t.handles(a): # check if it handles the multiaddr
let transpStart = t.start(a)
startFuts.add(transpStart)
try:
await transpStart
s.peerInfo.addrs[i] = t.ma # update peer's address
s.acceptFuts.add(s.accept(t))
except CancelledError as exc:
await s.stop()
raise exc
except CatchableError as exc:
debug "Failed to start one transport", address = $a, err = exc.msg
continue
proc peerIdentifiedHandler(peerInfo: PeerInfo, event: PeerEvent) {.async.} =
s.peerStore.replace(peerInfo)
s.connManager.addPeerEventHandler(peerIdentifiedHandler, PeerEventKind.Identified)
debug "Started libp2p node", peer = s.peerInfo
return startFuts # listen for incoming connections
proc newSwitch*(peerInfo: PeerInfo,
transports: seq[Transport],
identity: Identify,