From ca9c5c85e4363808d3f89655e2befe939959bdc2 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Wed, 25 Nov 2020 13:34:48 -0600 Subject: [PATCH] dont break chronicles logging streamline connsetup (#455) --- libp2p/stream/chronosstream.nim | 6 ++++-- libp2p/stream/connection.nim | 6 ++++-- libp2p/transports/tcptransport.nim | 25 +++++++++++++------------ 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/libp2p/stream/chronosstream.nim b/libp2p/stream/chronosstream.nim index 259de3b79..b496f1978 100644 --- a/libp2p/stream/chronosstream.nim +++ b/libp2p/stream/chronosstream.nim @@ -41,10 +41,12 @@ method initStream*(s: ChronosStream) = proc init*(C: type ChronosStream, client: StreamTransport, dir: Direction, - timeout = DefaultChronosStreamTimeout): ChronosStream = + timeout = DefaultChronosStreamTimeout, + observedAddr: MultiAddress = MultiAddress()): ChronosStream = result = C(client: client, timeout: timeout, - dir: dir) + dir: dir, + observedAddr: observedAddr) result.initStream() template withExceptions(body: untyped) = diff --git a/libp2p/stream/connection.nim b/libp2p/stream/connection.nim index 2b8c5e932..0acdeeb68 100644 --- a/libp2p/stream/connection.nim +++ b/libp2p/stream/connection.nim @@ -110,10 +110,12 @@ proc init*(C: type Connection, peerInfo: PeerInfo, dir: Direction, timeout: Duration = DefaultConnectionTimeout, - timeoutHandler: TimeoutHandler = nil): Connection = + timeoutHandler: TimeoutHandler = nil, + observedAddr: MultiAddress = MultiAddress()): Connection = result = C(peerInfo: peerInfo, dir: dir, timeout: timeout, - timeoutHandler: timeoutHandler) + timeoutHandler: timeoutHandler, + observedAddr: observedAddr) result.initStream() diff --git a/libp2p/transports/tcptransport.nim b/libp2p/transports/tcptransport.nim index 655246431..eb7f413ed 100644 --- a/libp2p/transports/tcptransport.nim +++ b/libp2p/transports/tcptransport.nim @@ -60,15 +60,25 @@ proc setupTcpTransportTracker(): TcpTransportTracker = proc connHandler*(t: TcpTransport, client: StreamTransport, dir: Direction): Future[Connection] {.async.} = - debug "Handling tcp connection", address = $client.remoteAddress, + var observedAddr: MultiAddress = MultiAddress() + try: + observedAddr = MultiAddress.init(client.remoteAddress).tryGet() + except CatchableError as exc: + trace "Connection setup failed", exc = exc.msg + if not(isNil(client) and client.closed): + await client.closeWait() + raise exc + + debug "Handling tcp connection", address = $observedAddr, dir = $dir, clients = t.clients[Direction.In].len + t.clients[Direction.Out].len let conn = Connection( ChronosStream.init( - client, - dir + client = client, + dir = dir, + observedAddr = observedAddr )) proc onClose() {.async.} = @@ -95,15 +105,6 @@ proc connHandler*(t: TcpTransport, t.clients[dir].add(client) asyncSpawn onClose() - try: - conn.observedAddr = MultiAddress.init(client.remoteAddress).tryGet() - except CatchableError as exc: - trace "Connection setup failed", exc = exc.msg, conn - if not(isNil(client) and client.closed): - await client.closeWait() - - raise exc - return conn proc init*(T: type TcpTransport,