WS: add timeout to handleRequest (#796)

This commit is contained in:
Tanguy 2022-11-22 10:44:08 +01:00 committed by GitHub
parent 711609057c
commit c43aacdc81
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 10 additions and 4 deletions

View File

@ -35,14 +35,18 @@ logScope:
export transport, websock, results export transport, websock, results
const const
WsTransportTrackerName* = "libp2p.wstransport"
DefaultHeadersTimeout = 3.seconds DefaultHeadersTimeout = 3.seconds
type type
WsStream = ref object of Connection WsStream = ref object of Connection
session: WSSession session: WSSession
method initStream*(s: WsStream) =
if s.objName.len == 0:
s.objName = "WsStream"
procCall Connection(s).initStream()
proc new*(T: type WsStream, proc new*(T: type WsStream,
session: WSSession, session: WSSession,
dir: Direction, dir: Direction,
@ -129,7 +133,7 @@ method start*(
factories = self.factories, factories = self.factories,
rng = self.rng) rng = self.rng)
for i, ma in addrs: for i, ma in addrs:
let isWss = let isWss =
if WSS.match(ma): if WSS.match(ma):
@ -256,7 +260,7 @@ method accept*(self: WsTransport): Future[Connection] {.async, gcsafe.} =
try: try:
let let
wstransp = await self.wsserver.handleRequest(req) wstransp = await self.wsserver.handleRequest(req).wait(self.handshakeTimeout)
isSecure = self.httpservers[index].secure isSecure = self.httpservers[index].secure
return await self.connHandler(wstransp, isSecure, Direction.In) return await self.connHandler(wstransp, isSecure, Direction.In)
@ -273,6 +277,8 @@ method accept*(self: WsTransport): Future[Connection] {.async, gcsafe.} =
debug "AsyncStream Error", exc = exc.msg debug "AsyncStream Error", exc = exc.msg
except TransportTooManyError as exc: except TransportTooManyError as exc:
debug "Too many files opened", exc = exc.msg debug "Too many files opened", exc = exc.msg
except AsyncTimeoutError as exc:
debug "Timed out", exc = exc.msg
except TransportUseClosedError as exc: except TransportUseClosedError as exc:
debug "Server was closed", exc = exc.msg debug "Server was closed", exc = exc.msg
raise newTransportClosedError(exc) raise newTransportClosedError(exc)