2019-09-01 21:51:39 +00:00
|
|
|
## Nim-LibP2P
|
2019-09-24 17:48:23 +00:00
|
|
|
## Copyright (c) 2019 Status Research & Development GmbH
|
2019-09-01 21:51:39 +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-01-07 08:02:37 +00:00
|
|
|
## This module implements an asynchronous buffer stream
|
2019-12-04 04:44:54 +00:00
|
|
|
## which emulates physical async IO.
|
2019-09-02 04:42:29 +00:00
|
|
|
##
|
2020-01-07 08:02:37 +00:00
|
|
|
## The stream is based on the standard library's `Deque`,
|
|
|
|
## which is itself based on a ring buffer.
|
2019-09-02 04:42:29 +00:00
|
|
|
##
|
2020-01-07 08:02:37 +00:00
|
|
|
## It works by exposing a regular LPStream interface and
|
|
|
|
## a method ``pushTo`` to push data to the internal read
|
2020-06-27 17:33:34 +00:00
|
|
|
## buffer; as well as a handler that can be registered
|
2020-01-07 08:02:37 +00:00
|
|
|
## that gets triggered on every write to the stream. This
|
|
|
|
## allows using the buffered stream as a sort of proxy,
|
|
|
|
## which can be consumed as a regular LPStream but allows
|
|
|
|
## injecting data for reads and intercepting writes.
|
|
|
|
##
|
|
|
|
## Another notable feature is that the stream is fully
|
|
|
|
## ordered and asynchronous. Reads are queued up in order
|
2019-09-02 04:42:29 +00:00
|
|
|
## and are suspended when not enough data available. This
|
2020-01-07 08:02:37 +00:00
|
|
|
## allows preserving backpressure while maintaining full
|
2020-06-27 17:33:34 +00:00
|
|
|
## asynchrony. Both writing to the internal buffer with
|
2020-01-07 08:02:37 +00:00
|
|
|
## ``pushTo`` as well as reading with ``read*` methods,
|
|
|
|
## will suspend until either the amount of elements in the
|
2019-09-03 20:41:38 +00:00
|
|
|
## buffer goes below ``maxSize`` or more data becomes available.
|
2019-09-02 04:42:29 +00:00
|
|
|
|
2020-05-23 17:08:39 +00:00
|
|
|
import deques, math
|
2020-04-14 13:27:07 +00:00
|
|
|
import chronos, chronicles, metrics
|
2020-06-19 17:29:43 +00:00
|
|
|
import ../stream/connection
|
2019-09-01 21:51:39 +00:00
|
|
|
|
2020-05-23 17:08:39 +00:00
|
|
|
when chronicles.enabledLogLevel == LogLevel.TRACE:
|
|
|
|
import oids
|
|
|
|
|
2020-06-19 17:29:43 +00:00
|
|
|
export connection
|
2020-05-06 16:31:47 +00:00
|
|
|
|
2020-05-20 00:14:15 +00:00
|
|
|
logScope:
|
2020-06-10 08:48:01 +00:00
|
|
|
topics = "bufferstream"
|
2019-09-06 21:28:54 +00:00
|
|
|
|
2020-05-20 00:14:15 +00:00
|
|
|
const
|
|
|
|
DefaultBufferSize* = 1024
|
2019-09-01 21:51:39 +00:00
|
|
|
|
2020-05-20 00:14:15 +00:00
|
|
|
const
|
|
|
|
BufferStreamTrackerName* = "libp2p.bufferstream"
|
2019-12-04 04:44:54 +00:00
|
|
|
|
2020-05-20 00:14:15 +00:00
|
|
|
type
|
2020-04-21 01:24:42 +00:00
|
|
|
BufferStreamTracker* = ref object of TrackerBase
|
|
|
|
opened*: uint64
|
|
|
|
closed*: uint64
|
|
|
|
|
|
|
|
proc setupBufferStreamTracker(): BufferStreamTracker {.gcsafe.}
|
|
|
|
|
|
|
|
proc getBufferStreamTracker(): BufferStreamTracker {.gcsafe.} =
|
|
|
|
result = cast[BufferStreamTracker](getTracker(BufferStreamTrackerName))
|
|
|
|
if isNil(result):
|
|
|
|
result = setupBufferStreamTracker()
|
|
|
|
|
|
|
|
proc dumpTracking(): string {.gcsafe.} =
|
|
|
|
var tracker = getBufferStreamTracker()
|
|
|
|
result = "Opened buffers: " & $tracker.opened & "\n" &
|
|
|
|
"Closed buffers: " & $tracker.closed
|
|
|
|
|
|
|
|
proc leakTransport(): bool {.gcsafe.} =
|
|
|
|
var tracker = getBufferStreamTracker()
|
|
|
|
result = (tracker.opened != tracker.closed)
|
|
|
|
|
|
|
|
proc setupBufferStreamTracker(): BufferStreamTracker =
|
|
|
|
result = new BufferStreamTracker
|
|
|
|
result.opened = 0
|
|
|
|
result.closed = 0
|
|
|
|
result.dump = dumpTracking
|
|
|
|
result.isLeaked = leakTransport
|
|
|
|
addTracker(BufferStreamTrackerName, result)
|
2020-05-20 00:14:15 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
# TODO: figure out how to make this generic to avoid casts
|
|
|
|
WriteHandler* = proc (data: seq[byte]): Future[void] {.gcsafe.}
|
|
|
|
|
2020-06-19 17:29:43 +00:00
|
|
|
BufferStream* = ref object of Connection
|
2020-05-20 00:14:15 +00:00
|
|
|
maxSize*: int # buffer's max size in bytes
|
|
|
|
readBuf: Deque[byte] # this is a ring buffer based dequeue
|
|
|
|
readReqs*: Deque[Future[void]] # use dequeue to fire reads in order
|
|
|
|
dataReadEvent*: AsyncEvent # event triggered when data has been consumed from the internal buffer
|
|
|
|
writeHandler*: WriteHandler # user provided write callback
|
|
|
|
writeLock*: AsyncLock # write lock to guarantee ordered writes
|
|
|
|
lock: AsyncLock # pushTo lock to guarantee ordered reads
|
|
|
|
piped: BufferStream # a piped bufferstream instance
|
|
|
|
|
|
|
|
AlreadyPipedError* = object of CatchableError
|
|
|
|
NotWritableError* = object of CatchableError
|
2020-04-14 13:27:07 +00:00
|
|
|
|
2020-05-23 17:08:39 +00:00
|
|
|
proc newAlreadyPipedError*(): ref CatchableError {.inline.} =
|
2019-12-04 04:44:54 +00:00
|
|
|
result = newException(AlreadyPipedError, "stream already piped")
|
|
|
|
|
2020-05-23 17:08:39 +00:00
|
|
|
proc newNotWritableError*(): ref CatchableError {.inline.} =
|
2019-12-04 04:44:54 +00:00
|
|
|
result = newException(NotWritableError, "stream is not writable")
|
|
|
|
|
|
|
|
proc requestReadBytes(s: BufferStream): Future[void] =
|
2020-01-07 08:02:37 +00:00
|
|
|
## create a future that will complete when more
|
2019-09-01 21:51:39 +00:00
|
|
|
## data becomes available in the read buffer
|
2019-09-05 03:02:22 +00:00
|
|
|
result = newFuture[void]()
|
2019-09-01 21:51:39 +00:00
|
|
|
s.readReqs.addLast(result)
|
2020-05-23 17:08:39 +00:00
|
|
|
# trace "requestReadBytes(): added a future to readReqs", oid = s.oid
|
|
|
|
|
2020-06-19 17:29:43 +00:00
|
|
|
method initStream*(s: BufferStream) =
|
|
|
|
if s.objName.len == 0:
|
|
|
|
s.objName = "BufferStream"
|
2020-05-23 17:08:39 +00:00
|
|
|
|
2020-06-19 17:29:43 +00:00
|
|
|
procCall Connection(s).initStream()
|
2020-05-23 17:08:39 +00:00
|
|
|
inc getBufferStreamTracker().opened
|
2019-09-01 21:51:39 +00:00
|
|
|
|
2019-12-04 04:44:54 +00:00
|
|
|
proc initBufferStream*(s: BufferStream,
|
|
|
|
handler: WriteHandler = nil,
|
|
|
|
size: int = DefaultBufferSize) =
|
2020-05-23 17:08:39 +00:00
|
|
|
s.initStream()
|
|
|
|
|
2019-09-04 01:40:54 +00:00
|
|
|
s.maxSize = if isPowerOfTwo(size): size else: nextPowerOfTwo(size)
|
|
|
|
s.readBuf = initDeque[byte](s.maxSize)
|
2019-09-05 03:02:22 +00:00
|
|
|
s.readReqs = initDeque[Future[void]]()
|
2019-09-04 01:40:54 +00:00
|
|
|
s.dataReadEvent = newAsyncEvent()
|
2019-12-04 04:44:54 +00:00
|
|
|
s.lock = newAsyncLock()
|
2020-05-20 00:14:15 +00:00
|
|
|
s.writeLock = newAsyncLock()
|
|
|
|
|
|
|
|
if not(isNil(handler)):
|
|
|
|
s.writeHandler = proc (data: seq[byte]) {.async, gcsafe.} =
|
2020-06-29 15:15:31 +00:00
|
|
|
defer:
|
2020-05-20 00:14:15 +00:00
|
|
|
s.writeLock.release()
|
|
|
|
|
2020-06-29 15:15:31 +00:00
|
|
|
# Using a lock here to guarantee
|
|
|
|
# proper write ordering. This is
|
|
|
|
# specially important when
|
|
|
|
# implementing half-closed in mplex
|
|
|
|
# or other functionality that requires
|
|
|
|
# strict message ordering
|
|
|
|
await s.writeLock.acquire()
|
|
|
|
await handler(data)
|
|
|
|
|
|
|
|
trace "created bufferstream", oid = $s.oid
|
2019-09-04 01:40:54 +00:00
|
|
|
|
2019-12-04 04:44:54 +00:00
|
|
|
proc newBufferStream*(handler: WriteHandler = nil,
|
|
|
|
size: int = DefaultBufferSize): BufferStream =
|
2019-09-01 21:51:39 +00:00
|
|
|
new result
|
2019-09-04 01:40:54 +00:00
|
|
|
result.initBufferStream(handler, size)
|
2019-09-01 21:51:39 +00:00
|
|
|
|
2019-12-04 04:44:54 +00:00
|
|
|
proc popFirst*(s: BufferStream): byte =
|
2019-09-01 21:51:39 +00:00
|
|
|
result = s.readBuf.popFirst()
|
|
|
|
s.dataReadEvent.fire()
|
|
|
|
|
|
|
|
proc popLast*(s: BufferStream): byte =
|
|
|
|
result = s.readBuf.popLast()
|
|
|
|
s.dataReadEvent.fire()
|
|
|
|
|
|
|
|
proc shrink(s: BufferStream, fromFirst = 0, fromLast = 0) =
|
|
|
|
s.readBuf.shrink(fromFirst, fromLast)
|
|
|
|
s.dataReadEvent.fire()
|
|
|
|
|
|
|
|
proc len*(s: BufferStream): int = s.readBuf.len
|
|
|
|
|
2020-05-20 00:14:15 +00:00
|
|
|
method pushTo*(s: BufferStream, data: seq[byte]) {.base, async.} =
|
2020-01-07 08:02:37 +00:00
|
|
|
## Write bytes to internal read buffer, use this to fill up the
|
2019-09-01 21:51:39 +00:00
|
|
|
## buffer with data.
|
|
|
|
##
|
2020-01-07 08:02:37 +00:00
|
|
|
## This method is async and will wait until all data has been
|
2019-09-01 21:51:39 +00:00
|
|
|
## written to the internal buffer; this is done so that backpressure
|
|
|
|
## is preserved.
|
2019-12-04 04:44:54 +00:00
|
|
|
##
|
|
|
|
|
2020-05-20 00:14:15 +00:00
|
|
|
if s.atEof:
|
|
|
|
raise newLPStreamEOFError()
|
2020-04-14 13:21:16 +00:00
|
|
|
|
2020-06-29 15:15:31 +00:00
|
|
|
defer:
|
2020-06-27 17:33:34 +00:00
|
|
|
# trace "ended", size = s.len
|
2020-02-12 14:39:32 +00:00
|
|
|
s.lock.release()
|
2019-09-01 21:51:39 +00:00
|
|
|
|
2020-06-29 15:15:31 +00:00
|
|
|
await s.lock.acquire()
|
|
|
|
var index = 0
|
|
|
|
while not s.closed():
|
|
|
|
while index < data.len and s.readBuf.len < s.maxSize:
|
|
|
|
s.readBuf.addLast(data[index])
|
|
|
|
inc(index)
|
|
|
|
# trace "pushTo()", msg = "added " & $s.len & " bytes to readBuf", oid = s.oid
|
|
|
|
|
|
|
|
# resolve the next queued read request
|
|
|
|
if s.readReqs.len > 0:
|
|
|
|
s.readReqs.popFirst().complete()
|
|
|
|
# trace "pushTo(): completed a readReqs future", oid = s.oid
|
|
|
|
|
|
|
|
if index >= data.len:
|
|
|
|
return
|
|
|
|
|
|
|
|
# if we couldn't transfer all the data to the
|
|
|
|
# internal buf wait on a read event
|
|
|
|
await s.dataReadEvent.wait()
|
|
|
|
s.dataReadEvent.clear()
|
|
|
|
|
2020-07-12 16:37:10 +00:00
|
|
|
proc drainBuffer*(s: BufferStream) {.async.} =
|
|
|
|
## wait for all data in the buffer to be consumed
|
|
|
|
##
|
|
|
|
|
|
|
|
trace "draining buffer", len = s.len
|
|
|
|
while s.len > 0:
|
|
|
|
await s.dataReadEvent.wait()
|
|
|
|
s.dataReadEvent.clear()
|
|
|
|
|
2019-09-04 06:40:53 +00:00
|
|
|
method readOnce*(s: BufferStream,
|
|
|
|
pbytes: pointer,
|
|
|
|
nbytes: int):
|
2019-12-04 04:44:54 +00:00
|
|
|
Future[int] {.async.} =
|
2020-05-20 00:14:15 +00:00
|
|
|
if s.atEof:
|
|
|
|
raise newLPStreamEOFError()
|
|
|
|
|
2020-06-27 17:33:34 +00:00
|
|
|
if s.len() == 0:
|
2019-09-05 03:02:22 +00:00
|
|
|
await s.requestReadBytes()
|
2019-12-04 04:44:54 +00:00
|
|
|
|
2020-06-27 17:33:34 +00:00
|
|
|
var index = 0
|
|
|
|
var size = min(nbytes, s.len)
|
|
|
|
let output = cast[ptr UncheckedArray[byte]](pbytes)
|
2020-08-04 13:22:05 +00:00
|
|
|
|
|
|
|
s.activity = true # reset activity flag
|
2020-06-27 17:33:34 +00:00
|
|
|
while s.len() > 0 and index < size:
|
|
|
|
output[index] = s.popFirst()
|
|
|
|
inc(index)
|
|
|
|
|
|
|
|
return size
|
2019-09-01 21:51:39 +00:00
|
|
|
|
2020-05-20 00:14:15 +00:00
|
|
|
method write*(s: BufferStream, msg: seq[byte]) {.async.} =
|
2019-09-02 04:42:29 +00:00
|
|
|
## Write sequence of bytes ``sbytes`` of length ``msglen`` to writer
|
|
|
|
## stream ``wstream``.
|
|
|
|
##
|
|
|
|
## Sequence of bytes ``sbytes`` must not be zero-length.
|
|
|
|
##
|
|
|
|
## If ``msglen < 0`` whole sequence ``sbytes`` will be writen to stream.
|
|
|
|
## If ``msglen > len(sbytes)`` only ``len(sbytes)`` bytes will be written to
|
|
|
|
## stream.
|
2019-12-04 04:44:54 +00:00
|
|
|
##
|
2020-05-20 00:14:15 +00:00
|
|
|
|
|
|
|
if s.closed:
|
|
|
|
raise newLPStreamClosedError()
|
|
|
|
|
2020-03-04 19:45:14 +00:00
|
|
|
if isNil(s.writeHandler):
|
2020-05-20 00:14:15 +00:00
|
|
|
raise newNotWritableError()
|
2020-03-04 19:45:14 +00:00
|
|
|
|
2020-08-04 13:22:05 +00:00
|
|
|
s.activity = true # reset activity flag
|
2020-05-20 00:14:15 +00:00
|
|
|
await s.writeHandler(msg)
|
2019-12-04 04:44:54 +00:00
|
|
|
|
2020-06-27 17:33:34 +00:00
|
|
|
# TODO: move pipe routines out
|
2019-12-04 04:44:54 +00:00
|
|
|
proc pipe*(s: BufferStream,
|
|
|
|
target: BufferStream): BufferStream =
|
2020-01-07 08:02:37 +00:00
|
|
|
## pipe the write end of this stream to
|
2019-12-04 04:44:54 +00:00
|
|
|
## be the source of the target stream
|
|
|
|
##
|
|
|
|
## Note that this only works with the LPStream
|
2020-01-07 08:02:37 +00:00
|
|
|
## interface methods `read*` and `write` are
|
2019-12-04 04:44:54 +00:00
|
|
|
## piped.
|
|
|
|
##
|
2020-05-20 00:14:15 +00:00
|
|
|
if not(isNil(s.piped)):
|
2019-12-04 04:44:54 +00:00
|
|
|
raise newAlreadyPipedError()
|
|
|
|
|
2020-05-20 00:14:15 +00:00
|
|
|
s.piped = target
|
2019-12-04 04:44:54 +00:00
|
|
|
let oldHandler = target.writeHandler
|
2020-03-24 07:48:05 +00:00
|
|
|
proc handler(data: seq[byte]) {.async, closure, gcsafe.} =
|
2019-12-04 04:44:54 +00:00
|
|
|
if not isNil(oldHandler):
|
|
|
|
await oldHandler(data)
|
2019-09-01 21:51:39 +00:00
|
|
|
|
2020-01-07 08:02:37 +00:00
|
|
|
# if we're piping to self,
|
|
|
|
# then add the data to the
|
2019-12-04 04:44:54 +00:00
|
|
|
# buffer directly and fire
|
|
|
|
# the read event
|
|
|
|
if s == target:
|
|
|
|
for b in data:
|
|
|
|
s.readBuf.addLast(b)
|
|
|
|
|
2020-01-07 08:02:37 +00:00
|
|
|
# notify main loop of available
|
2019-12-04 04:44:54 +00:00
|
|
|
# data
|
|
|
|
s.dataReadEvent.fire()
|
|
|
|
else:
|
|
|
|
await target.pushTo(data)
|
|
|
|
|
|
|
|
s.writeHandler = handler
|
|
|
|
result = target
|
|
|
|
|
|
|
|
proc `|`*(s: BufferStream, target: BufferStream): BufferStream =
|
|
|
|
## pipe operator to make piping less verbose
|
|
|
|
pipe(s, target)
|
|
|
|
|
2020-05-20 00:14:15 +00:00
|
|
|
method close*(s: BufferStream) {.async, gcsafe.} =
|
2020-05-23 17:08:39 +00:00
|
|
|
try:
|
|
|
|
## close the stream and clear the buffer
|
|
|
|
if not s.isClosed:
|
2020-07-17 15:36:48 +00:00
|
|
|
trace "closing bufferstream", oid = $s.oid
|
2020-06-27 17:33:34 +00:00
|
|
|
s.isEof = true
|
2020-05-23 17:08:39 +00:00
|
|
|
for r in s.readReqs:
|
|
|
|
if not(isNil(r)) and not(r.finished()):
|
|
|
|
r.fail(newLPStreamEOFError())
|
|
|
|
s.dataReadEvent.fire()
|
|
|
|
s.readBuf.clear()
|
|
|
|
|
2020-06-19 17:29:43 +00:00
|
|
|
await procCall Connection(s).close()
|
2020-05-23 17:08:39 +00:00
|
|
|
inc getBufferStreamTracker().closed
|
2020-06-29 15:15:31 +00:00
|
|
|
trace "bufferstream closed", oid = $s.oid
|
2020-05-23 17:08:39 +00:00
|
|
|
else:
|
|
|
|
trace "attempt to close an already closed bufferstream", trace = getStackTrace()
|
2020-06-29 15:15:31 +00:00
|
|
|
except CancelledError as exc:
|
|
|
|
raise
|
2020-05-23 17:08:39 +00:00
|
|
|
except CatchableError as exc:
|
|
|
|
trace "error closing buffer stream", exc = exc.msg
|