Allow to override TCP connection timeouts (#903)

This commit is contained in:
Tanguy 2023-06-07 14:27:32 +02:00 committed by GitHub
parent c76d1e18ef
commit 32085ca88a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 3 deletions

View File

@ -40,6 +40,7 @@ type
flags: set[ServerFlags]
clientFlags: set[SocketFlags]
acceptFuts: seq[Future[StreamTransport]]
connectionsTimeout: Duration
TcpTransportTracker* = ref object of TrackerBase
opened*: uint64
@ -94,7 +95,8 @@ proc connHandler*(self: TcpTransport,
ChronosStream.init(
client = client,
dir = dir,
observedAddr = observedAddr
observedAddr = observedAddr,
timeout = self.connectionsTimeout
))
proc onClose() {.async.} =
@ -126,7 +128,8 @@ proc connHandler*(self: TcpTransport,
proc new*(
T: typedesc[TcpTransport],
flags: set[ServerFlags] = {},
upgrade: Upgrade): T {.public.} =
upgrade: Upgrade,
connectionsTimeout = 10.minutes): T {.public.} =
let
transport = T(
@ -141,7 +144,8 @@ proc new*(
else:
default(set[SocketFlags]),
upgrader: upgrade,
networkReachability: NetworkReachability.Unknown)
networkReachability: NetworkReachability.Unknown,
connectionsTimeout: connectionsTimeout)
return transport

View File

@ -174,6 +174,23 @@ suite "TCP transport":
proc transProvider(): Transport = TcpTransport.new(upgrade = Upgrade())
asyncTest "Custom timeout":
let ma = @[MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet()]
let transport: TcpTransport = TcpTransport.new(upgrade = Upgrade(), connectionsTimeout=1.milliseconds)
asyncSpawn transport.start(ma)
proc acceptHandler() {.async, gcsafe.} =
let conn = await transport.accept()
await conn.join()
let handlerWait = acceptHandler()
let streamTransport = await connect(transport.addrs[0])
await handlerWait.wait(1.seconds) # when no issues will not wait that long!
await streamTransport.closeWait()
await transport.stop()
commonTransportTest(
transProvider,
"/ip4/0.0.0.0/tcp/0")