nim-libp2p/libp2p/stream/connection.nim

154 lines
4.3 KiB
Nim
Raw Normal View History

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.
2023-06-07 11:12:49 +00:00
{.push raises: [].}
import std/[hashes, oids, strformat]
import stew/results
2020-08-02 10:22:49 +00:00
import chronicles, chronos, metrics
import lpstream,
../multiaddress,
2021-01-29 16:14:53 +00:00
../peerinfo,
../errors
export lpstream, peerinfo, errors, results
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
const
ConnectionTrackerName* = "Connection"
DefaultConnectionTimeout* = 5.minutes
type
2023-06-07 11:12:49 +00:00
TimeoutHandler* = proc(): Future[void] {.gcsafe, raises: [].}
Connection* = ref object of LPStream
activity*: bool # reset every time data is sent or received
timeout*: Duration # channel timeout if no activity
timerTaskFut: Future[void] # the current timer instance
timeoutHandler*: TimeoutHandler # timeout handler
peerId*: PeerId
observedAddr*: Opt[MultiAddress]
2022-08-01 12:31:22 +00:00
protocol*: string # protocol used by the connection, used as tag for metrics
transportDir*: Direction # The bottom level transport (generally the socket) direction
when defined(libp2p_agents_metrics):
shortAgent*: string
proc timeoutMonitor(s: Connection) {.async, gcsafe.}
func shortLog*(conn: Connection): string =
try:
if conn.isNil: "Connection(nil)"
else: &"{shortLog(conn.peerId)}:{conn.oid}"
except ValueError as exc:
raiseAssert(exc.msg)
chronicles.formatIt(Connection): shortLog(it)
method initStream*(s: Connection) =
if s.objName.len == 0:
s.objName = ConnectionTrackerName
procCall LPStream(s).initStream()
doAssert(isNil(s.timerTaskFut))
if s.timeout > 0.millis:
trace "Monitoring for timeout", s, timeout = s.timeout
s.timerTaskFut = s.timeoutMonitor()
if isNil(s.timeoutHandler):
s.timeoutHandler = proc(): Future[void] =
trace "Idle timeout expired, closing connection", s
s.close()
method closeImpl*(s: Connection): Future[void] =
# Cleanup timeout timer
trace "Closing connection", s
if not isNil(s.timerTaskFut) and not s.timerTaskFut.finished:
s.timerTaskFut.cancel()
s.timerTaskFut = nil
trace "Closed connection", s
procCall LPStream(s).closeImpl()
func hash*(p: Connection): Hash =
cast[pointer](p).hash
proc pollActivity(s: Connection): Future[bool] {.async.} =
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
if not(isNil(s.timeoutHandler)):
trace "Calling timeout handler", s
try:
await s.timeoutHandler()
except CancelledError:
# timeoutHandler is expected to be fast, but it's still possible that
# cancellation will happen here - no need to warn about it - we do want to
# stop the polling however
debug "Timeout handler cancelled", s
except CatchableError as exc: # Shouldn't happen
warn "exception in timeout handler", s, exc = exc.msg
return false
proc timeoutMonitor(s: Connection) {.async, gcsafe.} =
## monitor the channel for inactivity
##
## if the timeout was hit, it means that
## neither incoming nor outgoing activity
## has been detected and the channel will
## be reset
##
while true:
try: # Sleep at least once!
await sleepAsync(s.timeout)
except CancelledError:
return
if not await s.pollActivity():
return
2022-08-01 12:31:22 +00:00
method getWrapped*(s: Connection): Connection {.base.} =
doAssert(false, "not implemented!")
2023-03-08 11:30:19 +00:00
when defined(libp2p_agents_metrics):
proc setShortAgent*(s: Connection, shortAgent: string) =
var conn = s
while not isNil(conn):
conn.shortAgent = shortAgent
conn = conn.getWrapped()
proc new*(C: type Connection,
peerId: PeerId,
dir: Direction,
observedAddr: Opt[MultiAddress],
timeout: Duration = DefaultConnectionTimeout,
timeoutHandler: TimeoutHandler = nil): Connection =
result = C(peerId: peerId,
dir: dir,
timeout: timeout,
timeoutHandler: timeoutHandler,
observedAddr: observedAddr)
result.initStream()