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

View File

@ -174,6 +174,23 @@ suite "TCP transport":
proc transProvider(): Transport = TcpTransport.new(upgrade = Upgrade()) 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( commonTransportTest(
transProvider, transProvider,
"/ip4/0.0.0.0/tcp/0") "/ip4/0.0.0.0/tcp/0")