Skip to content

Commit c43aacd

Browse files
authored
WS: add timeout to handleRequest (#796)
1 parent 7116090 commit c43aacd

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

libp2p/transports/wstransport.nim

+10-4
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,18 @@ logScope:
3535
export transport, websock, results
3636

3737
const
38-
WsTransportTrackerName* = "libp2p.wstransport"
39-
4038
DefaultHeadersTimeout = 3.seconds
4139

4240
type
4341
WsStream = ref object of Connection
4442
session: WSSession
4543

44+
method initStream*(s: WsStream) =
45+
if s.objName.len == 0:
46+
s.objName = "WsStream"
47+
48+
procCall Connection(s).initStream()
49+
4650
proc new*(T: type WsStream,
4751
session: WSSession,
4852
dir: Direction,
@@ -129,7 +133,7 @@ method start*(
129133
factories = self.factories,
130134
rng = self.rng)
131135

132-
136+
133137
for i, ma in addrs:
134138
let isWss =
135139
if WSS.match(ma):
@@ -256,7 +260,7 @@ method accept*(self: WsTransport): Future[Connection] {.async, gcsafe.} =
256260

257261
try:
258262
let
259-
wstransp = await self.wsserver.handleRequest(req)
263+
wstransp = await self.wsserver.handleRequest(req).wait(self.handshakeTimeout)
260264
isSecure = self.httpservers[index].secure
261265

262266
return await self.connHandler(wstransp, isSecure, Direction.In)
@@ -273,6 +277,8 @@ method accept*(self: WsTransport): Future[Connection] {.async, gcsafe.} =
273277
debug "AsyncStream Error", exc = exc.msg
274278
except TransportTooManyError as exc:
275279
debug "Too many files opened", exc = exc.msg
280+
except AsyncTimeoutError as exc:
281+
debug "Timed out", exc = exc.msg
276282
except TransportUseClosedError as exc:
277283
debug "Server was closed", exc = exc.msg
278284
raise newTransportClosedError(exc)

0 commit comments

Comments
 (0)