2022-07-01 18:19:57 +00:00
|
|
|
# Nim-LibP2P
|
2024-03-05 07:06:27 +00:00
|
|
|
# Copyright (c) 2023-2024 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.
|
2020-06-19 17:29:43 +00:00
|
|
|
|
2023-06-07 11:12:49 +00:00
|
|
|
{.push raises: [].}
|
2021-05-21 16:27:01 +00:00
|
|
|
|
2020-11-19 02:06:42 +00:00
|
|
|
import std/[hashes, oids, strformat]
|
2022-09-22 19:55:59 +00:00
|
|
|
import stew/results
|
2020-08-02 10:22:49 +00:00
|
|
|
import chronicles, chronos, metrics
|
2020-06-19 17:29:43 +00:00
|
|
|
import lpstream, ../multiaddress, ../peerinfo, ../errors
|
|
|
|
|
2022-09-22 19:55:59 +00:00
|
|
|
export lpstream, peerinfo, errors, results
|
2020-06-19 17:29:43 +00:00
|
|
|
|
2020-08-02 10:22:49 +00:00
|
|
|
logScope:
|
2020-12-01 17:34:27 +00:00
|
|
|
topics = "libp2p connection"
|
2020-08-02 10:22:49 +00:00
|
|
|
|
2020-06-19 17:29:43 +00:00
|
|
|
const
|
2020-11-05 03:52:54 +00:00
|
|
|
ConnectionTrackerName* = "Connection"
|
2020-08-10 22:17:11 +00:00
|
|
|
DefaultConnectionTimeout* = 5.minutes
|
2020-06-19 17:29:43 +00:00
|
|
|
|
|
|
|
type
|
2024-03-05 07:06:27 +00:00
|
|
|
TimeoutHandler* = proc(): Future[void] {.async: (raises: []).}
|
2020-08-04 13:22:05 +00:00
|
|
|
|
2020-06-19 17:29:43 +00:00
|
|
|
Connection* = ref object of LPStream
|
2024-03-05 07:06:27 +00:00
|
|
|
activity*: bool # reset every time data is sent or received
|
|
|
|
timeout*: Duration # channel timeout if no activity
|
|
|
|
timerTaskFut: Future[void].Raising([]) # the current timer instance
|
2020-08-04 13:22:05 +00:00
|
|
|
timeoutHandler*: TimeoutHandler # timeout handler
|
2021-09-08 09:07:46 +00:00
|
|
|
peerId*: PeerId
|
2022-09-22 19:55:59 +00:00
|
|
|
observedAddr*: Opt[MultiAddress]
|
2024-03-05 07:06:27 +00:00
|
|
|
protocol*: string # protocol used by the connection, used as metrics tag
|
|
|
|
transportDir*: Direction # underlying transport (usually socket) direction
|
2021-09-08 09:07:46 +00:00
|
|
|
when defined(libp2p_agents_metrics):
|
|
|
|
shortAgent*: string
|
2020-06-19 17:29:43 +00:00
|
|
|
|
2024-03-05 07:06:27 +00:00
|
|
|
proc timeoutMonitor(s: Connection) {.async: (raises: []).}
|
2020-06-19 17:29:43 +00:00
|
|
|
|
2024-10-31 09:29:32 +00:00
|
|
|
method shortLog*(conn: Connection): string {.raises: [].} =
|
|
|
|
if conn == nil:
|
|
|
|
"Connection(nil)"
|
|
|
|
else:
|
|
|
|
&"{shortLog(conn.peerId)}:{conn.oid}:{conn.protocol}"
|
2021-05-21 16:27:01 +00:00
|
|
|
|
2020-09-06 08:31:47 +00:00
|
|
|
chronicles.formatIt(Connection):
|
|
|
|
shortLog(it)
|
|
|
|
|
2020-06-19 17:29:43 +00:00
|
|
|
method initStream*(s: Connection) =
|
|
|
|
if s.objName.len == 0:
|
2021-06-14 08:26:11 +00:00
|
|
|
s.objName = ConnectionTrackerName
|
2020-06-19 17:29:43 +00:00
|
|
|
|
|
|
|
procCall LPStream(s).initStream()
|
2020-08-10 22:17:11 +00:00
|
|
|
|
2024-03-05 07:06:27 +00:00
|
|
|
doAssert(s.timerTaskFut == nil)
|
refactor bufferstream to use a queue (#346)
This change modifies how the backpressure algorithm in bufferstream
works - in particular, instead of working byte-by-byte, it will now work
seq-by-seq.
When data arrives, it usually does so in packets - in the current
bufferstream, the packet is read then split into bytes which are fed one
by one to the bufferstream. On the reading side, the bytes are popped of
the bufferstream, again byte by byte, to satisfy `readOnce` requests -
this introduces a lot of synchronization traffic because the checks for
full buffer and for async event handling must be done for every byte.
In this PR, a queue of length 1 is used instead - this means there will
at most exist one "packet" in `pushTo`, one in the queue and one in the
slush buffer that is used to store incomplete reads.
* avoid byte-by-byte copy to buffer, with synchronization in-between
* reuse AsyncQueue synchronization logic instead of rolling own
* avoid writeHandler callback - implement `write` method instead
* simplify EOF signalling by only setting EOF flag in queue reader (and
reset)
* remove BufferStream pipes (unused)
* fixes drainBuffer deadlock when drain is called from within read loop
and thus blocks draining
* fix lpchannel init order
2020-09-10 06:19:13 +00:00
|
|
|
|
2020-08-10 22:17:11 +00:00
|
|
|
if s.timeout > 0.millis:
|
2020-09-09 17:12:08 +00:00
|
|
|
trace "Monitoring for timeout", s, timeout = s.timeout
|
|
|
|
|
2020-08-10 22:17:11 +00:00
|
|
|
s.timerTaskFut = s.timeoutMonitor()
|
2024-03-05 07:06:27 +00:00
|
|
|
if s.timeoutHandler == nil:
|
|
|
|
s.timeoutHandler = proc(): Future[void] {.async: (raises: [], raw: true).} =
|
|
|
|
trace "Idle timeout expired, closing connection", s
|
|
|
|
s.close()
|
2020-08-04 13:22:05 +00:00
|
|
|
|
2024-03-05 07:06:27 +00:00
|
|
|
method closeImpl*(s: Connection): Future[void] {.async: (raises: []).} =
|
2020-09-21 17:48:19 +00:00
|
|
|
# Cleanup timeout timer
|
|
|
|
trace "Closing connection", s
|
2021-01-04 18:59:05 +00:00
|
|
|
|
2024-03-05 07:06:27 +00:00
|
|
|
if s.timerTaskFut != nil and not s.timerTaskFut.finished:
|
|
|
|
# Don't `cancelAndWait` here to avoid risking deadlock in this scenario:
|
|
|
|
# - `pollActivity` is waiting for `s.timeoutHandler` to complete.
|
|
|
|
# - `s.timeoutHandler` may have triggered `closeImpl` and we are now here.
|
|
|
|
# In this situation, we have to return for `s.timerTaskFut` to complete.
|
|
|
|
s.timerTaskFut.cancelSoon()
|
2020-11-29 12:34:19 +00:00
|
|
|
s.timerTaskFut = nil
|
2020-08-04 13:22:05 +00:00
|
|
|
|
2020-11-01 22:23:26 +00:00
|
|
|
trace "Closed connection", s
|
2020-09-21 17:48:19 +00:00
|
|
|
|
|
|
|
procCall LPStream(s).closeImpl()
|
2020-06-19 17:29:43 +00:00
|
|
|
|
2020-07-17 15:36:48 +00:00
|
|
|
func hash*(p: Connection): Hash =
|
|
|
|
cast[pointer](p).hash
|
2020-08-04 13:22:05 +00:00
|
|
|
|
2024-03-05 07:06:27 +00:00
|
|
|
proc pollActivity(s: Connection): Future[bool] {.async: (raises: []).} =
|
2020-11-29 12:34:19 +00:00
|
|
|
if s.closed and s.atEof:
|
|
|
|
return false # Done, no more monitoring
|
|
|
|
|
|
|
|
if s.activity:
|
|
|
|
s.activity = false
|
|
|
|
return true
|
|
|
|
|
|
|
|
# Inactivity timeout happened, call timeout monitor
|
|
|
|
|
|
|
|
trace "Connection timed out", s
|
2024-03-05 07:06:27 +00:00
|
|
|
if s.timeoutHandler != nil:
|
2020-11-29 12:34:19 +00:00
|
|
|
trace "Calling timeout handler", s
|
2024-03-05 07:06:27 +00:00
|
|
|
await s.timeoutHandler()
|
2020-11-29 12:34:19 +00:00
|
|
|
|
|
|
|
return false
|
|
|
|
|
2024-03-05 07:06:27 +00:00
|
|
|
proc timeoutMonitor(s: Connection) {.async: (raises: []).} =
|
2020-11-01 22:23:26 +00:00
|
|
|
## monitor the channel for inactivity
|
2020-08-04 13:22:05 +00:00
|
|
|
##
|
|
|
|
## if the timeout was hit, it means that
|
|
|
|
## neither incoming nor outgoing activity
|
|
|
|
## has been detected and the channel will
|
|
|
|
## be reset
|
|
|
|
##
|
|
|
|
|
2020-11-29 12:34:19 +00:00
|
|
|
while true:
|
|
|
|
try: # Sleep at least once!
|
2020-08-04 13:22:05 +00:00
|
|
|
await sleepAsync(s.timeout)
|
2020-11-29 12:34:19 +00:00
|
|
|
except CancelledError:
|
|
|
|
return
|
2020-08-04 13:22:05 +00:00
|
|
|
|
2020-11-29 12:34:19 +00:00
|
|
|
if not await s.pollActivity():
|
|
|
|
return
|
2020-08-04 13:22:05 +00:00
|
|
|
|
2022-08-01 12:31:22 +00:00
|
|
|
method getWrapped*(s: Connection): Connection {.base.} =
|
2024-03-05 07:06:27 +00:00
|
|
|
raiseAssert("Not implemented!")
|
2022-08-01 12:31:22 +00:00
|
|
|
|
2023-03-08 11:30:19 +00:00
|
|
|
when defined(libp2p_agents_metrics):
|
|
|
|
proc setShortAgent*(s: Connection, shortAgent: string) =
|
|
|
|
var conn = s
|
2024-03-05 07:06:27 +00:00
|
|
|
while conn != nil:
|
2023-03-08 11:30:19 +00:00
|
|
|
conn.shortAgent = shortAgent
|
|
|
|
conn = conn.getWrapped()
|
|
|
|
|
2024-03-05 07:06:27 +00:00
|
|
|
proc new*(
|
|
|
|
C: type Connection,
|
|
|
|
peerId: PeerId,
|
|
|
|
dir: Direction,
|
|
|
|
observedAddr: Opt[MultiAddress],
|
|
|
|
timeout: Duration = DefaultConnectionTimeout,
|
|
|
|
timeoutHandler: TimeoutHandler = nil,
|
|
|
|
): Connection =
|
2021-09-08 09:07:46 +00:00
|
|
|
result = C(
|
|
|
|
peerId: peerId,
|
2020-08-04 13:22:05 +00:00
|
|
|
dir: dir,
|
|
|
|
timeout: timeout,
|
2020-11-25 19:34:48 +00:00
|
|
|
timeoutHandler: timeoutHandler,
|
|
|
|
observedAddr: observedAddr,
|
|
|
|
)
|
2020-08-04 13:22:05 +00:00
|
|
|
|
|
|
|
result.initStream()
|