don't quit accept prematurelly (#443)

This commit is contained in:
Dmitriy Ryajov 2020-11-19 09:10:25 -06:00 committed by GitHub
parent 93b6c4dc52
commit c42009d56e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 16 additions and 16 deletions

View File

@ -159,9 +159,21 @@ method stop*(t: TcpTransport) {.async, gcsafe.} =
finally:
t.running = false
template withTransportErrors(body: untyped): untyped =
method accept*(t: TcpTransport): Future[Connection] {.async, gcsafe.} =
## accept a new TCP connection
##
if not t.running:
raise newTransportClosedError()
try:
body
let transp = await t.server.accept()
return await t.connHandler(transp, Direction.In)
except TransportOsError as exc:
# TODO: it doesn't sound like all OS errors
# can be ignored, we should re-raise those
# that can't.
debug "OS Error", exc = exc.msg
except TransportTooManyError as exc:
warn "Too many files opened", exc = exc.msg
except TransportUseClosedError as exc:
@ -171,17 +183,6 @@ template withTransportErrors(body: untyped): untyped =
trace "Unexpected error creating connection", exc = exc.msg
raise exc
method accept*(t: TcpTransport): Future[Connection] {.async, gcsafe.} =
## accept a new TCP connection
##
if not t.running:
raise newTransportClosedError()
withTransportErrors:
let transp = await t.server.accept()
return await t.connHandler(transp, Direction.In)
method dial*(t: TcpTransport,
address: MultiAddress):
Future[Connection] {.async, gcsafe.} =
@ -190,9 +191,8 @@ method dial*(t: TcpTransport,
trace "Dialing remote peer", address = $address
withTransportErrors:
let transp = await connect(address)
return await t.connHandler(transp, Direction.Out)
let transp = await connect(address)
return await t.connHandler(transp, Direction.Out)
method handles*(t: TcpTransport, address: MultiAddress): bool {.gcsafe.} =
if procCall Transport(t).handles(address):