@statewalker/webrun-streams-libp2p 0.5.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +202 -82
- package/dist/connect-serve.d.ts +114 -67
- package/dist/connect-serve.d.ts.map +1 -1
- package/dist/duplex-over-stream.d.ts +12 -0
- package/dist/duplex-over-stream.d.ts.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +304 -27
- package/dist/legacy-connect-serve.d.ts +116 -0
- package/dist/legacy-connect-serve.d.ts.map +1 -0
- package/dist/port-mux.d.ts +26 -1
- package/dist/port-mux.d.ts.map +1 -1
- package/dist/shared.d.ts +26 -0
- package/dist/shared.d.ts.map +1 -0
- package/dist/stream-gate.d.ts +1 -1
- package/dist/stream-gate.d.ts.map +1 -1
- package/package.json +6 -5
- package/src/connect-serve.ts +337 -342
- package/src/duplex-over-stream.ts +12 -0
- package/src/index.ts +2 -0
- package/src/legacy-connect-serve.ts +425 -0
- package/src/port-mux.ts +60 -9
- package/src/shared.ts +28 -0
- package/src/stream-gate.ts +5 -3
package/README.md
CHANGED
|
@@ -3,12 +3,18 @@
|
|
|
3
3
|
## What it is
|
|
4
4
|
|
|
5
5
|
A libp2p adapter for the `Duplex` seam from `@statewalker/webrun-streams`.
|
|
6
|
-
Each `call(input)`
|
|
7
|
-
`
|
|
8
|
-
|
|
9
|
-
protocol
|
|
10
|
-
additionally tells the handler which peer libp2p
|
|
11
|
-
connection.
|
|
6
|
+
Each `call(input)` is one port of a windowed native port mux: one new libp2p
|
|
7
|
+
`Stream`, negotiating `<protocol>/ports/window`, over which `webrun-rpc`'s
|
|
8
|
+
stream tier runs the call. The serving side handles that protocol and runs a
|
|
9
|
+
handler per call. The default `protocol` is `/webrun-streams/1.0.0`. It targets
|
|
10
|
+
libp2p 3.x. `serveConnections` additionally tells the handler which peer libp2p
|
|
11
|
+
authenticated on the connection.
|
|
12
|
+
|
|
13
|
+
Releases up to 0.6.0 ran each call straight on a stream negotiating `protocol`
|
|
14
|
+
itself, with their own flow control. That protocol is **deprecated** and kept,
|
|
15
|
+
on both ends, for one release: a new server still answers old callers on it,
|
|
16
|
+
and a new caller falls back to it against an old server (see "Compatibility
|
|
17
|
+
with 0.6.0 and older").
|
|
12
18
|
|
|
13
19
|
## Why it exists
|
|
14
20
|
|
|
@@ -20,13 +26,15 @@ natively, there is no multiplexer on top: one call is one real libp2p stream.
|
|
|
20
26
|
|
|
21
27
|
What the adapter adds on top of a raw stream:
|
|
22
28
|
|
|
23
|
-
- **
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
- **
|
|
29
|
-
|
|
29
|
+
- **One flow control, bounded.** The port's credit window (default 4 MiB) is
|
|
30
|
+
the only flow control: up to a window is in flight per round trip, and a
|
|
31
|
+
producer never runs more than about a window ahead of a consumer that stopped
|
|
32
|
+
reading, whatever libp2p buffers (statewalker/webrun-wire#153). It is the
|
|
33
|
+
same stack every other `webrun-rpc` transport runs.
|
|
34
|
+
- **Error fidelity.** A handler's `Error` reaches the caller with `message`,
|
|
35
|
+
`stack` and custom fields.
|
|
36
|
+
- **Cancellation** in both directions: a caller's `.return()` ends the call at
|
|
37
|
+
once, and the handler's `finally` runs.
|
|
30
38
|
- **A guard against a burst of calls on a fresh connection**, which would
|
|
31
39
|
otherwise kill the connection's muxer.
|
|
32
40
|
- **Caller identity** for the serving side, through `serveConnections`.
|
|
@@ -37,7 +45,9 @@ What the adapter adds on top of a raw stream:
|
|
|
37
45
|
pnpm add @statewalker/webrun-streams-libp2p @libp2p/interface @multiformats/multiaddr
|
|
38
46
|
```
|
|
39
47
|
|
|
40
|
-
Runtime
|
|
48
|
+
Runtime dependencies: `@statewalker/webrun-streams`, `@statewalker/webrun-rpc`
|
|
49
|
+
(the port stack `connect`/`serve` run) and `@statewalker/webrun-msgpack` (the
|
|
50
|
+
calls' wire codec). Peer dependencies:
|
|
41
51
|
|
|
42
52
|
| Peer | Range | Required |
|
|
43
53
|
| --- | --- | --- |
|
|
@@ -51,19 +61,21 @@ runs (Node or browser).
|
|
|
51
61
|
|
|
52
62
|
| Export | What it gives |
|
|
53
63
|
| --- | --- |
|
|
54
|
-
| `connect(params)` | `Connect<ConnectLibp2pParams>`. Resolves `{ call, close }` immediately (no dial yet). Each `call(input)`
|
|
55
|
-
| `serve(params, handler)` | `Serve<ServeLibp2pParams>`. `
|
|
56
|
-
| `serveConnections(params, makeHandler)` | Like `serve`, but `makeHandler(context)` is called once per
|
|
57
|
-
| `
|
|
58
|
-
| `
|
|
64
|
+
| `connect(params)` | `Connect<ConnectLibp2pParams>`. Resolves `{ call, close }` immediately (no dial yet). Each `call(input)` opens one port (one stream). `close()` closes every port this `connect` still holds open. |
|
|
65
|
+
| `serve(params, handler)` | `Serve<ServeLibp2pParams>`. Handles `<protocol>/ports` (with and without `/window`) and, deprecated, `protocol`; runs `handler` per call. Resolves to an idempotent teardown that unhandles all of them. |
|
|
66
|
+
| `serveConnections(params, makeHandler)` | Like `serve`, but `makeHandler(context)` is called once per call with `{ remotePeer }`, and returns that call's `Duplex`. |
|
|
67
|
+
| `DEFAULT_WINDOW` | `4194304` (4 MiB), the port window `connect`/`serve` grant unless told otherwise. See "Sizing the window". |
|
|
68
|
+
| `portProtocolOf(protocol)` | `protocol + "/ports"`: what a call's stream negotiates (with `/window` appended between two windowed ends). |
|
|
69
|
+
| `DEFAULT_PROTOCOL` | `"/webrun-streams/1.0.0"`. |
|
|
70
|
+
| `duplexOverStream(stream, input, options?)` | **Deprecated.** The deprecated protocol's primitive: runs one call over one libp2p `Stream`. Removed in the next release. |
|
|
71
|
+
| `PeerGoneError` | What a call fails with when its stream ended before the call finished: the peer died, the connection dropped, or the stream was reset. `cause` is libp2p's close error, if any. |
|
|
59
72
|
| `messageTargetOverStream(stream, options?)` | `{ target, signal }`: one stream as a pipe of `Uint8Array` messages for `multiplexPort` / `overPipe`, and an `AbortSignal` that aborts when the stream is over. `options.maxMessageBytes` caps one message. See below. |
|
|
60
|
-
| `libp2pPortMux(options)` | Native MessagePorts: a `webrun-rpc` `PortMux` (by shape) whose every port is its own libp2p stream, for `overPorts
|
|
73
|
+
| `libp2pPortMux(options)` | Native MessagePorts: a `webrun-rpc` `PortMux` (by shape) whose every port is its own libp2p stream, for `overPorts`; what `connect`/`serve` run on. `options`: `node`, `codec` (a value codec: `{ encode: serialize, decode: deserialize }` from `webrun-msgpack`), `peer` (to open ports), `onPort(port, { remotePeer, connection })` (to accept them), `protocol`, `maxMessageSize`, stream limits, `window`. The port lifecycle (codec wrapping, messages held until a port's first listener, close signals, accept/refuse, the caller's signal, close-all) is the shared `newNativePortMux` from `@statewalker/webrun-streams`; `describePortMux` (`webrun-streams-conformance`) is its contract, run against this transport too. See "MessagePorts over libp2p" below. |
|
|
61
74
|
| `DEFAULT_PORT_PROTOCOL`, `DEFAULT_PORT_MAX_MESSAGE_SIZE` | `"/webrun-ports/1.0.0"`, 256 KiB. |
|
|
62
|
-
| `
|
|
63
|
-
| `
|
|
64
|
-
| `DEFAULT_CLOSE_TIMEOUT_MS` | `300_000` (5 minutes), the bound on a graceful close. |
|
|
75
|
+
| `DEFAULT_DRAIN_TIMEOUT_MS` | **Deprecated** (the deprecated protocol only). `300_000` (5 minutes). |
|
|
76
|
+
| `DEFAULT_CLOSE_TIMEOUT_MS` | **Deprecated** (the deprecated protocol only). `300_000` (5 minutes), the bound on a graceful close. |
|
|
65
77
|
| `DEFAULT_MAX_MESSAGE_BYTES` | `67_108_864` (64 MiB), `messageTargetOverStream`'s cap on one message. |
|
|
66
|
-
| `DEFAULT_MAX_FRAME_BYTES` | `67_108_864` (64 MiB),
|
|
78
|
+
| `DEFAULT_MAX_FRAME_BYTES` | `67_108_864` (64 MiB), the default cap on one inbound frame (`maxFrameBytes`). |
|
|
67
79
|
| `ConnectLibp2pParams`, `ServeLibp2pParams`, `ConnectionContext`, `ServeConnectionsHandler`, `DuplexOverStreamOptions` | Types, below. |
|
|
68
80
|
|
|
69
81
|
`ConnectLibp2pParams`:
|
|
@@ -72,26 +84,32 @@ runs (Node or browser).
|
|
|
72
84
|
| --- | --- | --- | --- |
|
|
73
85
|
| `node` | `Libp2p` | required | The local node. Only `dialProtocol` is required; `getConnections` is used when present. |
|
|
74
86
|
| `peer` | `PeerId \| Multiaddr` | required | Who to dial. |
|
|
75
|
-
| `protocol` | `string` | `DEFAULT_PROTOCOL` |
|
|
76
|
-
| `
|
|
77
|
-
| `
|
|
87
|
+
| `protocol` | `string` | `DEFAULT_PROTOCOL` | The protocol id both ends are configured with. Calls negotiate `portProtocolOf(protocol)`; `protocol` itself is the deprecated fallback. |
|
|
88
|
+
| `window` | `number` | `DEFAULT_WINDOW` (4 MiB) | The port window in bytes, at least 64 KiB: the most this end holds of a response delivered and not yet read. See "Sizing the window". |
|
|
89
|
+
| `timeout` | `number` | none | Per-call inactivity timeout in ms, reset by any chunk in either direction. Unset, a slow consumer is throttled, never failed. |
|
|
90
|
+
| `maxFrameBytes` | `number` | `DEFAULT_MAX_FRAME_BYTES` | The largest frame accepted from the peer. Past it, or on a malformed frame length, the call fails and the stream is aborted. |
|
|
78
91
|
| `maxOutboundStreams` | `number` | libp2p's (64) | Passed to `dialProtocol`. |
|
|
79
92
|
| `runOnLimitedConnection` | `boolean` | unset | Passed to `dialProtocol`. Allows relayed or otherwise limited connections. |
|
|
93
|
+
| `drainTimeoutMs`, `readHighWaterBytes` | `number` | 5 min, 64 KiB | **Deprecated**: the deprecated protocol only (an older server). |
|
|
80
94
|
|
|
81
|
-
`ServeLibp2pParams`: `node`, `protocol`, `
|
|
95
|
+
`ServeLibp2pParams`: `node`, `protocol`, `window`, `timeout`, `maxFrameBytes`,
|
|
82
96
|
`maxOutboundStreams` and `runOnLimitedConnection` as above (passed to
|
|
83
|
-
`node.handle`), plus `maxInboundStreams` (libp2p
|
|
97
|
+
`node.handle` for each served protocol), plus `maxInboundStreams` (libp2p
|
|
98
|
+
default 32), and the deprecated `drainTimeoutMs` and `readHighWaterBytes`
|
|
99
|
+
(older callers only). The server's `window` bounds what a call sends ahead of
|
|
100
|
+
the handler.
|
|
84
101
|
|
|
85
102
|
`ConnectionContext`: `{ remotePeer: PeerId }`.
|
|
86
103
|
`ServeConnectionsHandler`: `(context: ConnectionContext) => Duplex`.
|
|
87
104
|
|
|
88
|
-
`DuplexOverStreamOptions
|
|
105
|
+
`DuplexOverStreamOptions` (**deprecated**, with `duplexOverStream`):
|
|
89
106
|
|
|
90
107
|
| Field | Meaning |
|
|
91
108
|
| --- | --- |
|
|
92
|
-
| `onPeerInputEnd?(err?)` | Called once when the peer's side ends: closed, `ERROR` frame (passed as `err`) or torn down.
|
|
93
|
-
| `onSourceCompleted?()` | Called only when the peer's side ended normally.
|
|
94
|
-
| `drainTimeoutMs?` |
|
|
109
|
+
| `onPeerInputEnd?(err?)` | Called once when the peer's side ends: closed, `ERROR` frame (passed as `err`) or torn down. |
|
|
110
|
+
| `onSourceCompleted?()` | Called only when the peer's side ended normally. |
|
|
111
|
+
| `drainTimeoutMs?` | How long a backpressured send waits for `'drain'`. |
|
|
112
|
+
| `readHighWaterBytes?` | Received bytes held for a consumer that is not reading before the stream is paused. |
|
|
95
113
|
| `maxFrameBytes?` | As above. |
|
|
96
114
|
|
|
97
115
|
## Examples
|
|
@@ -160,7 +178,83 @@ const { call } = await connect({ node, peer, protocol: "/my-app/1.0.0", maxOutbo
|
|
|
160
178
|
|
|
161
179
|
## Internals
|
|
162
180
|
|
|
163
|
-
###
|
|
181
|
+
### A call is a port
|
|
182
|
+
|
|
183
|
+
`connect` builds a `libp2pPortMux` for `portProtocolOf(protocol)` with
|
|
184
|
+
`window` (default `DEFAULT_WINDOW`) and hands it to `webrun-rpc`'s `connect`
|
|
185
|
+
through `overPorts`; `serve` does the same with `webrun-rpc`'s `serve`. So one
|
|
186
|
+
call is one port, one port is one libp2p stream, and the stream tier
|
|
187
|
+
(`duplexOverPort`) runs the call: its chunks (at most 256 KiB each) go as
|
|
188
|
+
msgpack messages, pipelined up to the peer's window, and the window's credit
|
|
189
|
+
is their confirmation. yamux still multiplexes the streams, but its windows no
|
|
190
|
+
longer bound anything this package promises: the port's credit bounds what is
|
|
191
|
+
in flight per call. There is no flow control of this package's own any more;
|
|
192
|
+
the bespoke yamux pause/resume run-ahead bound of the deprecated protocol goes
|
|
193
|
+
with it in the next release.
|
|
194
|
+
|
|
195
|
+
Both ends of a call between two releases from this one on always run the
|
|
196
|
+
window, so a call's stream negotiates `<protocol>/ports/window`; the mux also
|
|
197
|
+
serves (and dials, second) `<protocol>/ports`, unwindowed.
|
|
198
|
+
|
|
199
|
+
### Sizing the window
|
|
200
|
+
|
|
201
|
+
A call moves at most one window per round trip, so its throughput is capped at
|
|
202
|
+
`window / RTT`, and a producer runs at most about one window (plus a chunk at
|
|
203
|
+
each end) ahead of a consumer that stopped reading. Size it at least to the
|
|
204
|
+
link's bandwidth-delay product:
|
|
205
|
+
|
|
206
|
+
| Link | BDP | Window |
|
|
207
|
+
| --- | --- | --- |
|
|
208
|
+
| LAN, 1 Gbit/s at 1 ms | 125 KB | the 64 KiB floor would cap it; 1 MiB is ample |
|
|
209
|
+
| 70 Mbit/s at 11 ms | 96 KB | 1 MiB |
|
|
210
|
+
| 200 Mbit/s at 100 ms | 2.5 MB | the 4 MiB default |
|
|
211
|
+
| 1 Gbit/s at 100 ms | 12.5 MB | 16 MiB |
|
|
212
|
+
|
|
213
|
+
Measured over an emulated 100 ms / 200 Mbit/s link (statewalker/webrun-wire#153):
|
|
214
|
+
a 1 MiB window capped a 16 MiB echo at 7.4 MiB/s, the 4 MiB window reached
|
|
215
|
+
15 MiB/s, as fast as the deprecated protocol. Far above the bottleneck's queue a
|
|
216
|
+
window only adds latency (bufferbloat): on an 11 ms link 4 MiB was slower than
|
|
217
|
+
1 MiB. Both ends set their own window: the caller's bounds the response, the
|
|
218
|
+
server's the request. Memory per call is bounded by the window, so a server
|
|
219
|
+
with many concurrent calls may want a smaller one.
|
|
220
|
+
|
|
221
|
+
### Compatibility with 0.6.0 and older
|
|
222
|
+
|
|
223
|
+
Deprecated shims, on both ends, for one release; removed in the next one:
|
|
224
|
+
|
|
225
|
+
- **New server, old caller.** `serve`/`serveConnections` also handle
|
|
226
|
+
`protocol` itself with the deprecated protocol (`legacy-connect-serve.ts`),
|
|
227
|
+
so an old `connect` keeps working.
|
|
228
|
+
- **New caller, old server.** An old server refuses `<protocol>/ports…`
|
|
229
|
+
(libp2p's `UnsupportedProtocolError`, before any of the call's input is
|
|
230
|
+
read), and the call runs on `protocol` instead. The node remembers the
|
|
231
|
+
refusal per peer and protocol for that connection, so later calls, from any
|
|
232
|
+
`connect()` on the node, dial `protocol` directly; a new connection (the peer
|
|
233
|
+
restarted, perhaps upgraded) is probed again. The first call to an old server
|
|
234
|
+
costs one extra protocol negotiation.
|
|
235
|
+
|
|
236
|
+
`tests/interop-0-6.test.ts` runs the conformance suite (L0–L6) and the cases
|
|
237
|
+
above against the published 0.6.0 in both directions.
|
|
238
|
+
|
|
239
|
+
Behaviour that differs from the deprecated protocol:
|
|
240
|
+
|
|
241
|
+
- **A call ends with the peer's output.** When the handler's output ends, the
|
|
242
|
+
call ends: input the caller has not sent yet is abandoned, and a handler
|
|
243
|
+
still reading its input is told the caller abandoned the stream. The
|
|
244
|
+
deprecated protocol kept sending until the input ended too.
|
|
245
|
+
- **No drain timeout.** A peer that stops reading holds at most a window per
|
|
246
|
+
call; set `timeout` to fail idle calls. `drainTimeoutMs` and
|
|
247
|
+
`readHighWaterBytes` apply to the deprecated protocol only.
|
|
248
|
+
- **Chunks.** A response arrives in chunks of up to 256 KiB (64 KiB before).
|
|
249
|
+
- **Failures on the serving side are not logged**: they reach the handler
|
|
250
|
+
(its input throws) and the caller.
|
|
251
|
+
|
|
252
|
+
### The deprecated protocol
|
|
253
|
+
|
|
254
|
+
The sections below describe the deprecated protocol, which `serve` still
|
|
255
|
+
answers and `connect` falls back to until the next release.
|
|
256
|
+
|
|
257
|
+
#### Frames on the stream
|
|
164
258
|
|
|
165
259
|
```
|
|
166
260
|
+--------+-----------------+-----------------+
|
|
@@ -190,7 +284,7 @@ or `webrun-streams-libp2p: a malformed frame length`, and aborts the stream:
|
|
|
190
284
|
a peer not speaking this framing would otherwise make the receiver buffer
|
|
191
285
|
without bound. On the serving side the handler's input throws that error.
|
|
192
286
|
|
|
193
|
-
|
|
287
|
+
#### Why identity reaches the handler by closure
|
|
194
288
|
|
|
195
289
|
`Duplex` is bytes-only, so `serveConnections` passes identity to a factory
|
|
196
290
|
instead of to the handler. `remotePeer` is `connection.remotePeer`, the peer id
|
|
@@ -201,7 +295,7 @@ inside it. Reusing one handler built outside it for every connection is how
|
|
|
201
295
|
identity-by-closure gets broken. `serve` is `serveConnections` with the context
|
|
202
296
|
ignored, so both share framing, teardown and failure handling.
|
|
203
297
|
|
|
204
|
-
|
|
298
|
+
#### Why sends wait for the `'drain'` event
|
|
205
299
|
|
|
206
300
|
libp2p 3.x streams are push-based: `stream.send(chunk)` returns `false` when
|
|
207
301
|
the write buffer is full, and the stream emits `'drain'` when it can take
|
|
@@ -212,7 +306,7 @@ so after the first backpressure cycle it resolves immediately while the buffer
|
|
|
212
306
|
is still full, and a write loop built on it fills the buffer without bound.
|
|
213
307
|
`tests/backpressure.test.ts` covers this.
|
|
214
308
|
|
|
215
|
-
|
|
309
|
+
#### Why both timeouts are minutes, not seconds
|
|
216
310
|
|
|
217
311
|
- **Drain timeout** (`DEFAULT_DRAIN_TIMEOUT_MS`, 5 minutes, `drainTimeoutMs`
|
|
218
312
|
per call site). A peer that requests something and then stops reading,
|
|
@@ -233,36 +327,7 @@ is still full, and a write loop built on it fills the buffer without bound.
|
|
|
233
327
|
`[webrun-streams-libp2p] closeStream: graceful close of protocol <p> failed (bound <ms>ms), aborting instead (peer may see truncated data): <reason>`.
|
|
234
328
|
The close timeout is not configurable through `connect` / `serve` params.
|
|
235
329
|
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
With no connection to the peer yet, every call dials and they all share the
|
|
239
|
-
one connection libp2p establishes. Their streams can reach the remote while it
|
|
240
|
-
is still upgrading that connection. yamux counts those as early streams and,
|
|
241
|
-
past `maxEarlyStreams` (10 by default), aborts the whole muxer: every stream
|
|
242
|
-
on the connection dies, the remote sees EOF during protocol negotiation, and
|
|
243
|
-
this side sees `StreamResetError`. Raising the limit on your own nodes would
|
|
244
|
-
not help against other peers.
|
|
245
|
-
|
|
246
|
-
So until one stream has negotiated its protocol on an open connection, calls
|
|
247
|
-
open their streams one at a time; after that they open concurrently. If the
|
|
248
|
-
proven connection closes, the next burst is gated again (this re-check needs
|
|
249
|
-
`node.getConnections`; a node that offers only `dialProtocol` stays proven
|
|
250
|
-
after its first stream). The state is shared by every `connect()` to the same
|
|
251
|
-
peer from the same node, because callers often `connect()` per request.
|
|
252
|
-
|
|
253
|
-
### Stream limits
|
|
254
|
-
|
|
255
|
-
libp2p caps the number of open streams for one protocol per connection, and
|
|
256
|
-
past the cap a new stream is reset, not queued. One call is one stream, so a
|
|
257
|
-
caller with more concurrent calls than the cap sees calls rejected with no
|
|
258
|
-
other symptom. `maxInboundStreams`, `maxOutboundStreams` and
|
|
259
|
-
`runOnLimitedConnection` are passed through unchanged; an unset option is left
|
|
260
|
-
out of the options object so libp2p's own default applies (inbound 32,
|
|
261
|
-
outbound 64, limited connections refused). `tests/stream-limits.test.ts`
|
|
262
|
-
checks both that a raised `maxInboundStreams` admits more than 32 concurrent
|
|
263
|
-
streams and that an unconfigured server stops at 32.
|
|
264
|
-
|
|
265
|
-
### How calls end
|
|
330
|
+
#### How calls end
|
|
266
331
|
|
|
267
332
|
- **Normal end.** Both sides finished: the stream is closed gracefully,
|
|
268
333
|
bounded by the close timeout.
|
|
@@ -300,14 +365,43 @@ streams and that an unconfigured server stops at 32.
|
|
|
300
365
|
A caller that sent its whole request and then stopped reading the response
|
|
301
366
|
(`.return()` mid-stream) is ordinary and is not logged.
|
|
302
367
|
|
|
368
|
+
### Why a burst of calls is serialised on a fresh connection
|
|
369
|
+
|
|
370
|
+
With no connection to the peer yet, every call dials and they all share the
|
|
371
|
+
one connection libp2p establishes. Their streams can reach the remote while it
|
|
372
|
+
is still upgrading that connection. yamux counts those as early streams and,
|
|
373
|
+
past `maxEarlyStreams` (10 by default), aborts the whole muxer: every stream
|
|
374
|
+
on the connection dies, the remote sees EOF during protocol negotiation, and
|
|
375
|
+
this side sees `StreamResetError`. Raising the limit on your own nodes would
|
|
376
|
+
not help against other peers.
|
|
377
|
+
|
|
378
|
+
So until one stream has negotiated its protocol on an open connection, calls
|
|
379
|
+
open their streams one at a time; after that they open concurrently. If the
|
|
380
|
+
proven connection closes, the next burst is gated again (this re-check needs
|
|
381
|
+
`node.getConnections`; a node that offers only `dialProtocol` stays proven
|
|
382
|
+
after its first stream). The state is shared by every `connect()` to the same
|
|
383
|
+
peer from the same node, because callers often `connect()` per request.
|
|
384
|
+
|
|
385
|
+
### Stream limits
|
|
386
|
+
|
|
387
|
+
libp2p caps the number of open streams for one protocol per connection, and
|
|
388
|
+
past the cap a new stream is reset, not queued. One call is one stream, so a
|
|
389
|
+
caller with more concurrent calls than the cap sees calls rejected with no
|
|
390
|
+
other symptom. `maxInboundStreams`, `maxOutboundStreams` and
|
|
391
|
+
`runOnLimitedConnection` are passed through unchanged; an unset option is left
|
|
392
|
+
out of the options object so libp2p's own default applies (inbound 32,
|
|
393
|
+
outbound 64, limited connections refused). `tests/stream-limits.test.ts`
|
|
394
|
+
checks both that a raised `maxInboundStreams` admits more than 32 concurrent
|
|
395
|
+
streams and that an unconfigured server stops at 32.
|
|
396
|
+
|
|
303
397
|
### MessagePorts over libp2p, and the port stack on them
|
|
304
398
|
|
|
305
|
-
`connect` / `serve` above
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
399
|
+
`connect` / `serve` above are the first of the two ways below, with a window
|
|
400
|
+
and msgpack set for you. Use the port mux directly to choose the codec, the
|
|
401
|
+
chunk size or the multiplexing, or to put other port users on the same node.
|
|
402
|
+
`webrun-rpc`'s port stack bounds a producer to its consumer whatever the
|
|
403
|
+
transport buffers underneath: each chunk waits for the peer's confirmation, or,
|
|
404
|
+
on a windowed port, for the window's credit. Two ways to put libp2p under it:
|
|
311
405
|
|
|
312
406
|
```ts
|
|
313
407
|
import { deserialize, msgpackCodec, serialize } from "@statewalker/webrun-msgpack";
|
|
@@ -336,10 +430,21 @@ Both pass the shared conformance suite, including L6 at a one-chunk bound
|
|
|
336
430
|
| who is calling | `onPort` gets `{ remotePeer, connection }` | the stream's connection, once |
|
|
337
431
|
| 4 MiB echo, in-process TCP, setup excluded (three runs) | 35–38 MiB/s (64 KiB chunks), 71–88 MiB/s (256 KiB) | 37–48 MiB/s, 79–87 MiB/s |
|
|
338
432
|
|
|
433
|
+
**A window per port.** By default the stream tier waits for each chunk's
|
|
434
|
+
confirmation on a native port, so one call moves one chunk (256 KiB) per round
|
|
435
|
+
trip. With `window: n` (bytes) on both ends, a port's stream negotiates
|
|
436
|
+
`windowProtocolOf(protocol)` (`protocol` + `/window`), each end grants the other
|
|
437
|
+
`n` bytes of messages delivered and not yet consumed, and the stream tier
|
|
438
|
+
pipelines its chunks up to the peer's window (statewalker/webrun-wire#153). A
|
|
439
|
+
mux with `window` dials the windowed protocol first and falls back to
|
|
440
|
+
`protocol`, and serves both, so it works against an older peer, unwindowed.
|
|
441
|
+
`describePortWindow` (`webrun-streams-conformance`) and L6 at the window's bound
|
|
442
|
+
run against it (`tests/port-window.test.ts`).
|
|
443
|
+
|
|
339
444
|
Each native port carries `closeSignal`, aborted when its stream ends, the peer
|
|
340
445
|
goes, or the port is closed here; `webrun-rpc`'s `getPortCloseSignal` reads it
|
|
341
|
-
from the port, so a call on a dead stream fails instead of waiting, and
|
|
342
|
-
|
|
446
|
+
from the port, so a call on a dead stream fails instead of waiting, and
|
|
447
|
+
`libp2pPortMux` itself imports nothing from `webrun-rpc`. Ports are opened through the
|
|
343
448
|
same early-stream gate as `connect()`. A port `onPort` refuses (returns
|
|
344
449
|
`false`) is aborted: the opener's signal aborts with `PeerGoneError` and a call
|
|
345
450
|
on it fails. A frame the port's codec cannot decode (a peer speaking another
|
|
@@ -390,6 +495,8 @@ reader buffer without bound. Raise it with
|
|
|
390
495
|
|
|
391
496
|
### Constraints
|
|
392
497
|
|
|
498
|
+
The two below concern the deprecated protocol.
|
|
499
|
+
|
|
393
500
|
- On the serving side, request bytes are queued for the handler without a
|
|
394
501
|
bound; backpressure applies to the handler's output, not to its input.
|
|
395
502
|
- `duplexOverStream` must start reading the stream without an intervening
|
|
@@ -405,9 +512,19 @@ its connection in-process and SIGKILLing a forked server
|
|
|
405
512
|
(`tests/fixtures/greeting-server.ts`, run with `tsx`), with 100 open channels
|
|
406
513
|
each; every channel must fail with an error within 1 s. The shared conformance
|
|
407
514
|
suite (`tests/conformance.test.ts`, about 7 s) runs in the same `pnpm test`, with
|
|
408
|
-
`skipHugeBody
|
|
409
|
-
|
|
410
|
-
|
|
515
|
+
`skipHugeBody`, for `connect`/`serve` and for the deprecated protocol on both
|
|
516
|
+
ends.
|
|
517
|
+
|
|
518
|
+
**`connect`/`serve` over the windowed port mux** (`tests/connect-over-ports.test.ts`):
|
|
519
|
+
the protocol ids negotiated, the 4 MiB default window and a 1 MiB `window`
|
|
520
|
+
option holding a producer to the window (L6, in both directions, over yamux's
|
|
521
|
+
16 MiB stream window), identity per call, and the window floor.
|
|
522
|
+
**Interop** (`tests/interop-0-6.test.ts`): the conformance suite L0–L6 and the
|
|
523
|
+
fallback cases between this release and the published 0.6.0
|
|
524
|
+
(`webrun-streams-libp2p-0-6`, an npm alias), old caller to new server and new
|
|
525
|
+
caller to old server.
|
|
526
|
+
|
|
527
|
+
**Run-ahead is bounded for the deprecated protocol** (spec D17, L6;
|
|
411
528
|
`tests/run-ahead.test.ts`, always on). L6 first found none: a consumer that
|
|
412
529
|
stopped reading let the producer run 22-24 MiB ahead until libp2p's 4 MiB read
|
|
413
530
|
buffer killed the stream. Two things buffered without limit:
|
|
@@ -476,13 +593,16 @@ the signal never aborted.
|
|
|
476
593
|
|
|
477
594
|
### Dependencies
|
|
478
595
|
|
|
479
|
-
- `@statewalker/webrun-streams` (`workspace:^`): the seam types
|
|
480
|
-
`serializeError` / `deserializeError`.
|
|
596
|
+
- `@statewalker/webrun-streams` (`workspace:^`): the seam types, the native
|
|
597
|
+
port mux and `serializeError` / `deserializeError`.
|
|
598
|
+
- `@statewalker/webrun-rpc` (`workspace:^`): the port stack `connect`/`serve`
|
|
599
|
+
run (`connect`, `serve`, `overPorts`).
|
|
600
|
+
- `@statewalker/webrun-msgpack` (`workspace:^`): the calls' value codec.
|
|
481
601
|
- `@libp2p/interface`, `@multiformats/multiaddr` (peers, types only) and
|
|
482
602
|
`libp2p` (optional peer). Dev dependencies `@chainsafe/libp2p-noise`,
|
|
483
603
|
`@chainsafe/libp2p-yamux`, `@libp2p/tcp` and `@libp2p/utils` build the test
|
|
484
|
-
nodes
|
|
485
|
-
|
|
604
|
+
nodes, `webrun-streams-libp2p-0-6` (the published 0.6.0) is the old peer of
|
|
605
|
+
the interop tests, and `tsx` runs the forked server.
|
|
486
606
|
|
|
487
607
|
## License
|
|
488
608
|
|