GeezerVison is showing github.com — stream: canWrite and ondrain now reflect physical capacity · nodejs/node@2dc09ca
File treeExpand file treeCollapse file tree Open diff view settings lib/internal/streams/iter
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: doc/api/stream_iter.md +14-6Lines changed: 14 additions & 6 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -417,9 +417,13 @@ writer.fail(err); // Always synchronous, no fallback needed | 417 | 417 |
| 418 | 418 | * {boolean|null} | 419 | 419 |
| 420 | | -Returns `true` if the next write is likely to be accepted (buffered data is | 421 | | -below capacity), `false` if backpressure is active, or `null` if the writer | 422 | | -is closed or the consumer has disconnected. | | 420 | +Returns `true` if the slots buffer has physical capacity (buffered data is | | 421 | +below the configured byte budget), `false` if the budget is exhausted, or | | 422 | +`null` if the writer is closed or the consumer has disconnected. | | 423 | + | | 424 | +This reports physical capacity independently of the backpressure policy. With | | 425 | +`'drop-oldest'` or `'drop-newest'`, writes still complete when this is `false` | | 426 | +by evicting buffered data or discarding the incoming data, respectively. | 423 | 427 |
| 424 | 428 | This is a hint, not a guarantee: the state can change between the check and | 425 | 429 | the write. Use [`ondrain()`][] to wait for capacity rather than polling. | @@ -1055,9 +1059,13 @@ added: v25.9.0 | 1055 | 1059 | * `drainable` {Object} An object implementing the drainable protocol. | 1056 | 1060 | * Returns: {Promise|null} | 1057 | 1061 |
| 1058 | | -Wait for a drainable writer's backpressure to clear. Returns `null` if | 1059 | | -the object does not implement the drainable protocol, or a promise that | 1060 | | -fulfills with `true` when the writer can accept more data. | | 1062 | +Wait for a drainable writer to regain physical buffer capacity. Returns `null` | | 1063 | +if the object does not implement the drainable protocol, or a promise that | | 1064 | +fulfills with `true` when buffered data falls below the byte budget. | | 1065 | + | | 1066 | +For writers using `'drop-oldest'` or `'drop-newest'`, this waits for physical | | 1067 | +capacity even though writes do not block. This allows producers to avoid data | | 1068 | +loss by waiting before writing. | 1061 | 1069 |
| 1062 | 1070 | ```mjs | 1063 | 1071 | import { push, ondrain, text } from 'node:stream/iter'; | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/streams/iter/broadcast.js +8-6Lines changed: 8 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -310,6 +310,7 @@ class BroadcastImpl { | 310 | 310 | if (this.#ended || this.#cancelled) return false; | 311 | 311 |
| 312 | 312 | const batchSize = entry.byteLength; | | 313 | + let droppedOldest = false; | 313 | 314 |
| 314 | 315 | // Skip empty chunks -- zero-byte writes would accumulate infinitely | 315 | 316 | // without ever triggering backpressure under a byte-budget model. | @@ -321,6 +322,7 @@ class BroadcastImpl { | 321 | 322 | case 'unbounded': | 322 | 323 | return false; | 323 | 324 | case 'drop-oldest': | | 325 | + droppedOldest = true; | 324 | 326 | while (this.#bufferedBytes >= this.#options.budget && | 325 | 327 | this.#buffer.length > 0) { | 326 | 328 | const evicted = this.#buffer.shift(); | @@ -343,6 +345,10 @@ class BroadcastImpl { | 343 | 345 | this.#buffer.push(entry); | 344 | 346 | this.#bufferedBytes += batchSize; | 345 | 347 | this.#notifyConsumers(); | | 348 | + if (droppedOldest && | | 349 | + this.#bufferedBytes < this.#options.budget) { | | 350 | + this[kOnBufferDrained]?.(); | | 351 | + } | 346 | 352 | return true; | 347 | 353 | } | 348 | 354 |
| @@ -399,15 +405,13 @@ class BroadcastImpl { | 399 | 405 | } | 400 | 406 |
| 401 | 407 | /** | 402 | | - * Check if the next write is likely to be accepted. | | 408 | + * Check whether the slots buffer has capacity. | 403 | 409 | * Returns null if ended/cancelled, true/false otherwise. | 404 | 410 | * @returns {boolean | null} | 405 | 411 | */ | 406 | 412 | [kCanWrite]() { | 407 | 413 | if (this.#ended || this.#cancelled) return null; | 408 | | - if ((this.#options.backpressure === 'strict' || | 409 | | - this.#options.backpressure === 'unbounded') && | 410 | | - this.#bufferedBytes >= this.#options.budget) { | | 414 | + if (this.#bufferedBytes >= this.#options.budget) { | 411 | 415 | return false; | 412 | 416 | } | 413 | 417 | return true; | @@ -666,7 +670,6 @@ class BroadcastWriter { | 666 | 670 |
| 667 | 671 | writeSync(chunk) { | 668 | 672 | if (this.#state !== 'open') return false; | 669 | | - if (!this.#broadcast[kCanWrite]()) return false; | 670 | 673 | const converted = | 671 | 674 | toUint8Array(chunk); | 672 | 675 | const batch = createBatchEntry([converted]); | @@ -680,7 +683,6 @@ class BroadcastWriter { | 680 | 683 | writevSync(chunks) { | 681 | 684 | validateArray(chunks, 'chunks'); | 682 | 685 | if (this.#state !== 'open') return false; | 683 | | - if (!this.#broadcast[kCanWrite]()) return false; | 684 | 686 | const converted = convertChunks(chunks); | 685 | 687 | const batch = createBatchEntry(converted); | 686 | 688 | if (this.#broadcast[kWrite](batch)) { | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/streams/iter/push.js +2-4Lines changed: 2 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -152,17 +152,15 @@ class PushQueue { | 152 | 152 | // =========================================================================== | 153 | 153 |
| 154 | 154 | /** | 155 | | - * Check if the next write is likely to be accepted. | | 155 | + * Check whether the slots buffer has capacity. | 156 | 156 | * Returns null if writer is closed/errored or consumer has terminated. | 157 | 157 | * @returns {boolean | null} | 158 | 158 | */ | 159 | 159 | get canWrite() { | 160 | 160 | if (this.#writerState !== 'open' || this.#consumerState !== 'active') { | 161 | 161 | return null; | 162 | 162 | } | 163 | | - if ((this.#backpressure === 'strict' || | 164 | | - this.#backpressure === 'unbounded') && | 165 | | - this.#bufferedBytes >= this.#budget) { | | 163 | + if (this.#bufferedBytes >= this.#budget) { | 166 | 164 | return false; | 167 | 165 | } | 168 | 166 | return true; | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-stream-iter-broadcast-backpressure.js +32-1Lines changed: 32 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 3 | 3 |
| 4 | 4 | const common = require('../common'); | 5 | 5 | const assert = require('assert'); | 6 | | -const { broadcast, text } = require('stream/iter'); | | 6 | +const { broadcast, ondrain, text } = require('stream/iter'); | 7 | 7 |
| 8 | 8 | // ============================================================================= | 9 | 9 | // Backpressure policies | @@ -47,6 +47,36 @@ async function testDropNewest() { | 47 | 47 | assert.strictEqual(data, 'K'.repeat(16384)); | 48 | 48 | } | 49 | 49 |
| | 50 | +async function testDropPoliciesReportPhysicalCapacity() { | | 51 | + const chunk = new Uint8Array(16384); | | 52 | + | | 53 | + for (const backpressure of ['drop-oldest', 'drop-newest']) { | | 54 | + const { writer, broadcast: bc } = broadcast({ | | 55 | + budget: chunk.byteLength, | | 56 | + backpressure, | | 57 | + }); | | 58 | + const iterator = bc.push()[Symbol.asyncIterator](); | | 59 | + | | 60 | + assert.strictEqual(writer.writeSync(chunk), true); | | 61 | + assert.strictEqual(writer.canWrite, false); | | 62 | + | | 63 | + let drained = false; | | 64 | + const drain = ondrain(writer); | | 65 | + drain.then(common.mustCall(() => { drained = true; })); | | 66 | + | | 67 | + // Drop policies still accept writes despite having no physical capacity. | | 68 | + assert.strictEqual(writer.writeSync(chunk), true); | | 69 | + assert.strictEqual(writer.canWrite, false); | | 70 | + await new Promise(setImmediate); | | 71 | + assert.strictEqual(drained, false); | | 72 | + | | 73 | + assert.strictEqual((await iterator.next()).done, false); | | 74 | + assert.strictEqual(await drain, true); | | 75 | + assert.strictEqual(writer.canWrite, true); | | 76 | + bc.cancel(); | | 77 | + } | | 78 | +} | | 79 | + | 50 | 80 | // ============================================================================= | 51 | 81 | // Block backpressure | 52 | 82 | // ============================================================================= | @@ -269,6 +299,7 @@ async function testEndSyncReturnValue() { | 269 | 299 | Promise.all([ | 270 | 300 | testDropOldest(), | 271 | 301 | testDropNewest(), | | 302 | + testDropPoliciesReportPhysicalCapacity(), | 272 | 303 | testBlockBackpressure(), | 273 | 304 | testBlockBackpressureContent(), | 274 | 305 | testStrictBackpressureOverflow(), | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-stream-iter-push-writer.js +31Lines changed: 31 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -19,6 +19,36 @@ async function testOndrain() { | 19 | 19 | assert.strictEqual(ondrain(writer), null); | 20 | 20 | } | 21 | 21 |
| | 22 | +async function testDropPoliciesReportPhysicalCapacity() { | | 23 | + const chunk = new Uint8Array(16384); | | 24 | + | | 25 | + for (const backpressure of ['drop-oldest', 'drop-newest']) { | | 26 | + const { writer, readable } = push({ | | 27 | + budget: chunk.byteLength, | | 28 | + backpressure, | | 29 | + }); | | 30 | + const iterator = readable[Symbol.asyncIterator](); | | 31 | + | | 32 | + assert.strictEqual(writer.writeSync(chunk), true); | | 33 | + assert.strictEqual(writer.canWrite, false); | | 34 | + | | 35 | + let drained = false; | | 36 | + const drain = ondrain(writer); | | 37 | + drain.then(common.mustCall(() => { drained = true; })); | | 38 | + | | 39 | + // Drop policies still accept writes despite having no physical capacity. | | 40 | + assert.strictEqual(writer.writeSync(chunk), true); | | 41 | + assert.strictEqual(writer.canWrite, false); | | 42 | + await new Promise(setImmediate); | | 43 | + assert.strictEqual(drained, false); | | 44 | + | | 45 | + assert.strictEqual((await iterator.next()).done, false); | | 46 | + assert.strictEqual(await drain, true); | | 47 | + assert.strictEqual(writer.canWrite, true); | | 48 | + await iterator.return(); | | 49 | + } | | 50 | +} | | 51 | + | 22 | 52 | async function testOndrainNonDrainable() { | 23 | 53 | // Non-drainable objects return null | 24 | 54 | assert.strictEqual(ondrain(null), null); | @@ -586,6 +616,7 @@ async function testFailRejectsPendingReadWithFalsyReason() { | 586 | 616 |
| 587 | 617 | Promise.all([ | 588 | 618 | testOndrain(), | | 619 | + testDropPoliciesReportPhysicalCapacity(), | 589 | 620 | testOndrainNonDrainable(), | 590 | 621 | testWriteWithSignalRejects(), | 591 | 622 | testWriteWithPreAbortedSignal(), | |
|