mirror of
https://github.com/vacp2p/nim-libp2p-experimental.git
synced 2025-01-28 11:05:09 +00:00
96d4c44fec
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
57 lines
1.5 KiB
Nim
57 lines
1.5 KiB
Nim
import chronos, bearssl
|
|
|
|
import ../libp2p/transports/tcptransport
|
|
import ../libp2p/stream/bufferstream
|
|
import ../libp2p/crypto/crypto
|
|
import ../libp2p/stream/lpstream
|
|
|
|
const
|
|
StreamTransportTrackerName = "stream.transport"
|
|
StreamServerTrackerName = "stream.server"
|
|
|
|
trackerNames = [
|
|
# ConnectionTrackerName,
|
|
BufferStreamTrackerName,
|
|
TcpTransportTrackerName,
|
|
StreamTransportTrackerName,
|
|
StreamServerTrackerName
|
|
]
|
|
|
|
iterator testTrackers*(extras: openArray[string] = []): TrackerBase =
|
|
for name in trackerNames:
|
|
let t = getTracker(name)
|
|
if not isNil(t): yield t
|
|
for name in extras:
|
|
let t = getTracker(name)
|
|
if not isNil(t): yield t
|
|
|
|
type RngWrap = object
|
|
rng: ref BrHmacDrbgContext
|
|
|
|
var rngVar: RngWrap
|
|
|
|
proc getRng(): ref BrHmacDrbgContext =
|
|
# TODO if `rngVar` is a threadvar like it should be, there are random and
|
|
# spurious compile failures on mac - this is not gcsafe but for the
|
|
# purpose of the tests, it's ok as long as we only use a single thread
|
|
{.gcsafe.}:
|
|
if rngVar.rng.isNil:
|
|
rngVar.rng = newRng()
|
|
rngVar.rng
|
|
|
|
template rng*(): ref BrHmacDrbgContext =
|
|
getRng()
|
|
|
|
type
|
|
WriteHandler* = proc(data: seq[byte]): Future[void] {.gcsafe.}
|
|
TestBufferStream* = ref object of BufferStream
|
|
writeHandler*: WriteHandler
|
|
|
|
method write*(s: TestBufferStream, msg: seq[byte]): Future[void] =
|
|
s.writeHandler(msg)
|
|
|
|
proc newBufferStream*(writeHandler: WriteHandler): TestBufferStream =
|
|
new result
|
|
result.writeHandler = writeHandler
|
|
result.initStream()
|