27
27
protocols/ secure/ secure,
28
28
peerinfo,
29
29
utils/ semaphore,
30
+ ./ muxers/ muxer,
30
31
connmanager,
31
32
nameresolving/ nameresolver,
32
33
peerid,
61
62
started: bool
62
63
services* : seq [Service ]
63
64
65
+ UpgradeError * = object of LPError
66
+
64
67
Service * = ref object of RootObj
65
68
inUse: bool
66
69
@@ -216,30 +219,44 @@ proc mount*[T: LPProtocol](
216
219
s.ms.addHandler (proto.codecs, proto, matcher)
217
220
s.peerInfo.protocols.add (proto.codec)
218
221
219
- proc upgrader (switch: Switch , trans: Transport , conn: Connection ) {.async .} =
220
- let muxed = await trans.upgrade (conn, Opt .none (PeerId ))
221
- switch.connManager.storeMuxer (muxed)
222
- await switch.peerStore.identify (muxed)
223
- await switch.connManager.triggerPeerEvents (
224
- muxed.connection.peerId, PeerEvent (kind: PeerEventKind .Identified , initiator: false )
225
- )
226
- trace " Connection upgrade succeeded"
222
+ proc upgrader (
223
+ switch: Switch , trans: Transport , conn: Connection
224
+ ) {.async : (raises: [CancelledError , UpgradeError ]).} =
225
+ try :
226
+ let muxed = await trans.upgrade (conn, Opt .none (PeerId ))
227
+ switch.connManager.storeMuxer (muxed)
228
+ await switch.peerStore.identify (muxed)
229
+ await switch.connManager.triggerPeerEvents (
230
+ muxed.connection.peerId,
231
+ PeerEvent (kind: PeerEventKind .Identified , initiator: false ),
232
+ )
233
+ except CancelledError as e:
234
+ raise e
235
+ except CatchableError as e:
236
+ raise newException (UpgradeError , e.msg, e)
227
237
228
238
proc upgradeMonitor (
229
239
switch: Switch , trans: Transport , conn: Connection , upgrades: AsyncSemaphore
230
- ) {.async .} =
240
+ ) {.async : (raises: []).} =
241
+ var upgradeSuccessful = false
231
242
try :
232
243
await switch.upgrader (trans, conn).wait (30 .seconds)
233
- except CatchableError as exc:
234
- if exc isnot CancelledError :
235
- libp2p_failed_upgrades_incoming.inc ()
236
- if not isNil (conn):
237
- await conn.close ()
238
- trace " Exception awaiting connection upgrade" , description = exc.msg, conn
244
+ trace " Connection upgrade succeeded"
245
+ upgradeSuccessful = true
246
+ except CancelledError :
247
+ trace " Connection upgrade cancelled" , conn
248
+ except AsyncTimeoutError :
249
+ trace " Connection upgrade timeout" , conn
250
+ libp2p_failed_upgrades_incoming.inc ()
251
+ except UpgradeError as e:
252
+ trace " Connection upgrade failed" , description = e.msg, conn
253
+ libp2p_failed_upgrades_incoming.inc ()
239
254
finally :
255
+ if (not upgradeSuccessful) and (not isNil (conn)):
256
+ await conn.close ()
240
257
upgrades.release ()
241
258
242
- proc accept (s: Switch , transport: Transport ) {.async .} = # noraises
259
+ proc accept (s: Switch , transport: Transport ) {.async : (raises: []) .} =
243
260
# # switch accept loop, ran for every transport
244
261
# #
245
262
@@ -286,7 +303,7 @@ proc accept(s: Switch, transport: Transport) {.async.} = # noraises
286
303
await conn.close ()
287
304
return
288
305
289
- proc stop * (s: Switch ) {.async , public .} =
306
+ proc stop * (s: Switch ) {.public , async : (raises: [ CancelledError ]) .} =
290
307
# # Stop listening on every transport, and
291
308
# # close every active connections
292
309
@@ -318,7 +335,7 @@ proc stop*(s: Switch) {.async, public.} =
318
335
319
336
trace " Switch stopped"
320
337
321
- proc start * (s: Switch ) {.async , public .} =
338
+ proc start * (s: Switch ) {.public , async : (raises: [ CancelledError , LPError ]) .} =
322
339
# # Start listening on every transport
323
340
324
341
if s.started:
@@ -340,7 +357,7 @@ proc start*(s: Switch) {.async, public.} =
340
357
for fut in startFuts:
341
358
if fut.failed:
342
359
await s.stop ()
343
- raise fut.error
360
+ raise newException ( LPError , " starting transports failed " , fut.error)
344
361
345
362
for t in s.transports: # for each transport
346
363
if t.addrs.len > 0 or t.running:
0 commit comments