2022-07-01 20:19:57 +02:00
|
|
|
# Nim-LibP2P
|
2024-03-05 08:06:27 +01:00
|
|
|
# Copyright (c) 2023-2024 Status Research & Development GmbH
|
2022-07-01 20:19:57 +02:00
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
# at your option.
|
|
|
|
# This file may not be copied, modified, or distributed except according to
|
|
|
|
# those terms.
|
2019-08-22 15:35:47 -06:00
|
|
|
|
2023-06-07 13:12:49 +02:00
|
|
|
{.push raises: [].}
|
2021-05-21 10:27:01 -06:00
|
|
|
|
2023-05-18 10:24:17 +02:00
|
|
|
import std/[strformat]
|
2022-09-22 21:55:59 +02:00
|
|
|
import stew/results
|
2021-01-08 14:21:24 +09:00
|
|
|
import chronos, chronicles, metrics
|
2020-07-14 02:02:16 +02:00
|
|
|
import connection
|
2021-01-08 14:21:24 +09:00
|
|
|
import ../utility
|
2019-08-22 15:35:47 -06:00
|
|
|
|
2022-09-22 21:55:59 +02:00
|
|
|
export results
|
|
|
|
|
2019-09-11 20:10:38 -06:00
|
|
|
logScope:
|
2020-12-01 11:34:27 -06:00
|
|
|
topics = "libp2p chronosstream"
|
2019-09-11 20:10:38 -06:00
|
|
|
|
2020-08-04 07:22:05 -06:00
|
|
|
const
|
|
|
|
DefaultChronosStreamTimeout = 10.minutes
|
2020-11-04 21:52:54 -06:00
|
|
|
ChronosStreamTrackerName* = "ChronosStream"
|
2020-08-04 07:22:05 -06:00
|
|
|
|
|
|
|
type ChronosStream* = ref object of Connection
|
2019-08-22 15:35:47 -06:00
|
|
|
client: StreamTransport
|
2021-01-08 14:21:24 +09:00
|
|
|
when defined(libp2p_agents_metrics):
|
|
|
|
tracked: bool
|
|
|
|
|
|
|
|
when defined(libp2p_agents_metrics):
|
2024-03-05 08:06:27 +01:00
|
|
|
declareGauge libp2p_peers_identity, "peers identities", labels = ["agent"]
|
|
|
|
declareCounter libp2p_peers_traffic_read, "incoming traffic", labels = ["agent"]
|
|
|
|
declareCounter libp2p_peers_traffic_write, "outgoing traffic", labels = ["agent"]
|
2019-08-22 15:35:47 -06:00
|
|
|
|
2024-03-05 08:06:27 +01:00
|
|
|
declareCounter libp2p_network_bytes, "total traffic", labels = ["direction"]
|
2021-07-26 16:12:36 +02:00
|
|
|
|
2021-05-21 10:27:01 -06:00
|
|
|
func shortLog*(conn: ChronosStream): auto =
|
|
|
|
try:
|
2024-03-05 08:06:27 +01:00
|
|
|
if conn == nil:
|
|
|
|
"ChronosStream(nil)"
|
2021-09-08 11:07:46 +02:00
|
|
|
else:
|
|
|
|
&"{shortLog(conn.peerId)}:{conn.oid}"
|
2021-05-21 10:27:01 -06:00
|
|
|
except ValueError as exc:
|
2024-03-05 08:06:27 +01:00
|
|
|
raiseAssert(exc.msg)
|
2021-05-21 10:27:01 -06:00
|
|
|
|
2020-09-06 10:31:47 +02:00
|
|
|
chronicles.formatIt(ChronosStream):
|
|
|
|
shortLog(it)
|
|
|
|
|
2020-06-19 11:29:43 -06:00
|
|
|
method initStream*(s: ChronosStream) =
|
|
|
|
if s.objName.len == 0:
|
2021-07-13 13:53:08 +02:00
|
|
|
s.objName = ChronosStreamTrackerName
|
2020-06-19 11:29:43 -06:00
|
|
|
|
2024-03-05 08:06:27 +01:00
|
|
|
s.timeoutHandler = proc(): Future[void] {.async: (raises: [], raw: true).} =
|
2020-11-01 16:23:26 -06:00
|
|
|
trace "Idle timeout expired, closing ChronosStream", s
|
2024-03-05 08:06:27 +01:00
|
|
|
s.close()
|
2020-08-04 07:22:05 -06:00
|
|
|
|
2020-06-19 11:29:43 -06:00
|
|
|
procCall Connection(s).initStream()
|
|
|
|
|
2024-03-05 08:06:27 +01:00
|
|
|
proc init*(
|
|
|
|
C: type ChronosStream,
|
|
|
|
client: StreamTransport,
|
|
|
|
dir: Direction,
|
|
|
|
timeout = DefaultChronosStreamTimeout,
|
|
|
|
observedAddr: Opt[MultiAddress],
|
|
|
|
): ChronosStream =
|
2020-11-25 13:34:48 -06:00
|
|
|
result = C(client: client, timeout: timeout, dir: dir, observedAddr: observedAddr)
|
2020-06-19 11:29:43 -06:00
|
|
|
result.initStream()
|
2019-12-03 22:44:54 -06:00
|
|
|
|
2020-03-27 08:25:52 -06:00
|
|
|
template withExceptions(body: untyped) =
|
|
|
|
try:
|
|
|
|
body
|
2020-11-01 21:49:25 +01:00
|
|
|
except CancelledError as exc:
|
|
|
|
raise exc
|
2020-03-27 08:25:52 -06:00
|
|
|
except TransportIncompleteError:
|
2020-05-23 10:52:33 -06:00
|
|
|
# for all intents and purposes this is an EOF
|
2020-08-11 18:05:49 -06:00
|
|
|
raise newLPStreamIncompleteError()
|
2020-03-27 08:25:52 -06:00
|
|
|
except TransportLimitError:
|
|
|
|
raise newLPStreamLimitError()
|
2020-05-08 22:10:06 +02:00
|
|
|
except TransportUseClosedError:
|
|
|
|
raise newLPStreamEOFError()
|
|
|
|
except TransportError:
|
|
|
|
# TODO https://github.com/status-im/nim-chronos/pull/99
|
|
|
|
raise newLPStreamEOFError()
|
2020-03-27 08:25:52 -06:00
|
|
|
|
2021-01-08 14:21:24 +09:00
|
|
|
when defined(libp2p_agents_metrics):
|
|
|
|
proc trackPeerIdentity(s: ChronosStream) =
|
2021-09-08 11:07:46 +02:00
|
|
|
if not s.tracked and s.shortAgent.len > 0:
|
|
|
|
libp2p_peers_identity.inc(labelValues = [s.shortAgent])
|
|
|
|
s.tracked = true
|
2021-01-08 14:21:24 +09:00
|
|
|
|
|
|
|
proc untrackPeerIdentity(s: ChronosStream) =
|
|
|
|
if s.tracked:
|
|
|
|
libp2p_peers_identity.dec(labelValues = [s.shortAgent])
|
|
|
|
s.tracked = false
|
|
|
|
|
2024-03-05 08:06:27 +01:00
|
|
|
method readOnce*(
|
|
|
|
s: ChronosStream, pbytes: pointer, nbytes: int
|
|
|
|
): Future[int] {.async: (raises: [CancelledError, LPStreamError]).} =
|
2020-05-19 18:14:15 -06:00
|
|
|
if s.atEof:
|
2019-12-10 14:50:35 -06:00
|
|
|
raise newLPStreamEOFError()
|
2020-03-27 08:25:52 -06:00
|
|
|
withExceptions:
|
2020-05-08 22:10:06 +02:00
|
|
|
result = await s.client.readOnce(pbytes, nbytes)
|
2020-08-04 07:22:05 -06:00
|
|
|
s.activity = true # reset activity flag
|
2023-04-03 14:37:23 +02:00
|
|
|
libp2p_network_bytes.inc(result.int64, labelValues = ["in"])
|
2021-01-08 14:21:24 +09:00
|
|
|
when defined(libp2p_agents_metrics):
|
|
|
|
s.trackPeerIdentity()
|
|
|
|
if s.tracked:
|
2023-04-03 14:37:23 +02:00
|
|
|
libp2p_peers_traffic_read.inc(result.int64, labelValues = [s.shortAgent])
|
2019-08-22 15:35:47 -06:00
|
|
|
|
2021-12-14 10:55:17 +01:00
|
|
|
proc completeWrite(
|
2024-03-05 08:06:27 +01:00
|
|
|
s: ChronosStream,
|
|
|
|
fut: Future[int].Raising([TransportError, CancelledError]),
|
|
|
|
msgLen: int,
|
|
|
|
): Future[void] {.async: (raises: [CancelledError, LPStreamError]).} =
|
2020-03-27 08:25:52 -06:00
|
|
|
withExceptions:
|
2020-09-24 07:30:19 +02:00
|
|
|
# StreamTransport will only return written < msg.len on fatal failures where
|
|
|
|
# further writing is not possible - in such cases, we'll raise here,
|
|
|
|
# since we don't return partial writes lengths
|
2021-12-14 10:55:17 +01:00
|
|
|
var written = await fut
|
2020-05-23 10:52:33 -06:00
|
|
|
|
2021-12-14 10:55:17 +01:00
|
|
|
if written < msgLen:
|
2020-05-23 10:52:33 -06:00
|
|
|
raise (ref LPStreamClosedError)(msg: "Write couldn't finish writing")
|
2019-12-03 22:44:54 -06:00
|
|
|
|
2020-09-24 07:30:19 +02:00
|
|
|
s.activity = true # reset activity flag
|
2021-12-14 10:55:17 +01:00
|
|
|
libp2p_network_bytes.inc(msgLen.int64, labelValues = ["out"])
|
2021-01-08 14:21:24 +09:00
|
|
|
when defined(libp2p_agents_metrics):
|
|
|
|
s.trackPeerIdentity()
|
|
|
|
if s.tracked:
|
2021-12-14 10:55:17 +01:00
|
|
|
libp2p_peers_traffic_write.inc(msgLen.int64, labelValues = [s.shortAgent])
|
|
|
|
|
2024-03-05 08:06:27 +01:00
|
|
|
method write*(
|
|
|
|
s: ChronosStream, msg: seq[byte]
|
|
|
|
): Future[void] {.async: (raises: [CancelledError, LPStreamError], raw: true).} =
|
2021-12-14 10:55:17 +01:00
|
|
|
# Avoid a copy of msg being kept in the closure created by `{.async.}` as this
|
|
|
|
# drives up memory usage
|
2022-09-29 20:02:10 +02:00
|
|
|
if msg.len == 0:
|
|
|
|
trace "Empty byte seq, nothing to write"
|
2023-07-10 15:52:08 +02:00
|
|
|
let fut = newFuture[void]("chronosstream.write.empty")
|
|
|
|
fut.complete()
|
|
|
|
return fut
|
2021-12-14 10:55:17 +01:00
|
|
|
if s.closed:
|
|
|
|
let fut = newFuture[void]("chronosstream.write.closed")
|
|
|
|
fut.fail(newLPStreamClosedError())
|
|
|
|
return fut
|
|
|
|
|
|
|
|
s.completeWrite(s.client.write(msg), msg.len)
|
2020-09-24 07:30:19 +02:00
|
|
|
|
2021-05-21 10:27:01 -06:00
|
|
|
method closed*(s: ChronosStream): bool =
|
2021-12-14 10:55:17 +01:00
|
|
|
s.client.closed
|
2019-08-22 15:35:47 -06:00
|
|
|
|
2021-05-21 10:27:01 -06:00
|
|
|
method atEof*(s: ChronosStream): bool =
|
2020-05-19 18:14:15 -06:00
|
|
|
s.client.atEof()
|
|
|
|
|
2024-03-05 08:06:27 +01:00
|
|
|
method closeImpl*(s: ChronosStream) {.async: (raises: []).} =
|
|
|
|
trace "Shutting down chronos stream", address = $s.client.remoteAddress(), s
|
2020-11-01 16:23:26 -06:00
|
|
|
|
2024-03-05 08:06:27 +01:00
|
|
|
if not s.client.closed():
|
|
|
|
await s.client.closeWait()
|
2020-11-01 16:23:26 -06:00
|
|
|
|
2024-03-05 08:06:27 +01:00
|
|
|
trace "Shutdown chronos stream", address = $s.client.remoteAddress(), s
|
2021-01-20 22:00:24 -06:00
|
|
|
|
2021-01-08 14:21:24 +09:00
|
|
|
when defined(libp2p_agents_metrics):
|
|
|
|
# do this after closing!
|
|
|
|
s.untrackPeerIdentity()
|
2020-09-21 19:48:19 +02:00
|
|
|
|
|
|
|
await procCall Connection(s).closeImpl()
|
2022-08-01 14:31:22 +02:00
|
|
|
|
|
|
|
method getWrapped*(s: ChronosStream): Connection =
|
|
|
|
nil
|