GeezerVison is showing github.com — quic: add option waitUntilAvailable to creating streams · nodejs/node@6603289
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: doc/api/quic.md +10Lines changed: 10 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -1365,6 +1365,11 @@ added: v23.8.0 | 1365 | 1365 | will buffer before `writeSync()` returns `false`. When the buffered | 1366 | 1366 | data exceeds this limit, the caller should wait for drain before | 1367 | 1367 | writing more. **Default:** `65536` (64 KB). | | 1368 | + * `waitUntilAvailable` {boolean} When true the promise will wait until flow | | 1369 | + control will allow to open the stream. If set to false, the function | | 1370 | + will return a rejected promise, if flow control will not allow to | | 1371 | + open the stream immediately. | | 1372 | + **Default:** `true` | 1368 | 1373 | * `onheaders` {Function} Callback for received initial response headers. | 1369 | 1374 | Called with `(headers)`. | 1370 | 1375 | * `ontrailers` {Function} Callback for received trailing headers. | @@ -1406,6 +1411,11 @@ added: v23.8.0 | 1406 | 1411 | will buffer before `writeSync()` returns `false`. When the buffered | 1407 | 1412 | data exceeds this limit, the caller should wait for drain before | 1408 | 1413 | writing more. **Default:** `65536` (64 KB). | | 1414 | + * `waitUntilAvailable` {boolean} When true the promise will wait until flow | | 1415 | + control will allow to open the stream. If set to false, the function | | 1416 | + will return a rejected promise, if flow control will not allow | | 1417 | + to open the stream immediately. | | 1418 | + **Default:** `true` | 1409 | 1419 | * `onheaders` {Function} Callback for received initial response headers. | 1410 | 1420 | Called with `(headers)`. | 1411 | 1421 | * `ontrailers` {Function} Callback for received trailing headers. | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/quic/quic.js +3-2Lines changed: 3 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -3229,6 +3229,7 @@ class QuicSession { | 3229 | 3229 | incremental = false, | 3230 | 3230 | budget = kDefaultBudget, | 3231 | 3231 | headers, | | 3232 | + waitUntilAvailable = true, | 3232 | 3233 | onheaders, | 3233 | 3234 | ontrailers, | 3234 | 3235 | oninfo, | @@ -3237,10 +3238,10 @@ class QuicSession { | 3237 | 3238 |
| 3238 | 3239 | validateOneOf(priority, 'options.priority', ['default', 'low', 'high']); | 3239 | 3240 | validateBoolean(incremental, 'options.incremental'); | 3240 | | - | | 3241 | + validateBoolean(waitUntilAvailable, 'options.waitUntilAvailable'); | 3241 | 3242 | const validatedBody = validateBody(body); | 3242 | 3243 |
| 3243 | | - const handle = this.#handle.openStream(direction, validatedBody); | | 3244 | + const handle = this.#handle.openStream(direction, waitUntilAvailable, validatedBody); | 3244 | 3245 | if (handle === undefined) { | 3245 | 3246 | throw new ERR_QUIC_OPEN_STREAM_FAILED(); | 3246 | 3247 | } | |
Collapse file Copy file name to clipboardExpand all lines: src/quic/session.cc +21-5Lines changed: 21 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1181,17 +1181,26 @@ struct Session::Impl final : public MemoryRetainer { | 1181 | 1181 | } | 1182 | 1182 |
| 1183 | 1183 | DCHECK(args[0]->IsUint32()); | | 1184 | + DCHECK(args[1]->IsBoolean()); | | 1185 | + | | 1186 | + auto direction = FromV8Value<Direction>(args[0]); | | 1187 | + if (!args[1].As<v8::Boolean>()->Value()) { | | 1188 | + // This is waitUntilAvailable | | 1189 | + if (!session->CanImmediatelyOpenStream(direction)) { | | 1190 | + return THROW_ERR_INVALID_STATE( | | 1191 | + env, "No new stream available within flow control"); | | 1192 | + } | | 1193 | + } | 1184 | 1194 |
| 1185 | 1195 | // GetDataQueueFromSource handles type validation. | 1186 | 1196 | std::shared_ptr<DataQueue> data_source; | 1187 | | - if (!Stream::GetDataQueueFromSource(env, args[1]).To(&data_source)) | | 1197 | + if (!Stream::GetDataQueueFromSource(env, args[2]).To(&data_source)) | 1188 | 1198 | [[unlikely]] { | 1189 | 1199 | return THROW_ERR_INVALID_ARG_VALUE(env, "Invalid data source"); | 1190 | 1200 | } | 1191 | 1201 |
| 1192 | 1202 | session->impl_->handshake_deferred_ = false; | 1193 | 1203 | SendPendingDataScope send_scope(session); | 1194 | | - auto direction = FromV8Value<Direction>(args[0]); | 1195 | 1204 | Local<Object> stream; | 1196 | 1205 | if (session->OpenStream(direction, std::move(data_source)).ToLocal(&stream)) | 1197 | 1206 | [[likely]] { | @@ -3239,6 +3248,14 @@ BaseObjectPtr<Stream> Session::CreateStream( | 3239 | 3248 | return {}; | 3240 | 3249 | } | 3241 | 3250 |
| | 3251 | +bool Session::CanImmediatelyOpenStream(Direction direction) { | | 3252 | + if (direction == Direction::BIDIRECTIONAL) { | | 3253 | + return max_local_streams_bidi() > 0; | | 3254 | + } else { | | 3255 | + return max_local_streams_uni() > 0; | | 3256 | + } | | 3257 | +} | | 3258 | + | 3242 | 3259 | MaybeLocal<Object> Session::OpenStream(Direction direction, | 3243 | 3260 | std::shared_ptr<DataQueue> data_source) { | 3244 | 3261 | // If can_create_streams() returns false, we are not able to open a stream | @@ -3544,13 +3561,12 @@ void Session::SetApplicationError(error_code app_error_code) { | 3544 | 3561 |
| 3545 | 3562 | uint64_t Session::max_local_streams_uni() const { | 3546 | 3563 | DCHECK(!is_destroyed()); | 3547 | | - return ngtcp2_conn_get_streams_uni_left(*this); | | 3564 | + return ngtcp2_conn_get_streams_uni_left2(*this); | 3548 | 3565 | } | 3549 | 3566 |
| 3550 | 3567 | uint64_t Session::max_local_streams_bidi() const { | 3551 | 3568 | DCHECK(!is_destroyed()); | 3552 | | - return ngtcp2_conn_get_local_transport_params(*this) | 3553 | | - ->initial_max_streams_bidi; | | 3569 | + return ngtcp2_conn_get_streams_bidi_left2(*this); | 3554 | 3570 | } | 3555 | 3571 |
| 3556 | 3572 | void Session::set_wrapped() { | |
Collapse file Copy file name to clipboardExpand all lines: src/quic/session.h +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -535,6 +535,9 @@ class Session final : public AsyncWrap, private SessionTicket::AppData::Source { | 535 | 535 | size_t max_packet_size() const; | 536 | 536 | void set_priority_supported(bool on = true); | 537 | 537 |
| | 538 | + // Check whether flow control permits opening another stream | | 539 | + bool CanImmediatelyOpenStream(Direction direction); | | 540 | + | 538 | 541 | // Open a new locally-initialized stream with the specified directionality. | 539 | 542 | // If the session is not yet in a state where the stream can be opened -- | 540 | 543 | // such as when the handshake is not yet sufficiently far along and ORTT | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-quic-stream-limits-pending.mjs +45-14Lines changed: 45 additions & 14 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -16,9 +16,11 @@ if (!hasQuic) { | 16 | 16 |
| 17 | 17 | const { listen, connect } = await import('../common/quic.mjs'); | 18 | 18 | const { bytes } = await import('stream/iter'); | | 19 | +const { setTimeout: sleep } = await import('timers/promises'); | 19 | 20 |
| 20 | 21 | const encoder = new TextEncoder(); | 21 | 22 | const allDone = Promise.withResolvers(); | | 23 | +const twoDone = Promise.withResolvers(); | 22 | 24 | let serverStreamCount = 0; | 23 | 25 |
| 24 | 26 | // Server allows only 1 bidi stream at a time. | @@ -27,11 +29,17 @@ const serverEndpoint = await listen(mustCall((serverSession) => { | 27 | 29 | await bytes(stream); | 28 | 30 | stream.writer.endSync(); | 29 | 31 | await stream.closed; | 30 | | - if (++serverStreamCount === 2) { | 31 | | - serverSession.close(); | | 32 | + ++serverStreamCount; | | 33 | + if (serverStreamCount === 2) { | | 34 | + twoDone.resolve(); | | 35 | + } | | 36 | + if (serverStreamCount === 3) { | 32 | 37 | allDone.resolve(); | 33 | 38 | } | 34 | | - }, 2); | | 39 | + if (serverStreamCount === 4) { | | 40 | + serverSession.close(); | | 41 | + } | | 42 | + }, 3); | 35 | 43 | }), { | 36 | 44 | transportParams: { initialMaxStreamsBidi: 1 }, | 37 | 45 | }); | @@ -51,27 +59,45 @@ s1.opened.then(() => { | 51 | 59 | opened++; | 52 | 60 | }); | 53 | 61 |
| 54 | | -// Second stream is created but queued as pending because the | 55 | | -// server only allows 1 concurrent bidi stream. | 56 | | -const s2 = await clientSession.createBidirectionalStream({ | 57 | | - body: encoder.encode('stream 2'), | | 62 | +let s2; | | 63 | +await assert.rejects( | | 64 | + async () => { | | 65 | + // Second stream should not open, but throw. | | 66 | + s2 = await clientSession.createBidirectionalStream({ | | 67 | + body: encoder.encode('stream 2a'), | | 68 | + waitUntilAvailable: false, | | 69 | + }); | | 70 | + // eslint-disable-next-line node-core/must-call-assert | | 71 | + s2.opened.then(() => { | | 72 | + opened++; | | 73 | + }); | | 74 | + }, | | 75 | + { | | 76 | + name: 'Error', | | 77 | + message: 'No new stream available within flow control', | | 78 | + }, | | 79 | +); | | 80 | +// Ok try again a second second stream, that patiently waits | | 81 | +s2 = await clientSession.createBidirectionalStream({ | | 82 | + body: encoder.encode('stream 2b') | 58 | 83 | }); | 59 | | - | 60 | 84 | // eslint-disable-next-line node-core/must-call-assert | 61 | 85 | s2.opened.then(() => { | 62 | 86 | opened++; | 63 | 87 | }); | 64 | 88 |
| | 89 | + | 65 | 90 | // Third stream is created but queued as pending because the | 66 | 91 | // server only allows 1 concurrent bidi stream. | 67 | 92 | const s3 = await clientSession.createBidirectionalStream({ | 68 | 93 | body: encoder.encode('stream 3'), | 69 | 94 | }); | 70 | 95 |
| 71 | 96 |
| 72 | | -// s2 should be pending until s1 closes and the server grants | | 97 | +// s2 and s3 should be pending until s1 closes and the server grants | 73 | 98 | // more stream credits. | 74 | 99 | assert.strictEqual(s2.pending, true); | | 100 | +assert.strictEqual(s3.pending, true); | 75 | 101 | assert.strictEqual(opened, 1); | 76 | 102 |
| 77 | 103 | await dump(s1); | @@ -82,15 +108,20 @@ s3.destroy(err); | 82 | 108 |
| 83 | 109 | await Promise.all([assert.rejects(s3.opened, err), assert.rejects(s3.closed, err)]); | 84 | 110 |
| 85 | | - | 86 | | -// After s1 closes, the server sends MAX_STREAMS which opens s2. | | 111 | +// After s1 closes, the server sends MAX_STREAMS which opens s3. | 87 | 112 | // Wait for the server to receive both streams. | 88 | | -await allDone.promise; | 89 | | -assert.strictEqual(opened, 2); | 90 | | - | | 113 | +await twoDone.promise; | 91 | 114 | // s2 should no longer be pending. | 92 | 115 | await dump(s2); | 93 | 116 | await s2.closed; | 94 | 117 |
| | 118 | +await sleep(10); // We wait a bit, as we do not have a callback exposed to js | | 119 | +// fourth stream should open immediately and not throw | | 120 | +const s4 = await clientSession.createBidirectionalStream({ | | 121 | + body: encoder.encode('stream 4'), | | 122 | + waitUntilAvailable: false | | 123 | +}); | | 124 | +await Promise.all([s4.closed, allDone.promise]); | | 125 | + | 95 | 126 | await clientSession.close(); | 96 | 127 | await serverEndpoint.close(); |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-quic-stream-limits-uni.mjs +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -39,6 +39,7 @@ let opened = 0; | 39 | 39 | // First uni stream opens immediately. | 40 | 40 | const s1 = await clientSession.createUnidirectionalStream({ | 41 | 41 | body: encoder.encode('uni 1'), | | 42 | + waitUntilAvailable: false, | 42 | 43 | }); | 43 | 44 |
| 44 | 45 | // eslint-disable-next-line node-core/must-call-assert | |
|