From dd4da7491da15c186cc1498b068c999f794d1753 Mon Sep 17 00:00:00 2001 From: Kim De Mey Date: Thu, 15 Jun 2023 06:32:33 -0600 Subject: [PATCH] Remove yield usage from portal stream and protocol (#1602) --- fluffy/network/wire/portal_protocol.nim | 12 ++------ fluffy/network/wire/portal_stream.nim | 38 +++++++------------------ 2 files changed, 12 insertions(+), 38 deletions(-) diff --git a/fluffy/network/wire/portal_protocol.nim b/fluffy/network/wire/portal_protocol.nim index b94c996381..19ad21c50a 100644 --- a/fluffy/network/wire/portal_protocol.nim +++ b/fluffy/network/wire/portal_protocol.nim @@ -609,20 +609,12 @@ proc findContent*(p: PortalProtocol, dst: Node, contentKey: ByteList): return err("Trying to connect to node with unknown address") # uTP protocol uses BE for all values in the header, incl. connection id - let connFuture = p.stream.connectTo( + let connectionResult = + await p.stream.connectTo( nodeAddress.unsafeGet(), uint16.fromBytesBE(m.connectionId) ) - yield connFuture - - var connectionResult: Result[UtpSocket[NodeAddress], string] - - if connFuture.completed(): - connectionResult = connFuture.read() - else: - raise connFuture.error - if connectionResult.isErr(): debug "uTP connection error while trying to find content", error = connectionResult.error diff --git a/fluffy/network/wire/portal_stream.nim b/fluffy/network/wire/portal_stream.nim index fdabf65845..102a0d4525 100644 --- a/fluffy/network/wire/portal_stream.nim +++ b/fluffy/network/wire/portal_stream.nim @@ -131,42 +131,24 @@ proc connectTo*( nodeAddress: NodeAddress, connectionId: uint16): Future[Result[UtpSocket[NodeAddress], string]] {.async.} = - let connectFut = stream.transport.connectTo(nodeAddress, connectionId) - - # using yield, not await, as await does not play nice with cancellation - # interacting with async procs which allocates some resource - yield connectFut - - var socketRes: ConnectionResult[NodeAddress] - - if connectFut.completed(): - socketRes = connectFut.read() - else: - raise connectFut.error - - if socketRes.isErr(): - case socketRes.error.kind + let connectRes = await stream.transport.connectTo(nodeAddress, connectionId) + if connectRes.isErr(): + case connectRes.error.kind of SocketAlreadyExists: # This means that there is already a socket to this nodeAddress with given - # connection id. It probably means that a peersent us a connection id - # which is already in use.. - # For now just fail the connection and return an error. Another strategy - # to consider would be to check what is the connection status, and then - # re-use it, or close it and retry connection. + # connection id. This means that a peer sent us a connection id which is + # already in use. The connection is failed and an error returned. let msg = "Socket to " & $nodeAddress & "with connection id: " & $connectionId & " already exists" return err(msg) of ConnectionTimedOut: - # Another strategy for handling this error would be to retry connecting a - # few times before giving up. But we know (as we control the uTP impl) - # that this error will only occur when a SYN packet was re-sent 3 times - # and failed to be acked. This should be enough of indication that the - # remote host is not reachable. + # A time-out here means that a uTP SYN packet was re-sent 3 times and + # failed to be acked. This should be enough of indication that the + # remote host is not reachable and no new connections are attempted. let msg = "uTP timeout while trying to connect to " & $nodeAddress return err(msg) - - let socket = socketRes.get() - return ok(socket) + else: + return ok(connectRes.get()) proc writeContentRequest( socket: UtpSocket[NodeAddress], stream: PortalStream,