Skip to content

Commit

Permalink
Use ReusePort when starting
Browse files Browse the repository at this point in the history
  • Loading branch information
diegomrsantos committed Mar 8, 2023
1 parent e8f1c69 commit fe05760
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
16 changes: 14 additions & 2 deletions libp2p/transports/tcptransport.nim
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ type
opened*: uint64
closed*: uint64

TcpTransportError* = object of transport.TransportError

proc setupTcpTransportTracker(): TcpTransportTracker {.gcsafe, raises: [Defect].}

proc getTcpTransportTracker(): TcpTransportTracker {.gcsafe.} =
Expand Down Expand Up @@ -157,6 +159,17 @@ method start*(
warn "TCP transport already running"
return

proc getPort(ma: MultiAddress): seq[byte] =
return ma[1].get().protoArgument().get()

proc isNotZeroPort(port: seq[byte]): bool =
return port != @[0.byte, 0]

let supported = addrs.filterIt(self.handles(it))
let nonZeroPorts = supported.mapIt(getPort(it)).filterIt(isNotZeroPort(it))
if deduplicate(nonZeroPorts).len < nonZeroPorts.len:
raise newException(TcpTransportError, "Duplicate ports detected")

await procCall Transport(self).start(addrs)
trace "Starting TCP transport"
inc getTcpTransportTracker().opened
Expand All @@ -166,8 +179,7 @@ method start*(
trace "Invalid address detected, skipping!", address = ma
continue

if self.networkReachability == NetworkReachability.NotReachable:
self.flags.incl(ServerFlags.ReusePort)
self.flags.incl(ServerFlags.ReusePort)
let server = createStreamServer(
ma = ma,
flags = self.flags,
Expand Down
19 changes: 19 additions & 0 deletions tests/testtcptransport.nim
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,25 @@ suite "TCP transport":
server.close()
await server.join()

asyncTest "Starting with duplicate ports must fail":
# Starting with duplicate addresses must fail
let ma = @[MultiAddress.init("/ip4/0.0.0.0/tcp/8080").tryGet(),
MultiAddress.init("/ip4/0.0.0.0/tcp/8080").tryGet()]

let transport: TcpTransport = TcpTransport.new(upgrade = Upgrade())

expect TcpTransportError:
await transport.start(ma)

asyncTest "Starting with duplicate but zero ports addresses must NOT fail":
let ma = @[MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet(),
MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet()]

let transport: TcpTransport = TcpTransport.new(upgrade = Upgrade())

await transport.start(ma)
await transport.stop()

proc transProvider(): Transport = TcpTransport.new(upgrade = Upgrade())

commonTransportTest(
Expand Down

0 comments on commit fe05760

Please sign in to comment.