2020-06-19 17:29:43 +00:00
|
|
|
## Nim-LibP2P
|
|
|
|
## Copyright (c) 2020 Status Research & Development GmbH
|
|
|
|
## 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-09-06 08:31:47 +00:00
|
|
|
import std/[hashes, oids, strformat]
|
2020-08-02 10:22:49 +00:00
|
|
|
import chronicles, chronos, metrics
|
2020-06-19 17:29:43 +00:00
|
|
|
import lpstream,
|
|
|
|
../multiaddress,
|
|
|
|
../peerinfo
|
|
|
|
|
2020-09-06 08:31:47 +00:00
|
|
|
export lpstream, peerinfo
|
2020-06-19 17:29:43 +00:00
|
|
|
|
2020-08-02 10:22:49 +00:00
|
|
|
logScope:
|
|
|
|
topics = "connection"
|
|
|
|
|
2020-06-19 17:29:43 +00:00
|
|
|
const
|
|
|
|
ConnectionTrackerName* = "libp2p.connection"
|
2020-08-10 22:17:11 +00:00
|
|
|
DefaultConnectionTimeout* = 5.minutes
|
2020-06-19 17:29:43 +00:00
|
|
|
|
|
|
|
type
|
2020-08-04 13:22:05 +00:00
|
|
|
TimeoutHandler* = proc(): Future[void] {.gcsafe.}
|
|
|
|
|
2020-07-17 15:36:48 +00:00
|
|
|
Direction* {.pure.} = enum
|
|
|
|
None, In, Out
|
|
|
|
|
2020-06-19 17:29:43 +00:00
|
|
|
Connection* = ref object of LPStream
|
2020-08-04 13:22:05 +00:00
|
|
|
activity*: bool # reset every time data is sent or received
|
|
|
|
timeout*: Duration # channel timeout if no activity
|
2020-09-09 17:12:08 +00:00
|
|
|
timerTaskFut: Future[void] # the current timer instance
|
2020-08-04 13:22:05 +00:00
|
|
|
timeoutHandler*: TimeoutHandler # timeout handler
|
2020-06-19 17:29:43 +00:00
|
|
|
peerInfo*: PeerInfo
|
|
|
|
observedAddr*: Multiaddress
|
2020-07-17 15:36:48 +00:00
|
|
|
dir*: Direction
|
2020-06-19 17:29:43 +00:00
|
|
|
|
|
|
|
ConnectionTracker* = ref object of TrackerBase
|
|
|
|
opened*: uint64
|
|
|
|
closed*: uint64
|
|
|
|
|
|
|
|
proc setupConnectionTracker(): ConnectionTracker {.gcsafe.}
|
2020-08-04 13:22:05 +00:00
|
|
|
proc timeoutMonitor(s: Connection) {.async, gcsafe.}
|
2020-06-19 17:29:43 +00:00
|
|
|
|
|
|
|
proc getConnectionTracker*(): ConnectionTracker {.gcsafe.} =
|
|
|
|
result = cast[ConnectionTracker](getTracker(ConnectionTrackerName))
|
|
|
|
if isNil(result):
|
|
|
|
result = setupConnectionTracker()
|
|
|
|
|
|
|
|
proc dumpTracking(): string {.gcsafe.} =
|
|
|
|
var tracker = getConnectionTracker()
|
|
|
|
result = "Opened conns: " & $tracker.opened & "\n" &
|
|
|
|
"Closed conns: " & $tracker.closed
|
|
|
|
|
|
|
|
proc leakTransport(): bool {.gcsafe.} =
|
|
|
|
var tracker = getConnectionTracker()
|
|
|
|
result = (tracker.opened != tracker.closed)
|
|
|
|
|
|
|
|
proc setupConnectionTracker(): ConnectionTracker =
|
|
|
|
result = new ConnectionTracker
|
|
|
|
result.opened = 0
|
|
|
|
result.closed = 0
|
|
|
|
result.dump = dumpTracking
|
|
|
|
result.isLeaked = leakTransport
|
|
|
|
addTracker(ConnectionTrackerName, result)
|
|
|
|
|
2020-09-06 08:31:47 +00:00
|
|
|
func shortLog*(conn: Connection): string =
|
|
|
|
if conn.isNil: "Connection(nil)"
|
|
|
|
elif conn.peerInfo.isNil: $conn.oid
|
|
|
|
else: &"{shortLog(conn.peerInfo.peerId)}:{conn.oid}"
|
|
|
|
chronicles.formatIt(Connection): shortLog(it)
|
|
|
|
|
2020-06-19 17:29:43 +00:00
|
|
|
method initStream*(s: Connection) =
|
|
|
|
if s.objName.len == 0:
|
|
|
|
s.objName = "Connection"
|
|
|
|
|
|
|
|
procCall LPStream(s).initStream()
|
|
|
|
s.closeEvent = newAsyncEvent()
|
2020-08-04 13:22:05 +00:00
|
|
|
|
2020-08-10 22:17:11 +00:00
|
|
|
if isNil(s.timeoutHandler):
|
|
|
|
s.timeoutHandler = proc() {.async.} =
|
|
|
|
await s.close()
|
|
|
|
|
2020-08-04 13:22:05 +00:00
|
|
|
doAssert(isNil(s.timerTaskFut))
|
2020-08-10 22:17:11 +00:00
|
|
|
# doAssert(s.timeout > 0.millis)
|
|
|
|
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()
|
2020-08-04 13:22:05 +00:00
|
|
|
|
2020-06-19 17:29:43 +00:00
|
|
|
inc getConnectionTracker().opened
|
|
|
|
|
|
|
|
method close*(s: Connection) {.async.} =
|
2020-08-04 13:22:05 +00:00
|
|
|
## cleanup timers
|
|
|
|
if not isNil(s.timerTaskFut) and not s.timerTaskFut.finished:
|
|
|
|
s.timerTaskFut.cancel()
|
|
|
|
|
2020-07-17 15:36:48 +00:00
|
|
|
if not s.isClosed:
|
|
|
|
await procCall LPStream(s).close()
|
|
|
|
inc getConnectionTracker().closed
|
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
|
|
|
|
|
|
|
proc timeoutMonitor(s: Connection) {.async, gcsafe.} =
|
|
|
|
## monitor the channel for innactivity
|
|
|
|
##
|
|
|
|
## if the timeout was hit, it means that
|
|
|
|
## neither incoming nor outgoing activity
|
|
|
|
## has been detected and the channel will
|
|
|
|
## be reset
|
|
|
|
##
|
|
|
|
|
|
|
|
try:
|
|
|
|
while true:
|
|
|
|
await sleepAsync(s.timeout)
|
|
|
|
|
|
|
|
if s.closed or s.atEof:
|
|
|
|
return
|
|
|
|
|
|
|
|
if s.activity:
|
|
|
|
s.activity = false
|
|
|
|
continue
|
|
|
|
|
|
|
|
break
|
|
|
|
|
|
|
|
# reset channel on innactivity timeout
|
2020-09-06 08:31:47 +00:00
|
|
|
trace "Connection timed out", s
|
2020-08-04 13:22:05 +00:00
|
|
|
if not(isNil(s.timeoutHandler)):
|
|
|
|
await s.timeoutHandler()
|
|
|
|
|
|
|
|
except CancelledError as exc:
|
|
|
|
raise exc
|
|
|
|
except CatchableError as exc:
|
2020-09-06 08:31:47 +00:00
|
|
|
trace "exception in timeout", exc = exc.msg, s
|
2020-08-04 13:22:05 +00:00
|
|
|
|
|
|
|
proc init*(C: type Connection,
|
|
|
|
peerInfo: PeerInfo,
|
|
|
|
dir: Direction,
|
|
|
|
timeout: Duration = DefaultConnectionTimeout,
|
|
|
|
timeoutHandler: TimeoutHandler = nil): Connection =
|
|
|
|
result = C(peerInfo: peerInfo,
|
|
|
|
dir: dir,
|
|
|
|
timeout: timeout,
|
|
|
|
timeoutHandler: timeoutHandler)
|
|
|
|
|
|
|
|
result.initStream()
|