2019-08-22 15:35:47 -06:00
|
|
|
## Nim-LibP2P
|
2019-09-24 11:48:23 -06:00
|
|
|
## Copyright (c) 2019 Status Research & Development GmbH
|
2019-08-22 15:35:47 -06: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.
|
|
|
|
|
2021-05-21 10:27:01 -06:00
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
2021-03-09 13:22:52 +01:00
|
|
|
import std/[oids, strformat]
|
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
|
|
|
|
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
|
|
|
|
shortAgent: string
|
|
|
|
|
|
|
|
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 15:35:47 -06:00
|
|
|
|
2021-07-26 16:12:36 +02:00
|
|
|
declareCounter(libp2p_network_bytes, "total traffic", labels = ["direction"])
|
|
|
|
|
2021-05-21 10:27:01 -06:00
|
|
|
func shortLog*(conn: ChronosStream): auto =
|
|
|
|
try:
|
|
|
|
if conn.isNil: "ChronosStream(nil)"
|
|
|
|
elif conn.peerInfo.isNil: $conn.oid
|
|
|
|
else: &"{shortLog(conn.peerInfo.peerId)}:{conn.oid}"
|
|
|
|
except ValueError as exc:
|
|
|
|
raise newException(Defect, exc.msg)
|
|
|
|
|
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
|
|
|
|
2020-08-04 07:22:05 -06:00
|
|
|
s.timeoutHandler = proc() {.async, gcsafe.} =
|
2020-11-01 16:23:26 -06:00
|
|
|
trace "Idle timeout expired, closing ChronosStream", s
|
2020-08-04 07:22:05 -06:00
|
|
|
await s.close()
|
|
|
|
|
2020-06-19 11:29:43 -06:00
|
|
|
procCall Connection(s).initStream()
|
|
|
|
|
2020-08-04 07:22:05 -06:00
|
|
|
proc init*(C: type ChronosStream,
|
|
|
|
client: StreamTransport,
|
2020-11-01 16:23:26 -06:00
|
|
|
dir: Direction,
|
2020-11-25 13:34:48 -06:00
|
|
|
timeout = DefaultChronosStreamTimeout,
|
|
|
|
observedAddr: MultiAddress = MultiAddress()): ChronosStream =
|
2020-08-04 07:22:05 -06:00
|
|
|
result = C(client: client,
|
2020-11-01 16:23:26 -06:00
|
|
|
timeout: timeout,
|
2020-11-25 13:34:48 -06:00
|
|
|
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) =
|
|
|
|
if not s.tracked:
|
|
|
|
if not isNil(s.peerInfo) and s.peerInfo.agentVersion.len > 0:
|
|
|
|
# / seems a weak "standard" so for now it's reliable
|
2021-03-15 01:42:47 +00:00
|
|
|
let shortAgent = s.peerInfo.agentVersion.split("/")[0].safeToLowerAscii()
|
|
|
|
if shortAgent.isOk() and KnownLibP2PAgentsSeq.contains(shortAgent.get()):
|
|
|
|
s.shortAgent = shortAgent.get()
|
2021-01-08 14:21:24 +09:00
|
|
|
else:
|
|
|
|
s.shortAgent = "unknown"
|
|
|
|
libp2p_peers_identity.inc(labelValues = [s.shortAgent])
|
|
|
|
s.tracked = true
|
|
|
|
|
|
|
|
proc untrackPeerIdentity(s: ChronosStream) =
|
|
|
|
if s.tracked:
|
|
|
|
libp2p_peers_identity.dec(labelValues = [s.shortAgent])
|
|
|
|
s.tracked = false
|
|
|
|
|
2019-12-03 22:44:54 -06:00
|
|
|
method readOnce*(s: ChronosStream, pbytes: pointer, nbytes: int): Future[int] {.async.} =
|
2020-05-19 18:14:15 -06:00
|
|
|
if s.atEof:
|
2019-12-10 14:50:35 -06:00
|
|
|
raise newLPStreamEOFError()
|
2019-08-22 15:35:47 -06:00
|
|
|
|
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
|
2021-07-26 16:12:36 +02:00
|
|
|
libp2p_network_bytes.inc(nbytes.int64, labelValues = ["in"])
|
2021-01-08 14:21:24 +09:00
|
|
|
when defined(libp2p_agents_metrics):
|
|
|
|
s.trackPeerIdentity()
|
|
|
|
if s.tracked:
|
|
|
|
libp2p_peers_traffic_read.inc(nbytes.int64, labelValues = [s.shortAgent])
|
2019-08-22 15:35:47 -06:00
|
|
|
|
2020-05-07 22:37:46 +02:00
|
|
|
method write*(s: ChronosStream, msg: seq[byte]) {.async.} =
|
2020-05-19 18:14:15 -06:00
|
|
|
if s.closed:
|
|
|
|
raise newLPStreamClosedError()
|
|
|
|
|
2020-05-08 22:10:06 +02:00
|
|
|
if msg.len == 0:
|
|
|
|
return
|
2019-08-22 15:35:47 -06:00
|
|
|
|
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
|
|
|
|
var written = await s.client.write(msg)
|
2020-05-23 10:52:33 -06:00
|
|
|
|
2020-05-29 10:46:27 -06:00
|
|
|
if written < msg.len:
|
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-07-26 16:12:36 +02:00
|
|
|
libp2p_network_bytes.inc(msg.len.int64, labelValues = ["out"])
|
2021-01-08 14:21:24 +09:00
|
|
|
when defined(libp2p_agents_metrics):
|
|
|
|
s.trackPeerIdentity()
|
|
|
|
if s.tracked:
|
|
|
|
libp2p_peers_traffic_write.inc(msg.len.int64, labelValues = [s.shortAgent])
|
2020-09-24 07:30:19 +02:00
|
|
|
|
2021-05-21 10:27:01 -06:00
|
|
|
method closed*(s: ChronosStream): bool =
|
2020-05-08 22:10:06 +02:00
|
|
|
result = 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()
|
|
|
|
|
2020-09-21 19:48:19 +02:00
|
|
|
method closeImpl*(s: ChronosStream) {.async.} =
|
2020-05-23 10:52:33 -06:00
|
|
|
try:
|
2021-01-08 14:21:24 +09:00
|
|
|
trace "Shutting down chronos stream", address = $s.client.remoteAddress(), s
|
|
|
|
|
2020-09-21 19:48:19 +02:00
|
|
|
if not s.client.closed():
|
|
|
|
await s.client.closeWait()
|
2020-11-01 16:23:26 -06:00
|
|
|
|
2021-01-08 14:21:24 +09:00
|
|
|
trace "Shutdown chronos stream", address = $s.client.remoteAddress(), s
|
2020-11-01 16:23:26 -06:00
|
|
|
|
2020-07-07 18:33:05 -06:00
|
|
|
except CancelledError as exc:
|
|
|
|
raise exc
|
2020-05-23 10:52:33 -06:00
|
|
|
except CatchableError as exc:
|
2020-11-01 16:23:26 -06:00
|
|
|
trace "Error closing chronosstream", s, msg = exc.msg
|
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()
|