Skip to content

Commit 03f67d3

Browse files
authored
add support for setting protocol handlers with {.raises.} annotation (#1064)
1 parent bb97a9d commit 03f67d3

File tree

5 files changed

+57
-20
lines changed

5 files changed

+57
-20
lines changed

examples/helloworld.nim

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ proc new(T: typedesc[TestProto]): T =
2020
# We must close the connections ourselves when we're done with it
2121
await conn.close()
2222

23-
return T(codecs: @[TestCodec], handler: handle)
23+
return T.new(codecs = @[TestCodec], handler = handle)
2424

2525
##
2626
# Helper to create a switch/node

libp2p/multistream.nim

+8-4
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,14 @@ proc addHandler*(m: MultistreamSelect,
246246
matcher: Matcher = nil) =
247247
addHandler(m, @[codec], protocol, matcher)
248248

249-
proc addHandler*(m: MultistreamSelect,
250-
codec: string,
251-
handler: LPProtoHandler,
252-
matcher: Matcher = nil) =
249+
proc addHandler*[E](
250+
m: MultistreamSelect,
251+
codec: string,
252+
handler: LPProtoHandler |
253+
proc (
254+
conn: Connection,
255+
proto: string): InternalRaisesFuture[void, E],
256+
matcher: Matcher = nil) =
253257
## helper to allow registering pure handlers
254258
trace "registering proto handler", proto = codec
255259
let protocol = new LPProtocol

libp2p/protocols/protocol.nim

+45-11
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,12 @@ const
1919

2020
type
2121
LPProtoHandler* = proc (
22-
conn: Connection,
23-
proto: string):
24-
Future[void]
25-
{.gcsafe, raises: [].}
22+
conn: Connection,
23+
proto: string): Future[void] {.async.}
2624

2725
LPProtocol* = ref object of RootObj
2826
codecs*: seq[string]
29-
handler*: LPProtoHandler ## this handler gets invoked by the protocol negotiator
27+
handlerImpl: LPProtoHandler ## invoked by the protocol negotiator
3028
started*: bool
3129
maxIncomingStreams: Opt[int]
3230

@@ -52,23 +50,59 @@ proc `maxIncomingStreams=`*(p: LPProtocol, val: int) =
5250
p.maxIncomingStreams = Opt.some(val)
5351

5452
func codec*(p: LPProtocol): string =
55-
assert(p.codecs.len > 0, "Codecs sequence was empty!")
53+
doAssert(p.codecs.len > 0, "Codecs sequence was empty!")
5654
p.codecs[0]
5755

5856
func `codec=`*(p: LPProtocol, codec: string) =
5957
# always insert as first codec
6058
# if we use this abstraction
6159
p.codecs.insert(codec, 0)
6260

61+
template `handler`*(p: LPProtocol): LPProtoHandler =
62+
p.handlerImpl
63+
64+
template `handler`*(
65+
p: LPProtocol, conn: Connection, proto: string): Future[void] =
66+
p.handlerImpl(conn, proto)
67+
68+
func `handler=`*(p: LPProtocol, handler: LPProtoHandler) =
69+
p.handlerImpl = handler
70+
71+
# Callbacks that are annotated with `{.async: (raises).}` explicitly
72+
# document the types of errors that they may raise, but are not compatible
73+
# with `LPProtoHandler` and need to use a custom `proc` type.
74+
# They are internally wrapped into a `LPProtoHandler`, but still allow the
75+
# compiler to check that their `{.async: (raises).}` annotation is correct.
76+
# https://github.com/nim-lang/Nim/issues/23432
77+
func `handler=`*[E](
78+
p: LPProtocol,
79+
handler: proc (
80+
conn: Connection,
81+
proto: string): InternalRaisesFuture[void, E]) =
82+
proc wrap(conn: Connection, proto: string): Future[void] {.async.} =
83+
await handler(conn, proto)
84+
p.handlerImpl = wrap
85+
6386
proc new*(
64-
T: type LPProtocol,
65-
codecs: seq[string],
66-
handler: LPProtoHandler,
67-
maxIncomingStreams: Opt[int] | int = Opt.none(int)): T =
87+
T: type LPProtocol,
88+
codecs: seq[string],
89+
handler: LPProtoHandler,
90+
maxIncomingStreams: Opt[int] | int = Opt.none(int)): T =
6891
T(
6992
codecs: codecs,
70-
handler: handler,
93+
handlerImpl: handler,
7194
maxIncomingStreams:
7295
when maxIncomingStreams is int: Opt.some(maxIncomingStreams)
7396
else: maxIncomingStreams
7497
)
98+
99+
proc new*[E](
100+
T: type LPProtocol,
101+
codecs: seq[string],
102+
handler: proc (
103+
conn: Connection,
104+
proto: string): InternalRaisesFuture[void, E],
105+
maxIncomingStreams: Opt[int] | int = Opt.none(int)): T =
106+
proc wrap(conn: Connection, proto: string): Future[void] {.async.} =
107+
await handler(conn, proto)
108+
T.new(codec, wrap, maxIncomingStreams)

tests/errorhelpers.nim

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ proc allFuturesThrowing*(args: varargs[FutureBase]): Future[void] =
2424
proc allFuturesThrowing*[T](futs: varargs[Future[T]]): Future[void] =
2525
allFuturesThrowing(futs.mapIt(FutureBase(it)))
2626

27-
proc allFuturesThrowing*[T, E](
27+
proc allFuturesThrowing*[T, E]( # https://github.com/nim-lang/Nim/issues/23432
2828
futs: varargs[InternalRaisesFuture[T, E]]): Future[void] =
2929
allFuturesThrowing(futs.mapIt(FutureBase(it)))

tests/testtortransport.nim

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{.used.}
22

33
# Nim-Libp2p
4-
# Copyright (c) 2023 Status Research & Development GmbH
4+
# Copyright (c) 2023-2024 Status Research & Development GmbH
55
# Licensed under either of
66
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
77
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
@@ -88,7 +88,6 @@ suite "Tor transport":
8888

8989
# every incoming connections will be in handled in this closure
9090
proc handle(conn: Connection, proto: string) {.async.} =
91-
9291
var resp: array[6, byte]
9392
await conn.readExactly(addr resp, 6)
9493
check string.fromBytes(resp) == "client"
@@ -97,7 +96,7 @@ suite "Tor transport":
9796
# We must close the connections ourselves when we're done with it
9897
await conn.close()
9998

100-
return T(codecs: @[TestCodec], handler: handle)
99+
return T.new(codecs = @[TestCodec], handler = handle)
101100

102101
let rng = newRng()
103102

0 commit comments

Comments
 (0)