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 stew;https://github.com/status-im/nim-stew@#bb705bf17b46d2c8f9bfb106d9cc7437009a2501
testutils;https://github.com/status-im/nim-testutils@#aa6e5216f4b4ab5aa971cdcdd70e1ec1203cedf2 testutils;https://github.com/status-im/nim-testutils@#aa6e5216f4b4ab5aa971cdcdd70e1ec1203cedf2
unittest2;https://github.com/status-im/nim-unittest2@#4e2893eacb916c7678fdc4935ff7420f13bf3a9c 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 zlib;https://github.com/status-im/nim-zlib@#74cdeb54b21bededb5a515d36f608bc1850555a2

View File

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