WS Accept timeout (#699)

* Add timeout to WS accept
* Handle more WS errors
This commit is contained in:
Tanguy 2022-03-17 10:16:48 +01:00 committed by GitHub
parent c7504d2446
commit eaa72dcdbe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 20 deletions

View File

@ -13,5 +13,5 @@ serialization;https://github.com/status-im/nim-serialization@#37bc0db558d8571196
stew;https://github.com/status-im/nim-stew@#bb705bf17b46d2c8f9bfb106d9cc7437009a2501
testutils;https://github.com/status-im/nim-testutils@#aa6e5216f4b4ab5aa971cdcdd70e1ec1203cedf2
unittest2;https://github.com/status-im/nim-unittest2@#4e2893eacb916c7678fdc4935ff7420f13bf3a9c
websock;https://github.com/status-im/nim-websock@#73edde4417f7b45003113b7a34212c3ccd95b9fd
websock;https://github.com/status-im/nim-websock@#853299e399746eff4096870067cbc61861ecd534
zlib;https://github.com/status-im/nim-zlib@#74cdeb54b21bededb5a515d36f608bc1850555a2

View File

@ -30,6 +30,8 @@ export transport, websock
const
WsTransportTrackerName* = "libp2p.wstransport"
DefaultHeadersTimeout = 3.seconds
type
WsStream = ref object of Connection
session: WSSession
@ -92,6 +94,7 @@ type
tlsCertificate: TLSCertificate
tlsFlags: set[TLSFlags]
flags: set[ServerFlags]
handshakeTimeout: Duration
factories: seq[ExtFactory]
rng: Rng
@ -131,9 +134,13 @@ method start*(
address = ma.initTAddress().tryGet(),
tlsPrivateKey = self.tlsPrivateKey,
tlsCertificate = self.tlsCertificate,
flags = self.flags)
flags = self.flags,
handshakeTimeout = self.handshakeTimeout)
else:
HttpServer.create(ma.initTAddress().tryGet())
HttpServer.create(
ma.initTAddress().tryGet(),
handshakeTimeout = self.handshakeTimeout
)
self.httpservers &= httpserver
@ -222,19 +229,19 @@ method accept*(self: WsTransport): Future[Connection] {.async, gcsafe.} =
if not self.running:
raise newTransportClosedError()
if self.acceptFuts.len <= 0:
self.acceptFuts = self.httpservers.mapIt(it.accept())
if self.acceptFuts.len <= 0:
return
let
finished = await one(self.acceptFuts)
index = self.acceptFuts.find(finished)
self.acceptFuts[index] = self.httpservers[index].accept()
try:
if self.acceptFuts.len <= 0:
self.acceptFuts = self.httpservers.mapIt(it.accept())
if self.acceptFuts.len <= 0:
return
let
finished = await one(self.acceptFuts)
index = self.acceptFuts.find(finished)
self.acceptFuts[index] = self.httpservers[index].accept()
let req = await finished
try:
@ -250,6 +257,8 @@ method accept*(self: WsTransport): Future[Connection] {.async, gcsafe.} =
debug "OS Error", exc = exc.msg
except WebSocketError as exc:
debug "Websocket Error", exc = exc.msg
except HttpError as exc:
debug "Http Error", exc = exc.msg
except AsyncStreamError as exc:
debug "AsyncStream Error", exc = exc.msg
except TransportTooManyError as exc:
@ -301,7 +310,8 @@ proc new*(
tlsFlags: set[TLSFlags] = {},
flags: set[ServerFlags] = {},
factories: openArray[ExtFactory] = [],
rng: Rng = nil): T =
rng: Rng = nil,
handshakeTimeout = DefaultHeadersTimeout): T =
T(
upgrader: upgrade,
@ -310,14 +320,16 @@ proc new*(
tlsFlags: tlsFlags,
flags: flags,
factories: @factories,
rng: rng)
rng: rng,
handshakeTimeout: handshakeTimeout)
proc new*(
T: typedesc[WsTransport],
upgrade: Upgrade,
flags: set[ServerFlags] = {},
factories: openArray[ExtFactory] = [],
rng: Rng = nil): T =
rng: Rng = nil,
handshakeTimeout = DefaultHeadersTimeout): T =
T.new(
upgrade = upgrade,
@ -325,4 +337,5 @@ proc new*(
tlsCertificate = nil,
flags = flags,
factories = @factories,
rng = rng)
rng = rng,
handshakeTimeout = handshakeTimeout)