2022-07-01 18:19:57 +00:00
|
|
|
# Nim-LibP2P
|
2023-01-20 14:47:40 +00:00
|
|
|
# Copyright (c) 2023 Status Research & Development GmbH
|
2022-07-01 18:19:57 +00: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 21:35:47 +00:00
|
|
|
|
2022-08-03 11:33:19 +00:00
|
|
|
when (NimMajor, NimMinor) < (1, 4):
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
else:
|
|
|
|
{.push raises: [].}
|
2021-05-21 16:27:01 +00:00
|
|
|
|
2021-03-09 12:22:52 +00:00
|
|
|
import std/[oids, strformat]
|
2022-09-22 19:55:59 +00:00
|
|
|
import stew/results
|
2021-01-08 05:21:24 +00:00
|
|
|
import chronos, chronicles, metrics
|
2020-07-14 00:02:16 +00:00
|
|
|
import connection
|
2021-01-08 05:21:24 +00:00
|
|
|
import ../utility
|
2019-08-22 21:35:47 +00:00
|
|
|
|
2022-09-22 19:55:59 +00:00
|
|
|
export results
|
|
|
|
|
2019-09-12 02:10:38 +00:00
|
|
|
logScope:
|
2020-12-01 17:34:27 +00:00
|
|
|
topics = "libp2p chronosstream"
|
2019-09-12 02:10:38 +00:00
|
|
|
|
2020-08-04 13:22:05 +00:00
|
|
|
const
|
|
|
|
DefaultChronosStreamTimeout = 10.minutes
|
2020-11-05 03:52:54 +00:00
|
|
|
ChronosStreamTrackerName* = "ChronosStream"
|
2020-08-04 13:22:05 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
ChronosStream* = ref object of Connection
|
2019-08-22 21:35:47 +00:00
|
|
|
client: StreamTransport
|
2021-01-08 05:21:24 +00:00
|
|
|
when defined(libp2p_agents_metrics):
|
|
|
|
tracked: bool
|
|
|
|
|
|
|
|
when defined(libp2p_agents_metrics):
|
|
|
|
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 21:35:47 +00:00
|
|
|
|
2021-07-26 14:12:36 +00:00
|
|
|
declareCounter(libp2p_network_bytes, "total traffic", labels = ["direction"])
|
|
|
|
|
2021-05-21 16:27:01 +00:00
|
|
|
func shortLog*(conn: ChronosStream): auto =
|
|
|
|
try:
|
|
|
|
if conn.isNil: "ChronosStream(nil)"
|
2021-09-08 09:07:46 +00:00
|
|
|
else: &"{shortLog(conn.peerId)}:{conn.oid}"
|
2021-05-21 16:27:01 +00:00
|
|
|
except ValueError as exc:
|
|
|
|
raise newException(Defect, exc.msg)
|
|
|
|
|
2020-09-06 08:31:47 +00:00
|
|
|
chronicles.formatIt(ChronosStream): shortLog(it)
|
|
|
|
|
2020-06-19 17:29:43 +00:00
|
|
|
method initStream*(s: ChronosStream) =
|
|
|
|
if s.objName.len == 0:
|
2021-07-13 11:53:08 +00:00
|
|
|
s.objName = ChronosStreamTrackerName
|
2020-06-19 17:29:43 +00:00
|
|
|
|
2020-08-04 13:22:05 +00:00
|
|
|
s.timeoutHandler = proc() {.async, gcsafe.} =
|
2020-11-01 22:23:26 +00:00
|
|
|
trace "Idle timeout expired, closing ChronosStream", s
|
2020-08-04 13:22:05 +00:00
|
|
|
await s.close()
|
|
|
|
|
2020-06-19 17:29:43 +00:00
|
|
|
procCall Connection(s).initStream()
|
|
|
|
|
2020-08-04 13:22:05 +00:00
|
|
|
proc init*(C: type ChronosStream,
|
|
|
|
client: StreamTransport,
|
2020-11-01 22:23:26 +00:00
|
|
|
dir: Direction,
|
2020-11-25 19:34:48 +00:00
|
|
|
timeout = DefaultChronosStreamTimeout,
|
2022-09-22 19:55:59 +00:00
|
|
|
observedAddr: Opt[MultiAddress]): ChronosStream =
|
2020-08-04 13:22:05 +00:00
|
|
|
result = C(client: client,
|
2020-11-01 22:23:26 +00:00
|
|
|
timeout: timeout,
|
2020-11-25 19:34:48 +00:00
|
|
|
dir: dir,
|
|
|
|
observedAddr: observedAddr)
|
2020-06-19 17:29:43 +00:00
|
|
|
result.initStream()
|
2019-12-04 04:44:54 +00:00
|
|
|
|
2020-03-27 14:25:52 +00:00
|
|
|
template withExceptions(body: untyped) =
|
|
|
|
try:
|
|
|
|
body
|
2020-11-01 20:49:25 +00:00
|
|
|
except CancelledError as exc:
|
|
|
|
raise exc
|
2020-03-27 14:25:52 +00:00
|
|
|
except TransportIncompleteError:
|
2020-05-23 16:52:33 +00:00
|
|
|
# for all intents and purposes this is an EOF
|
2020-08-12 00:05:49 +00:00
|
|
|
raise newLPStreamIncompleteError()
|
2020-03-27 14:25:52 +00:00
|
|
|
except TransportLimitError:
|
|
|
|
raise newLPStreamLimitError()
|
2020-05-08 20:10:06 +00:00
|
|
|
except TransportUseClosedError:
|
|
|
|
raise newLPStreamEOFError()
|
|
|
|
except TransportError:
|
|
|
|
# TODO https://github.com/status-im/nim-chronos/pull/99
|
|
|
|
raise newLPStreamEOFError()
|
2020-03-27 14:25:52 +00:00
|
|
|
|
2021-01-08 05:21:24 +00:00
|
|
|
when defined(libp2p_agents_metrics):
|
|
|
|
proc trackPeerIdentity(s: ChronosStream) =
|
2021-09-08 09:07:46 +00:00
|
|
|
if not s.tracked and s.shortAgent.len > 0:
|
|
|
|
libp2p_peers_identity.inc(labelValues = [s.shortAgent])
|
|
|
|
s.tracked = true
|
2021-01-08 05:21:24 +00:00
|
|
|
|
|
|
|
proc untrackPeerIdentity(s: ChronosStream) =
|
|
|
|
if s.tracked:
|
|
|
|
libp2p_peers_identity.dec(labelValues = [s.shortAgent])
|
|
|
|
s.tracked = false
|
|
|
|
|
2019-12-04 04:44:54 +00:00
|
|
|
method readOnce*(s: ChronosStream, pbytes: pointer, nbytes: int): Future[int] {.async.} =
|
2020-05-20 00:14:15 +00:00
|
|
|
if s.atEof:
|
2019-12-10 20:50:35 +00:00
|
|
|
raise newLPStreamEOFError()
|
2020-03-27 14:25:52 +00:00
|
|
|
withExceptions:
|
2020-05-08 20:10:06 +00:00
|
|
|
result = await s.client.readOnce(pbytes, nbytes)
|
2020-08-04 13:22:05 +00:00
|
|
|
s.activity = true # reset activity flag
|
2021-07-26 14:12:36 +00:00
|
|
|
libp2p_network_bytes.inc(nbytes.int64, labelValues = ["in"])
|
2021-01-08 05:21:24 +00:00
|
|
|
when defined(libp2p_agents_metrics):
|
|
|
|
s.trackPeerIdentity()
|
|
|
|
if s.tracked:
|
|
|
|
libp2p_peers_traffic_read.inc(nbytes.int64, labelValues = [s.shortAgent])
|
2019-08-22 21:35:47 +00:00
|
|
|
|
2021-12-14 09:55:17 +00:00
|
|
|
proc completeWrite(
|
|
|
|
s: ChronosStream, fut: Future[int], msgLen: int): Future[void] {.async.} =
|
2020-03-27 14:25:52 +00:00
|
|
|
withExceptions:
|
2020-09-24 05:30:19 +00: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 09:55:17 +00:00
|
|
|
var written = await fut
|
2020-05-23 16:52:33 +00:00
|
|
|
|
2021-12-14 09:55:17 +00:00
|
|
|
if written < msgLen:
|
2020-05-23 16:52:33 +00:00
|
|
|
raise (ref LPStreamClosedError)(msg: "Write couldn't finish writing")
|
2019-12-04 04:44:54 +00:00
|
|
|
|
2020-09-24 05:30:19 +00:00
|
|
|
s.activity = true # reset activity flag
|
2021-12-14 09:55:17 +00:00
|
|
|
libp2p_network_bytes.inc(msgLen.int64, labelValues = ["out"])
|
2021-01-08 05:21:24 +00:00
|
|
|
when defined(libp2p_agents_metrics):
|
|
|
|
s.trackPeerIdentity()
|
|
|
|
if s.tracked:
|
2021-12-14 09:55:17 +00:00
|
|
|
libp2p_peers_traffic_write.inc(msgLen.int64, labelValues = [s.shortAgent])
|
|
|
|
|
|
|
|
method write*(s: ChronosStream, msg: seq[byte]): Future[void] =
|
|
|
|
# Avoid a copy of msg being kept in the closure created by `{.async.}` as this
|
|
|
|
# drives up memory usage
|
2022-09-29 18:02:10 +00:00
|
|
|
if msg.len == 0:
|
|
|
|
trace "Empty byte seq, nothing to write"
|
|
|
|
return
|
2021-12-14 09:55:17 +00: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 05:30:19 +00:00
|
|
|
|
2021-05-21 16:27:01 +00:00
|
|
|
method closed*(s: ChronosStream): bool =
|
2021-12-14 09:55:17 +00:00
|
|
|
s.client.closed
|
2019-08-22 21:35:47 +00:00
|
|
|
|
2021-05-21 16:27:01 +00:00
|
|
|
method atEof*(s: ChronosStream): bool =
|
2020-05-20 00:14:15 +00:00
|
|
|
s.client.atEof()
|
|
|
|
|
2020-09-21 17:48:19 +00:00
|
|
|
method closeImpl*(s: ChronosStream) {.async.} =
|
2020-05-23 16:52:33 +00:00
|
|
|
try:
|
2021-01-08 05:21:24 +00:00
|
|
|
trace "Shutting down chronos stream", address = $s.client.remoteAddress(), s
|
|
|
|
|
2020-09-21 17:48:19 +00:00
|
|
|
if not s.client.closed():
|
|
|
|
await s.client.closeWait()
|
2020-11-01 22:23:26 +00:00
|
|
|
|
2021-01-08 05:21:24 +00:00
|
|
|
trace "Shutdown chronos stream", address = $s.client.remoteAddress(), s
|
2020-11-01 22:23:26 +00:00
|
|
|
|
2020-07-08 00:33:05 +00:00
|
|
|
except CancelledError as exc:
|
|
|
|
raise exc
|
2020-05-23 16:52:33 +00:00
|
|
|
except CatchableError as exc:
|
2020-11-01 22:23:26 +00:00
|
|
|
trace "Error closing chronosstream", s, msg = exc.msg
|
2021-01-21 04:00:24 +00:00
|
|
|
|
2021-01-08 05:21:24 +00:00
|
|
|
when defined(libp2p_agents_metrics):
|
|
|
|
# do this after closing!
|
|
|
|
s.untrackPeerIdentity()
|
2020-09-21 17:48:19 +00:00
|
|
|
|
|
|
|
await procCall Connection(s).closeImpl()
|
2022-08-01 12:31:22 +00:00
|
|
|
|
|
|
|
method getWrapped*(s: ChronosStream): Connection = nil
|