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-08-10 22:17:11 +00:00
|
|
|
import hashes, oids
|
2020-08-02 10:22:49 +00:00
|
|
|
import chronicles, chronos, metrics
|
2020-06-19 17:29:43 +00:00
|
|
|
import lpstream,
|
|
|
|
../multiaddress,
|
|
|
|
../peerinfo
|
|
|
|
|
|
|
|
export lpstream
|
|
|
|
|
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
|
|
|
|
timerTaskFut: Future[void] # the current timer instanse
|
|
|
|
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)
|
|
|
|
|
|
|
|
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-15 19:50:31 +00:00
|
|
|
trace "timeout set at", timeout = s.timeout.millis
|
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:
|
|
|
|
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
|
|
|
|
|
|
|
proc `$`*(conn: Connection): string =
|
|
|
|
if not isNil(conn.peerInfo):
|
|
|
|
result = conn.peerInfo.id
|
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
|
|
|
|
##
|
|
|
|
|
|
|
|
logScope:
|
|
|
|
oid = $s.oid
|
|
|
|
|
|
|
|
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
|
|
|
|
trace "Connection timed out"
|
|
|
|
if not(isNil(s.timeoutHandler)):
|
|
|
|
await s.timeoutHandler()
|
|
|
|
|
|
|
|
except CancelledError as exc:
|
|
|
|
raise exc
|
|
|
|
except CatchableError as exc:
|
|
|
|
trace "exception in timeout", exc = exc.msg
|
|
|
|
|
|
|
|
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()
|