Allow to override TCP connection timeouts (#903)
This commit is contained in:
parent
c76d1e18ef
commit
32085ca88a
|
@ -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
|
||||
|
||||
|
|
|
@ -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")
|
||||
|
|
Loading…
Reference in New Issue