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()
|
2020-08-10 22:17:11 +00:00
|
|
|
|
2020-08-04 13:22:05 +00:00
|
|
|
doAssert(isNil(s.timerTaskFut))
|
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()
|
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
|
|
|
if isNil(s.timeoutHandler):
|
|
|
|
s.timeoutHandler = proc(): Future[void] = s.close()
|
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
|
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
|
|
|
except CatchableError as exc: # Shouldn't happen
|
|
|
|
warn "exception in timeout", s, exc = exc.msg
|
|
|
|
finally:
|
|
|
|
s.timerTaskFut = nil
|
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()
|