@@ -123,7 +123,9 @@ proc removeConnEventHandler*(
123
123
) =
124
124
c.connEvents[kind].excl (handler)
125
125
126
- proc triggerConnEvent * (c: ConnManager , peerId: PeerId , event: ConnEvent ) {.async .} =
126
+ proc triggerConnEvent * (
127
+ c: ConnManager , peerId: PeerId , event: ConnEvent
128
+ ) {.async : (raises: [CancelledError ]).} =
127
129
try :
128
130
trace " About to trigger connection events" , peer = peerId
129
131
if c.connEvents[event.kind].len () > 0 :
@@ -154,7 +156,9 @@ proc removePeerEventHandler*(
154
156
) =
155
157
c.peerEvents[kind].excl (handler)
156
158
157
- proc triggerPeerEvents * (c: ConnManager , peerId: PeerId , event: PeerEvent ) {.async .} =
159
+ proc triggerPeerEvents * (
160
+ c: ConnManager , peerId: PeerId , event: PeerEvent
161
+ ) {.async : (raises: [CancelledError ]).} =
158
162
trace " About to trigger peer events" , peer = peerId
159
163
if c.peerEvents[event.kind].len == 0 :
160
164
return
@@ -174,7 +178,7 @@ proc triggerPeerEvents*(c: ConnManager, peerId: PeerId, event: PeerEvent) {.asyn
174
178
175
179
proc expectConnection * (
176
180
c: ConnManager , p: PeerId , dir: Direction
177
- ): Future [Muxer ] {.async .} =
181
+ ): Future [Muxer ] {.async : (raises: [ AlreadyExpectingConnectionError , CancelledError ]) .} =
178
182
# # Wait for a peer to connect to us. This will bypass the `MaxConnectionsPerPeer`
179
183
let key = (p, dir)
180
184
if key in c.expectedConnectionsOverLimit:
@@ -183,7 +187,7 @@ proc expectConnection*(
183
187
" Already expecting an incoming connection from that peer" ,
184
188
)
185
189
186
- let future = newFuture [Muxer ]()
190
+ let future = Future [Muxer ]. Raising ([ CancelledError ]). init ()
187
191
c.expectedConnectionsOverLimit[key] = future
188
192
189
193
try :
@@ -205,18 +209,18 @@ proc contains*(c: ConnManager, muxer: Muxer): bool =
205
209
let conn = muxer.connection
206
210
return muxer in c.muxed.getOrDefault (conn.peerId)
207
211
208
- proc closeMuxer (muxer: Muxer ) {.async .} =
212
+ proc closeMuxer (muxer: Muxer ) {.async : (raises: [ CancelledError ]) .} =
209
213
trace " Cleaning up muxer" , m = muxer
210
214
211
215
await muxer.close ()
212
216
if not (isNil (muxer.handler)):
213
217
try :
214
- await muxer.handler # TODO noraises?
218
+ await muxer.handler
215
219
except CatchableError as exc:
216
220
trace " Exception in close muxer handler" , description = exc.msg
217
221
trace " Cleaned up muxer" , m = muxer
218
222
219
- proc muxCleanup (c: ConnManager , mux: Muxer ) {.async .} =
223
+ proc muxCleanup (c: ConnManager , mux: Muxer ) {.async : (raises: []) .} =
220
224
try :
221
225
trace " Triggering disconnect events" , mux
222
226
let peerId = mux.connection.peerId
@@ -238,7 +242,7 @@ proc muxCleanup(c: ConnManager, mux: Muxer) {.async.} =
238
242
# do not need to propagate CancelledError and should handle other errors
239
243
warn " Unexpected exception peer cleanup handler" , mux, description = exc.msg
240
244
241
- proc onClose (c: ConnManager , mux: Muxer ) {.async .} =
245
+ proc onClose (c: ConnManager , mux: Muxer ) {.async : (raises: []) .} =
242
246
# # connection close even handler
243
247
# #
244
248
# # triggers the connections resource cleanup
@@ -324,7 +328,9 @@ proc storeMuxer*(c: ConnManager, muxer: Muxer) {.raises: [CatchableError].} =
324
328
325
329
trace " Stored muxer" , muxer, direction = $ muxer.connection.dir, peers = c.muxed.len
326
330
327
- proc getIncomingSlot * (c: ConnManager ): Future [ConnectionSlot ] {.async .} =
331
+ proc getIncomingSlot * (
332
+ c: ConnManager
333
+ ): Future [ConnectionSlot ] {.async : (raises: [CancelledError ]).} =
328
334
await c.inSema.acquire ()
329
335
return ConnectionSlot (connManager: c, direction: In )
330
336
@@ -339,25 +345,21 @@ proc getOutgoingSlot*(
339
345
raise newTooManyConnectionsError ()
340
346
return ConnectionSlot (connManager: c, direction: Out )
341
347
348
+ func semaphore (c: ConnManager , dir: Direction ): AsyncSemaphore {.inline .} =
349
+ return if dir == In : c.inSema else : c.outSema
350
+
342
351
proc slotsAvailable * (c: ConnManager , dir: Direction ): int =
343
- case dir
344
- of Direction .In :
345
- return c.inSema.count
346
- of Direction .Out :
347
- return c.outSema.count
352
+ return semaphore (c, dir).count
348
353
349
354
proc release * (cs: ConnectionSlot ) =
350
- if cs.direction == In :
351
- cs.connManager.inSema.release ()
352
- else :
353
- cs.connManager.outSema.release ()
355
+ semaphore (cs.connManager, cs.direction).release ()
354
356
355
357
proc trackConnection * (cs: ConnectionSlot , conn: Connection ) =
356
358
if isNil (conn):
357
359
cs.release ()
358
360
return
359
361
360
- proc semaphoreMonitor () {.async .} =
362
+ proc semaphoreMonitor () {.async : (raises: [ CancelledError ]) .} =
361
363
try :
362
364
await conn.join ()
363
365
except CatchableError as exc:
@@ -373,28 +375,32 @@ proc trackMuxer*(cs: ConnectionSlot, mux: Muxer) =
373
375
return
374
376
cs.trackConnection (mux.connection)
375
377
376
- proc getStream * (c: ConnManager , muxer: Muxer ): Future [Connection ] {.async .} =
378
+ proc getStream * (
379
+ c: ConnManager , muxer: Muxer
380
+ ): Future [Connection ] {.async : (raises: [LPStreamError , MuxerError , CancelledError ]).} =
377
381
# # get a muxed stream for the passed muxer
378
382
# #
379
383
380
384
if not (isNil (muxer)):
381
385
return await muxer.newStream ()
382
386
383
- proc getStream * (c: ConnManager , peerId: PeerId ): Future [Connection ] {.async .} =
387
+ proc getStream * (
388
+ c: ConnManager , peerId: PeerId
389
+ ): Future [Connection ] {.async : (raises: [LPStreamError , MuxerError , CancelledError ]).} =
384
390
# # get a muxed stream for the passed peer from any connection
385
391
# #
386
392
387
393
return await c.getStream (c.selectMuxer (peerId))
388
394
389
395
proc getStream * (
390
396
c: ConnManager , peerId: PeerId , dir: Direction
391
- ): Future [Connection ] {.async .} =
397
+ ): Future [Connection ] {.async : (raises: [ LPStreamError , MuxerError , CancelledError ]) .} =
392
398
# # get a muxed stream for the passed peer from a connection with `dir`
393
399
# #
394
400
395
401
return await c.getStream (c.selectMuxer (peerId, dir))
396
402
397
- proc dropPeer * (c: ConnManager , peerId: PeerId ) {.async .} =
403
+ proc dropPeer * (c: ConnManager , peerId: PeerId ) {.async : (raises: [ CancelledError ]) .} =
398
404
# # drop connections and cleanup resources for peer
399
405
# #
400
406
trace " Dropping peer" , peerId
@@ -405,7 +411,7 @@ proc dropPeer*(c: ConnManager, peerId: PeerId) {.async.} =
405
411
406
412
trace " Peer dropped" , peerId
407
413
408
- proc close * (c: ConnManager ) {.async .} =
414
+ proc close * (c: ConnManager ) {.async : (raises: [ CancelledError ]) .} =
409
415
# # cleanup resources for the connection
410
416
# # manager
411
417
# #
0 commit comments