h2lean 0.10.1 → 0.11.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/lib/client-session.js +10 -4
- package/lib/client-stream.js +3 -0
- package/lib/compat.js +17 -7
- package/lib/fast-tick.js +115 -0
- package/lib/server.js +13 -0
- package/lib/session.js +16 -9
- package/lib/stream.js +76 -26
- package/package.json +1 -1
package/lib/client-session.js
CHANGED
|
@@ -116,6 +116,7 @@ const tls = require('node:tls');
|
|
|
116
116
|
const f = require('./frames');
|
|
117
117
|
const { Decoder, Encoder, HPackError, latin1 } = require('./hpack');
|
|
118
118
|
const FlatList = require('./flat-list');
|
|
119
|
+
const fastTick = require('./fast-tick');
|
|
119
120
|
// The process-wide inbound decode scratch, same shape and size as the server
|
|
120
121
|
// session's; the full why-sharing-is-safe argument lives with the server's
|
|
121
122
|
// instance (lib/session.js). A module-level instance here too, not the server's
|
|
@@ -258,6 +259,11 @@ class ClientHttp2Session extends EventEmitter {
|
|
|
258
259
|
}
|
|
259
260
|
socket[kBoundSession] = this;
|
|
260
261
|
this._socket = socket;
|
|
262
|
+
// Every deferred callback this session schedules runs through ONE route:
|
|
263
|
+
// node's own tick queue (default) or the shared batched pump of
|
|
264
|
+
// lib/fast-tick.js (`batchNextTick: true`) -- see session.js. Streams and
|
|
265
|
+
// anything else derived from the session copy this reference.
|
|
266
|
+
this._tick = fastTick.resolve(opts);
|
|
261
267
|
// public `socket` returns node's safe Proxy, built lazily; close drops it (see session.js)
|
|
262
268
|
this._proxySocket = null;
|
|
263
269
|
this.type = 1; // NGHTTP2_SESSION_CLIENT
|
|
@@ -557,7 +563,7 @@ class ClientHttp2Session extends EventEmitter {
|
|
|
557
563
|
// event fires. Without the deferral, this pattern misses the event:
|
|
558
564
|
// const sess = lite.connect(url, { createConnection: () => alreadyOpenSocket });
|
|
559
565
|
// sess.on('connect', handler); // attached after emit already ran synchronously
|
|
560
|
-
|
|
566
|
+
this._tick(() => {
|
|
561
567
|
if (this.destroyed || this._closeEmitted) {
|
|
562
568
|
return;
|
|
563
569
|
}
|
|
@@ -916,7 +922,7 @@ class ClientHttp2Session extends EventEmitter {
|
|
|
916
922
|
// node's requestOnError: destroy the stream with the captured error on the
|
|
917
923
|
// next tick, and submit nothing. The id reserved above is never used -- the
|
|
918
924
|
// session is destroyed or past its GOAWAY mark, so no later request takes it.
|
|
919
|
-
|
|
925
|
+
this._tick(() => stream.destroy(requestError));
|
|
920
926
|
} else if (this._started) {
|
|
921
927
|
this._enqueue(req);
|
|
922
928
|
} else {
|
|
@@ -2743,7 +2749,7 @@ class ClientHttp2Session extends EventEmitter {
|
|
|
2743
2749
|
this._maybeClose();
|
|
2744
2750
|
if (typeof cb === 'function') {
|
|
2745
2751
|
if (this._streams.size === 0) {
|
|
2746
|
-
|
|
2752
|
+
this._tick(cb);
|
|
2747
2753
|
} else {
|
|
2748
2754
|
this.once('close', cb);
|
|
2749
2755
|
}
|
|
@@ -2926,7 +2932,7 @@ class ClientHttp2Session extends EventEmitter {
|
|
|
2926
2932
|
socket.destroy();
|
|
2927
2933
|
}
|
|
2928
2934
|
}
|
|
2929
|
-
|
|
2935
|
+
this._tick(() => {
|
|
2930
2936
|
if (!this._closeEmitted) {
|
|
2931
2937
|
this._emitClose(err);
|
|
2932
2938
|
}
|
package/lib/client-stream.js
CHANGED
|
@@ -47,6 +47,9 @@ class ClientHttp2Stream extends Duplex {
|
|
|
47
47
|
super({ allowHalfOpen: true, decodeStrings: false });
|
|
48
48
|
this.id = id;
|
|
49
49
|
this.session = session;
|
|
50
|
+
// The session's one tick route -- node's queue or the batched pump of
|
|
51
|
+
// lib/fast-tick.js; see stream.js.
|
|
52
|
+
this._tick = session._tick;
|
|
50
53
|
// Per-stream flow-control state; the connection window lives on the session.
|
|
51
54
|
// Both start at the peer's advertised initial window (65535 until its SETTINGS
|
|
52
55
|
// land) exactly as the server side does. The value written here is the
|
package/lib/compat.js
CHANGED
|
@@ -53,7 +53,7 @@
|
|
|
53
53
|
'use strict';
|
|
54
54
|
|
|
55
55
|
const { Readable, Stream } = require('node:stream');
|
|
56
|
-
|
|
56
|
+
|
|
57
57
|
const { nullProto, trimName } = require('./headers');
|
|
58
58
|
|
|
59
59
|
function err(code, msg) {
|
|
@@ -201,13 +201,17 @@ function onRequestClose() {
|
|
|
201
201
|
// 'close' after 'end', so queue it behind the end the push above scheduled.
|
|
202
202
|
// node emits it right here instead, so the two stacks differ in the order of
|
|
203
203
|
// these two: see the difference list at the top of this file.
|
|
204
|
-
|
|
204
|
+
req._tick(emitCloseNT, req);
|
|
205
205
|
}
|
|
206
206
|
|
|
207
207
|
function emitCloseNT(target) {
|
|
208
208
|
target.emit('close');
|
|
209
209
|
}
|
|
210
210
|
|
|
211
|
+
function emitFinishNT(target) {
|
|
212
|
+
target.emit('finish');
|
|
213
|
+
}
|
|
214
|
+
|
|
211
215
|
function onRequestTimeout() {
|
|
212
216
|
const req = this._request;
|
|
213
217
|
if (req !== undefined && !req._closed) {
|
|
@@ -256,6 +260,9 @@ class Http2ServerRequest extends Readable {
|
|
|
256
260
|
constructor(stream, headers, rawHeaders) {
|
|
257
261
|
super({ autoDestroy: false });
|
|
258
262
|
this._stream = stream;
|
|
263
|
+
// The stream's one tick route -- node's queue or the batched pump of
|
|
264
|
+
// lib/fast-tick.js; the deferred callbacks of the compat layer go through it.
|
|
265
|
+
this._tick = stream._tick;
|
|
259
266
|
this._headers = headers;
|
|
260
267
|
this._rawHeaders = rawHeaders === null ? [] : rawHeaders;
|
|
261
268
|
this._trailers = { __proto__: null };
|
|
@@ -446,6 +453,9 @@ function onResponseTimeout() {
|
|
|
446
453
|
class Http2ServerResponse extends Stream {
|
|
447
454
|
constructor(stream) {
|
|
448
455
|
super();
|
|
456
|
+
// The stream's one tick route -- node's queue or the batched pump of
|
|
457
|
+
// lib/fast-tick.js; the deferred callbacks of the compat layer go through it.
|
|
458
|
+
this._tick = stream._tick;
|
|
449
459
|
this._state = {
|
|
450
460
|
closed: false,
|
|
451
461
|
ending: false,
|
|
@@ -758,14 +768,14 @@ class Http2ServerResponse extends Stream {
|
|
|
758
768
|
}
|
|
759
769
|
if (failure) {
|
|
760
770
|
if (typeof cb === 'function') {
|
|
761
|
-
|
|
771
|
+
this._tick(cb, failure);
|
|
762
772
|
}
|
|
763
773
|
this.destroy(failure);
|
|
764
774
|
return false;
|
|
765
775
|
}
|
|
766
776
|
if (state.headRequest) {
|
|
767
777
|
if (typeof cb === 'function') {
|
|
768
|
-
|
|
778
|
+
this._tick(cb);
|
|
769
779
|
}
|
|
770
780
|
return true; // a HEAD response has no body
|
|
771
781
|
}
|
|
@@ -786,7 +796,7 @@ class Http2ServerResponse extends Stream {
|
|
|
786
796
|
|
|
787
797
|
if (state.closed || state.ending) {
|
|
788
798
|
if (typeof cb === 'function') {
|
|
789
|
-
|
|
799
|
+
this._tick(cb);
|
|
790
800
|
}
|
|
791
801
|
return this;
|
|
792
802
|
}
|
|
@@ -807,7 +817,7 @@ class Http2ServerResponse extends Stream {
|
|
|
807
817
|
}
|
|
808
818
|
if (!state.closed && !state.finishEmitted) {
|
|
809
819
|
state.finishEmitted = true;
|
|
810
|
-
|
|
820
|
+
this._tick(emitFinishNT, this);
|
|
811
821
|
}
|
|
812
822
|
|
|
813
823
|
// node's compat end() ends the stream and does nothing else
|
|
@@ -892,7 +902,7 @@ class Http2ServerResponse extends Stream {
|
|
|
892
902
|
throw e;
|
|
893
903
|
}
|
|
894
904
|
if (this._state.closed) {
|
|
895
|
-
|
|
905
|
+
this._tick(callback, err('ERR_HTTP2_INVALID_STREAM', 'The stream has been destroyed'));
|
|
896
906
|
return;
|
|
897
907
|
}
|
|
898
908
|
this._stream.pushStream(headers, {}, (e, stream) => {
|
package/lib/fast-tick.js
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// A batched replacement for process.nextTick, built like lib/fast-timer.js is
|
|
4
|
+
// built: one module, one pump shared by every session in the process, and the
|
|
5
|
+
// callback order as the contract. The application opts in per server or client
|
|
6
|
+
// with `batchNextTick: true` (default false = node's own process.nextTick);
|
|
7
|
+
// H2LEAN_BATCH_TICKS=1 turns it on wherever the option is left undefined, so
|
|
8
|
+
// the whole suite can run under the pump and pin its event orders.
|
|
9
|
+
//
|
|
10
|
+
// What it changes: instead of one tick-queue entry per callback, callbacks
|
|
11
|
+
// land in a flat array and ONE real nextTick -- the pump -- drains them, so a
|
|
12
|
+
// busy server pays the scheduler entry once per event-loop turn instead of
|
|
13
|
+
// once per request. bench/micro/tick-cost.js prices the entry at ~0.1 us
|
|
14
|
+
// isolated while the server-measured marginal of one merged tick is
|
|
15
|
+
// 0.03-0.05 us (OPTIMIZATION.md item 46, the pump's paired numbers in item
|
|
16
|
+
// 47), which is why this removes ALL of a session's entries rather than
|
|
17
|
+
// merging two.
|
|
18
|
+
//
|
|
19
|
+
// What it keeps: FIFO order exactly as node's single queue gives it --
|
|
20
|
+
// including callbacks scheduled while the pump drains, which land in the next
|
|
21
|
+
// generation (the double buffer below). That reproduces node's tail-of-the-
|
|
22
|
+
// queue semantics for everything h2lean schedules, which is why the suite
|
|
23
|
+
// passes with the pump on: test/event-order.test.js and the teardown traces
|
|
24
|
+
// pin the orders this depends on.
|
|
25
|
+
//
|
|
26
|
+
// What it cannot keep, and the option documents: node's ONE queue interleaves
|
|
27
|
+
// foreign ticks -- an application's own process.nextTick called from inside
|
|
28
|
+
// an event handler -- with ours in schedule order, while a pump generation
|
|
29
|
+
// runs as one queue entry, so such a foreign tick can observe a different
|
|
30
|
+
// position between two of our callbacks. And async-context propagation: a
|
|
31
|
+
// callback runs in the pump's context, not in the context that was current
|
|
32
|
+
// when it was scheduled, so an application reading AsyncLocalStorage inside
|
|
33
|
+
// a deferred 'finish' handler needs the default mode.
|
|
34
|
+
//
|
|
35
|
+
// On an exception inside the pump, the throwing pair is dropped and the
|
|
36
|
+
// callbacks after it are carried into the next generation, whose pump entry
|
|
37
|
+
// runs at the next turn. Measured on a live node: an exception escaping a
|
|
38
|
+
// tick aborts that turn's drain -- the ticks behind it stay in the queue and
|
|
39
|
+
// run at the next tick phase only if the event loop gets there (a pending
|
|
40
|
+
// tick queue alone does not start a turn, and without an uncaughtException
|
|
41
|
+
// handler the process dies), so the pump reproduces exactly that shape.
|
|
42
|
+
//
|
|
43
|
+
// const fastTick = require('./fast-tick');
|
|
44
|
+
// this._tick = fastTick.resolve(opts); // schedule | process.nextTick
|
|
45
|
+
// this._tick(fn, arg); // (fn, arg) shape, like nextTick
|
|
46
|
+
|
|
47
|
+
let ready = []; // callbacks awaiting the next pump run: [fn, arg, fn, arg, ...]
|
|
48
|
+
let idle = []; // the recycled array of the generation just drained
|
|
49
|
+
let scheduled = false; // a pump entry exists in the real tick queue
|
|
50
|
+
|
|
51
|
+
// Read at resolve() time, not at module load, so a process can turn the mode
|
|
52
|
+
// on after the library is required (the suite gate runs the whole tree under
|
|
53
|
+
// the pump this way). One env getter per session construction, never per tick.
|
|
54
|
+
function envForced() {
|
|
55
|
+
return process.env.H2LEAN_BATCH_TICKS === '1';
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function pump() {
|
|
59
|
+
scheduled = false;
|
|
60
|
+
const list = ready;
|
|
61
|
+
// Re-entrant schedules during the drain push into `ready` -- the next
|
|
62
|
+
// generation -- and queue the next pump, which is node's tail-of-the-queue
|
|
63
|
+
// semantics: a callback scheduled while tick A runs executes after the
|
|
64
|
+
// ticks that were queued before A did.
|
|
65
|
+
ready = idle;
|
|
66
|
+
idle = list;
|
|
67
|
+
let i = 0;
|
|
68
|
+
try {
|
|
69
|
+
while (i < list.length) {
|
|
70
|
+
const fn = list[i];
|
|
71
|
+
fn(list[i + 1]);
|
|
72
|
+
i += 2;
|
|
73
|
+
}
|
|
74
|
+
} catch (err) {
|
|
75
|
+
// The ticks behind the throwing one stay queued on node too -- the drain
|
|
76
|
+
// aborts, the leftovers run at the next tick phase, if the loop gets
|
|
77
|
+
// there. Carry ours into the next generation, ahead of whatever the
|
|
78
|
+
// aborted drain scheduled there, and make sure a pump is queued. The
|
|
79
|
+
// failed pair is gone the way node's throwing tick is gone -- carrying it
|
|
80
|
+
// too re-runs the thrower at the head of every later generation and
|
|
81
|
+
// starves the survivors.
|
|
82
|
+
if (i + 2 < list.length) {
|
|
83
|
+
const rest = list.slice(i + 2);
|
|
84
|
+
for (let k = rest.length - 1; k >= 0; k--) {
|
|
85
|
+
ready.unshift(rest[k]);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
if (!scheduled && ready.length !== 0) {
|
|
89
|
+
scheduled = true;
|
|
90
|
+
process.nextTick(pump);
|
|
91
|
+
}
|
|
92
|
+
throw err;
|
|
93
|
+
} finally {
|
|
94
|
+
list.length = 0;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function schedule(fn, arg) {
|
|
99
|
+
ready.push(fn, arg);
|
|
100
|
+
if (!scheduled) {
|
|
101
|
+
scheduled = true;
|
|
102
|
+
process.nextTick(pump);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// The scheduler a session runs all of its ticks through. One reference per
|
|
107
|
+
// session keeps that session coherent -- its ticks must not mix routes, or
|
|
108
|
+
// a pump generation could reorder against its own native entries. The pump
|
|
109
|
+
// itself is one instance for the whole process: sessions in batch mode share
|
|
110
|
+
// it, sessions in the default mode never meet it.
|
|
111
|
+
function resolve(opts) {
|
|
112
|
+
return ((opts && opts.batchNextTick) ?? envForced()) ? schedule : process.nextTick;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
module.exports = { schedule, resolve };
|
package/lib/server.js
CHANGED
|
@@ -15,6 +15,15 @@
|
|
|
15
15
|
// keep-alive policy it served is session.setTimeout() plus
|
|
16
16
|
// the application destroying the session, which both stacks
|
|
17
17
|
// do implement. Harmless to keep, surprising to drop.
|
|
18
|
+
// * batchNextTick (createServer and createSecureServer; connect takes it too) is
|
|
19
|
+
// additive: with true, every deferred callback the session schedules runs through the
|
|
20
|
+
// shared pump of lib/fast-tick.js -- one real process.nextTick per event-loop turn
|
|
21
|
+
// instead of one per callback (OPTIMIZATION.md item 47: the stream arm -3.8%, the
|
|
22
|
+
// compat arm no measurable gain at saturation, which is why the default is node's own
|
|
23
|
+
// tick). The two behaviours the pump cannot reproduce -- a foreign tick's position
|
|
24
|
+
// between two of ours, and async-context propagation into the deferred callbacks --
|
|
25
|
+
// are in the fast-tick.js header; H2LEAN_BATCH_TICKS=1 fills in for an undefined
|
|
26
|
+
// option, so the suite pins the event orders with the pump on as well.
|
|
18
27
|
// * what this server advertises on the wire matches node: the first SETTINGS frame carries only
|
|
19
28
|
// the values the application asked for -- an EMPTY payload when it asked for nothing -- and a
|
|
20
29
|
// later server.updateSettings() reaches the NEXT connection, not the live sessions. The local
|
|
@@ -107,6 +116,10 @@ function initHttp2Server(server, options) {
|
|
|
107
116
|
// (internal/http2/util.js) takes it on a `typeof === 'number'` test alone -- so it rides
|
|
108
117
|
// through raw and the session constructor applies the same test and the same coercion.
|
|
109
118
|
maxSendHeaderBlockLength: opts.maxSendHeaderBlockLength,
|
|
119
|
+
// Rides through raw: the session resolves undefined (the default, node's own
|
|
120
|
+
// process.nextTick), true (the shared batched pump of lib/fast-tick.js) and
|
|
121
|
+
// explicit false alike, and H2LEAN_BATCH_TICKS=1 fills in for undefined only.
|
|
122
|
+
batchNextTick: opts.batchNextTick,
|
|
110
123
|
// The server `origins` option: each session sends one ORIGIN frame with these origins at
|
|
111
124
|
// connect. node only does it when the value is an array (core.js setupHandle), so it rides
|
|
112
125
|
// through unvalidated and Http2Session._start applies the same Array.isArray test.
|
package/lib/session.js
CHANGED
|
@@ -22,8 +22,9 @@ const { EventEmitter } = require('node:events');
|
|
|
22
22
|
const tls = require('node:tls');
|
|
23
23
|
const f = require('./frames');
|
|
24
24
|
const { Decoder, Encoder, HPackError, latin1, latin1Into } = require('./hpack');
|
|
25
|
-
const { Http2Stream, errorOf, streamErrorOf,
|
|
25
|
+
const { Http2Stream, errorOf, streamErrorOf, emitCloseNT, emitDrainNT } = require('./stream');
|
|
26
26
|
const FlatList = require('./flat-list');
|
|
27
|
+
const fastTick = require('./fast-tick');
|
|
27
28
|
// The process-wide inbound decode scratch: one list per module instance, shared by
|
|
28
29
|
// every session it serves, instead of one array per live session. Why sharing is
|
|
29
30
|
// safe: a header block's fill and its consumption are synchronous -- they happen
|
|
@@ -659,6 +660,12 @@ class Http2Session extends EventEmitter {
|
|
|
659
660
|
}
|
|
660
661
|
socket[kBoundSession] = this;
|
|
661
662
|
this._socket = socket;
|
|
663
|
+
// Every deferred callback this session schedules runs through ONE route:
|
|
664
|
+
// node's own tick queue (default) or the shared batched pump of
|
|
665
|
+
// lib/fast-tick.js (`batchNextTick: true`). Mixing routes inside one
|
|
666
|
+
// session would let a pump generation reorder against its own native
|
|
667
|
+
// entries, so streams and the compat objects copy this reference.
|
|
668
|
+
this._tick = fastTick.resolve(opts);
|
|
662
669
|
// The public `socket` accessor builds node's safe Proxy lazily and caches it here;
|
|
663
670
|
// close sets it to undefined so `session.socket` reads gone afterwards, as node does.
|
|
664
671
|
this._proxySocket = null;
|
|
@@ -946,7 +953,7 @@ class Http2Session extends EventEmitter {
|
|
|
946
953
|
// do the same, then subscribe. 'connect' was queued by _start ahead of this drain, so
|
|
947
954
|
// it fires first. An accepted createServer socket has nothing buffered here and takes
|
|
948
955
|
// the synchronous subscription below, the read path this file always had.
|
|
949
|
-
|
|
956
|
+
this._tick(() => {
|
|
950
957
|
if (this.destroyed) {
|
|
951
958
|
return;
|
|
952
959
|
}
|
|
@@ -976,7 +983,7 @@ class Http2Session extends EventEmitter {
|
|
|
976
983
|
// (setupHandle's destroyed branch), so the handlers at least run and clear out.
|
|
977
984
|
if (!this._connectEmitted) {
|
|
978
985
|
this._connectEmitted = true;
|
|
979
|
-
|
|
986
|
+
this._tick(() => this.emit('connect', this, this._socket));
|
|
980
987
|
}
|
|
981
988
|
return;
|
|
982
989
|
}
|
|
@@ -998,7 +1005,7 @@ class Http2Session extends EventEmitter {
|
|
|
998
1005
|
this.origin(...this._origins);
|
|
999
1006
|
}
|
|
1000
1007
|
this._connectEmitted = true;
|
|
1001
|
-
|
|
1008
|
+
this._tick(() => this.emit('connect', this, this._socket));
|
|
1002
1009
|
}
|
|
1003
1010
|
|
|
1004
1011
|
// ---- outbound queue --------------------------------------------------------
|
|
@@ -3065,7 +3072,7 @@ class Http2Session extends EventEmitter {
|
|
|
3065
3072
|
}
|
|
3066
3073
|
// the writable side accepted bytes again: res.on('drain') is how an
|
|
3067
3074
|
// application is allowed to resume writing after write() returned false
|
|
3068
|
-
|
|
3075
|
+
stream._tick(emitDrainNT, stream);
|
|
3069
3076
|
if (stream._endQueued) {
|
|
3070
3077
|
stream._endLocalSide();
|
|
3071
3078
|
}
|
|
@@ -3291,7 +3298,7 @@ class Http2Session extends EventEmitter {
|
|
|
3291
3298
|
// any of the five (out/probe-frameerror.js). This used to emit one on the session with
|
|
3292
3299
|
// the id and the code swapped and DATA hardcoded as the type; the outbound sites
|
|
3293
3300
|
// (_refuseSend, the PUSH_PROMISE bound) are the ones that match node's event.
|
|
3294
|
-
|
|
3301
|
+
stream._tick(() => stream.emit('error', errorOf('ERR_HTTP2_STREAM_ERROR', msg)));
|
|
3295
3302
|
}
|
|
3296
3303
|
|
|
3297
3304
|
_fatal(code, msg) {
|
|
@@ -3497,7 +3504,7 @@ class Http2Session extends EventEmitter {
|
|
|
3497
3504
|
// finishSessionClose() afterwards, which is the order the application sees: stream 'error',
|
|
3498
3505
|
// stream 'close', session 'error', session 'close'.
|
|
3499
3506
|
if (appError !== undefined) {
|
|
3500
|
-
|
|
3507
|
+
this._tick(() => this.emit('error', appError));
|
|
3501
3508
|
}
|
|
3502
3509
|
return this;
|
|
3503
3510
|
}
|
|
@@ -3517,7 +3524,7 @@ class Http2Session extends EventEmitter {
|
|
|
3517
3524
|
return;
|
|
3518
3525
|
}
|
|
3519
3526
|
this._closeEmitted = true;
|
|
3520
|
-
|
|
3527
|
+
this._tick(emitCloseNT, this);
|
|
3521
3528
|
}
|
|
3522
3529
|
|
|
3523
3530
|
_onSocketError(err) {
|
|
@@ -3623,7 +3630,7 @@ class Http2Session extends EventEmitter {
|
|
|
3623
3630
|
}
|
|
3624
3631
|
this._sendSettings();
|
|
3625
3632
|
if (cb) {
|
|
3626
|
-
|
|
3633
|
+
this._tick(cb);
|
|
3627
3634
|
}
|
|
3628
3635
|
}
|
|
3629
3636
|
|
package/lib/stream.js
CHANGED
|
@@ -120,6 +120,18 @@ function emitNT(target, name, arg) {
|
|
|
120
120
|
}
|
|
121
121
|
}
|
|
122
122
|
|
|
123
|
+
// One-arg emitters for the per-request deliveries: the batched tick route of
|
|
124
|
+
// lib/fast-tick.js carries (fn, arg) pairs, so these replace the
|
|
125
|
+
// `emitNT(target, 'close' | 'drain')` calls the hot paths used, without
|
|
126
|
+
// paying a closure per event (the reason emitNT exists, item 41).
|
|
127
|
+
function emitCloseNT(target) {
|
|
128
|
+
target.emit('close');
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function emitDrainNT(target) {
|
|
132
|
+
target.emit('drain');
|
|
133
|
+
}
|
|
134
|
+
|
|
123
135
|
function emitErrorCloseNT(target, err) {
|
|
124
136
|
target._errorEmitted = true;
|
|
125
137
|
target._streamErrored = err;
|
|
@@ -195,6 +207,13 @@ function finishNT(stream) {
|
|
|
195
207
|
// They were already going to run in the same tick drain, so folding them into
|
|
196
208
|
// one callback only removes one TickObject per request.
|
|
197
209
|
function emitEndCloseNT(target) {
|
|
210
|
+
if (target._finishFuse) {
|
|
211
|
+
// the finish work rides here, ahead of every state this tick flips: the separate
|
|
212
|
+
// tick it replaces ran in the same drain, directly before this callback, when the
|
|
213
|
+
// session reference and _closeDelivered were still what end() left them.
|
|
214
|
+
target._finishFuse = false;
|
|
215
|
+
finishNT(target);
|
|
216
|
+
}
|
|
198
217
|
target._closeDelivered = true;
|
|
199
218
|
// node clears the stream's session reference in _destroy, before any of the close
|
|
200
219
|
// events go out (internal/http2/core.js:2546) -- see _closeNow for why the drop waits
|
|
@@ -211,6 +230,10 @@ function emitEndCloseNT(target) {
|
|
|
211
230
|
// in one turn, with no 'end' owed. Marks the node-equivalent close moment -- see
|
|
212
231
|
// _closeDelivered above.
|
|
213
232
|
function emitCloseDeliveredNT(target) {
|
|
233
|
+
if (target._finishFuse) {
|
|
234
|
+
target._finishFuse = false;
|
|
235
|
+
finishNT(target);
|
|
236
|
+
}
|
|
214
237
|
target._closeDelivered = true;
|
|
215
238
|
target.session = undefined;
|
|
216
239
|
target.emit('close');
|
|
@@ -225,7 +248,7 @@ function autoCloseNT(stream) {
|
|
|
225
248
|
if (stream._destroyed) {
|
|
226
249
|
return;
|
|
227
250
|
}
|
|
228
|
-
|
|
251
|
+
stream._tick(emitEndNT, stream);
|
|
229
252
|
stream._destroy(NO_ERROR, undefined, true);
|
|
230
253
|
}
|
|
231
254
|
|
|
@@ -371,6 +394,9 @@ class Http2Stream extends Stream {
|
|
|
371
394
|
super();
|
|
372
395
|
this.id = id;
|
|
373
396
|
this.session = session;
|
|
397
|
+
// The session's one tick route -- node's queue or the batched pump of
|
|
398
|
+
// lib/fast-tick.js; every deferred callback of this stream goes through it.
|
|
399
|
+
this._tick = session._tick;
|
|
374
400
|
this.rstCode = 0;
|
|
375
401
|
// What respond() put on the wire, for the `sentHeaders` getter below: the caller's map
|
|
376
402
|
// plus the two values the encoder resolved. node keeps the same three pieces
|
|
@@ -454,6 +480,9 @@ class Http2Stream extends Stream {
|
|
|
454
480
|
// 'close' with writableFinished false and no 'finish').
|
|
455
481
|
this._writableFinished = false;
|
|
456
482
|
this._finishQueued = false;
|
|
483
|
+
// The finish work carried into a close-delivery tick scheduled by the same call
|
|
484
|
+
// of _endLocalSide (see the fuse there) instead of a nextTick of its own.
|
|
485
|
+
this._finishFuse = false;
|
|
457
486
|
}
|
|
458
487
|
|
|
459
488
|
get destroyed() {
|
|
@@ -1020,7 +1049,7 @@ class Http2Stream extends Stream {
|
|
|
1020
1049
|
// test/upstream-push.test.js, session-destroy).
|
|
1021
1050
|
const err = errorOf('ERR_STREAM_WRITE_AFTER_END', 'write after end');
|
|
1022
1051
|
if (cb) {
|
|
1023
|
-
|
|
1052
|
+
this._tick(cb, err);
|
|
1024
1053
|
}
|
|
1025
1054
|
if (!this._destroyed) {
|
|
1026
1055
|
this.destroy(err);
|
|
@@ -1032,7 +1061,7 @@ class Http2Stream extends Stream {
|
|
|
1032
1061
|
// the event is owed here instead. A reset or a session destroy is not
|
|
1033
1062
|
// this shape -- there node's errorOrDestroy is a no-op and no 'error'
|
|
1034
1063
|
// goes out (the session-destroy case of test/upstream-push.test.js).
|
|
1035
|
-
|
|
1064
|
+
this._tick(() => emitErrorOnlyNT(this, err));
|
|
1036
1065
|
}
|
|
1037
1066
|
return false;
|
|
1038
1067
|
}
|
|
@@ -1046,7 +1075,7 @@ class Http2Stream extends Stream {
|
|
|
1046
1075
|
// session's drain path). It reports the credit stall, nothing else.
|
|
1047
1076
|
this._needDrain = !canContinue;
|
|
1048
1077
|
if (cb) {
|
|
1049
|
-
|
|
1078
|
+
this._tick(cb);
|
|
1050
1079
|
}
|
|
1051
1080
|
return canContinue;
|
|
1052
1081
|
}
|
|
@@ -1069,7 +1098,7 @@ class Http2Stream extends Stream {
|
|
|
1069
1098
|
// _pumpEnd() for exactly that reason; write() after end IS the error -- see
|
|
1070
1099
|
// write() above.
|
|
1071
1100
|
if (cb) {
|
|
1072
|
-
|
|
1101
|
+
this._tick(cb);
|
|
1073
1102
|
}
|
|
1074
1103
|
return this;
|
|
1075
1104
|
}
|
|
@@ -1088,7 +1117,7 @@ class Http2Stream extends Stream {
|
|
|
1088
1117
|
// the auto-respond above already carried END_STREAM (a HEAD request):
|
|
1089
1118
|
// there is no body to send, RFC 9113 8.1 forbids one on a HEAD answer
|
|
1090
1119
|
if (cb) {
|
|
1091
|
-
|
|
1120
|
+
this._tick(cb);
|
|
1092
1121
|
}
|
|
1093
1122
|
return this;
|
|
1094
1123
|
}
|
|
@@ -1106,7 +1135,7 @@ class Http2Stream extends Stream {
|
|
|
1106
1135
|
this._endLocalSide();
|
|
1107
1136
|
}
|
|
1108
1137
|
if (cb) {
|
|
1109
|
-
|
|
1138
|
+
this._tick(cb);
|
|
1110
1139
|
}
|
|
1111
1140
|
return this;
|
|
1112
1141
|
}
|
|
@@ -1391,7 +1420,7 @@ class Http2Stream extends Stream {
|
|
|
1391
1420
|
try {
|
|
1392
1421
|
promisedId = this.session._allocPushSid();
|
|
1393
1422
|
} catch (err) {
|
|
1394
|
-
|
|
1423
|
+
this._tick(callback, err);
|
|
1395
1424
|
return;
|
|
1396
1425
|
}
|
|
1397
1426
|
|
|
@@ -1431,7 +1460,7 @@ class Http2Stream extends Stream {
|
|
|
1431
1460
|
// what keeps that shape; respond() reads _endQueued, write() refuses it.
|
|
1432
1461
|
stream._endQueued = true;
|
|
1433
1462
|
}
|
|
1434
|
-
|
|
1463
|
+
this._tick(() => callback(null, stream, h, 0));
|
|
1435
1464
|
}
|
|
1436
1465
|
|
|
1437
1466
|
// Readable-side control, node's contract: while paused no credit is released,
|
|
@@ -1460,7 +1489,7 @@ class Http2Stream extends Stream {
|
|
|
1460
1489
|
return;
|
|
1461
1490
|
}
|
|
1462
1491
|
this._resumeScheduled = true;
|
|
1463
|
-
|
|
1492
|
+
this._tick(() => {
|
|
1464
1493
|
this._resumeScheduled = false;
|
|
1465
1494
|
if (this._destroyed || this._flowing === false) {
|
|
1466
1495
|
return;
|
|
@@ -1879,7 +1908,7 @@ class Http2Stream extends Stream {
|
|
|
1879
1908
|
dest !== process.stdout && dest !== process.stderr;
|
|
1880
1909
|
const endFn = doEnd ? onend : unpipe;
|
|
1881
1910
|
if (src._eofEmitted === true) {
|
|
1882
|
-
|
|
1911
|
+
src._tick(endFn);
|
|
1883
1912
|
} else {
|
|
1884
1913
|
src.once('end', endFn);
|
|
1885
1914
|
}
|
|
@@ -2280,7 +2309,7 @@ class Http2Stream extends Stream {
|
|
|
2280
2309
|
return;
|
|
2281
2310
|
}
|
|
2282
2311
|
this._readableScheduled = true;
|
|
2283
|
-
|
|
2312
|
+
this._tick(() => {
|
|
2284
2313
|
this._readableScheduled = false;
|
|
2285
2314
|
if (this._destroyed || this._flowing === true ||
|
|
2286
2315
|
this.listenerCount('readable') === 0) {
|
|
@@ -2302,7 +2331,7 @@ class Http2Stream extends Stream {
|
|
|
2302
2331
|
return;
|
|
2303
2332
|
}
|
|
2304
2333
|
this._eofScheduled = true;
|
|
2305
|
-
|
|
2334
|
+
this._tick(() => {
|
|
2306
2335
|
this._eofScheduled = false;
|
|
2307
2336
|
if (this._destroyed || this._eofEmitted === true) {
|
|
2308
2337
|
return;
|
|
@@ -2370,7 +2399,7 @@ class Http2Stream extends Stream {
|
|
|
2370
2399
|
// the stream close after both; ending here sent the HEADERS only and the client
|
|
2371
2400
|
// waited for an answer that never came.
|
|
2372
2401
|
this._closeNow();
|
|
2373
|
-
|
|
2402
|
+
this._tick(emitEndCloseNT, this);
|
|
2374
2403
|
return;
|
|
2375
2404
|
}
|
|
2376
2405
|
if (this._state.state === ST_OPEN) {
|
|
@@ -2392,7 +2421,7 @@ class Http2Stream extends Stream {
|
|
|
2392
2421
|
}
|
|
2393
2422
|
return;
|
|
2394
2423
|
}
|
|
2395
|
-
|
|
2424
|
+
this._tick(emitEndNT, this);
|
|
2396
2425
|
}
|
|
2397
2426
|
|
|
2398
2427
|
// The writable-side 'finish' -- node's Writable fires it once every write has been
|
|
@@ -2409,7 +2438,7 @@ class Http2Stream extends Stream {
|
|
|
2409
2438
|
return;
|
|
2410
2439
|
}
|
|
2411
2440
|
this._finishQueued = true;
|
|
2412
|
-
|
|
2441
|
+
this._tick(finishNT, this);
|
|
2413
2442
|
}
|
|
2414
2443
|
|
|
2415
2444
|
_endLocalSide() {
|
|
@@ -2427,7 +2456,19 @@ class Http2Stream extends Stream {
|
|
|
2427
2456
|
if (this._queued === null) {
|
|
2428
2457
|
// every octet handed over has been accepted by the session: node's condition
|
|
2429
2458
|
// for the writable 'finish'
|
|
2430
|
-
this.
|
|
2459
|
+
if (!this._waitForTrailers && this._state.remoteClose === 1 && !this._destroyed) {
|
|
2460
|
+
// The graceful close below schedules a close-delivery tick in this same call,
|
|
2461
|
+
// and finishNT would have run in the same tick drain directly before it: the
|
|
2462
|
+
// finish work rides at the head of that tick instead of paying one of its own.
|
|
2463
|
+
// Measured (item 45): the tick of its own is ~0.05 us/req on the GET/empty-
|
|
2464
|
+
// response shape and the fuse recovers ~0.03 of it; what is left is the flag
|
|
2465
|
+
// flip and the emit, which the unpatched shape paid too. The trailers and
|
|
2466
|
+
// auto-close shapes keep their own tick: there the ordering against
|
|
2467
|
+
// 'wantTrailers'/autoCloseNT's setImmediate is what node pins.
|
|
2468
|
+
this._finishFuse = true;
|
|
2469
|
+
} else {
|
|
2470
|
+
this._scheduleFinish();
|
|
2471
|
+
}
|
|
2431
2472
|
}
|
|
2432
2473
|
if (this._state.state === ST_OPEN) {
|
|
2433
2474
|
this._state.state = ST_HALF_CLOSED_LOCAL;
|
|
@@ -2436,7 +2477,7 @@ class Http2Stream extends Stream {
|
|
|
2436
2477
|
// one shot: a later end() must not ask for trailers a second time. The flag
|
|
2437
2478
|
// itself stays set, because sendTrailers() is only allowed while it is set.
|
|
2438
2479
|
if (first) {
|
|
2439
|
-
|
|
2480
|
+
this._tick(wantTrailersNT, this);
|
|
2440
2481
|
}
|
|
2441
2482
|
return;
|
|
2442
2483
|
}
|
|
@@ -2494,10 +2535,10 @@ class Http2Stream extends Stream {
|
|
|
2494
2535
|
// stream.end then stream.close (test/event-order.test.js, scenario eof).
|
|
2495
2536
|
if (this._endPending || this._eofSeen === true) {
|
|
2496
2537
|
this._endPending = false;
|
|
2497
|
-
|
|
2538
|
+
this._tick(emitEndCloseNT, this);
|
|
2498
2539
|
return;
|
|
2499
2540
|
}
|
|
2500
|
-
|
|
2541
|
+
this._tick(emitCloseDeliveredNT, this);
|
|
2501
2542
|
}
|
|
2502
2543
|
|
|
2503
2544
|
_closeNow() {
|
|
@@ -2519,6 +2560,15 @@ class Http2Stream extends Stream {
|
|
|
2519
2560
|
if (this._destroyed) {
|
|
2520
2561
|
return;
|
|
2521
2562
|
}
|
|
2563
|
+
if (this._finishFuse) {
|
|
2564
|
+
// The end() in this turn fused its finish into a close-delivery tick that this
|
|
2565
|
+
// destroy now replaces. The destroy is not the hot shape: hand the finish back the
|
|
2566
|
+
// separate tick it would have had, scheduled before the close ticks below -- the
|
|
2567
|
+
// position (and the post-destroy session view) finishNT had when it was always
|
|
2568
|
+
// scheduled from _endLocalSide.
|
|
2569
|
+
this._finishFuse = false;
|
|
2570
|
+
this._tick(finishNT, this);
|
|
2571
|
+
}
|
|
2522
2572
|
this._destroyed = true;
|
|
2523
2573
|
// node's destroy does not touch state.flowing — the readableFlowing getter keeps
|
|
2524
2574
|
// reporting the last resume()/pause()/listener verdict even after the destroy
|
|
@@ -2562,7 +2612,7 @@ class Http2Stream extends Stream {
|
|
|
2562
2612
|
// node's closeStream emits 'aborted' before _destroy runs.
|
|
2563
2613
|
this.session = undefined;
|
|
2564
2614
|
if (err) {
|
|
2565
|
-
|
|
2615
|
+
this._tick(() => emitErrorCloseNT(this, err));
|
|
2566
2616
|
} else if (this._isConsuming() &&
|
|
2567
2617
|
(this._endPending || code === NO_ERROR || code === frames.CANCEL)) {
|
|
2568
2618
|
// node's _destroy (internal/http2/core.js:2527) calls `this.push(null)`
|
|
@@ -2580,9 +2630,9 @@ class Http2Stream extends Stream {
|
|
|
2580
2630
|
// reader is cancelled by the destroy bit and the owed 'end' never goes out
|
|
2581
2631
|
// (test/event-order.test.js, scenario reset).
|
|
2582
2632
|
this._endPending = false;
|
|
2583
|
-
|
|
2633
|
+
this._tick(emitEndCloseNT, this);
|
|
2584
2634
|
} else {
|
|
2585
|
-
|
|
2635
|
+
this._tick(emitCloseNT, this);
|
|
2586
2636
|
}
|
|
2587
2637
|
}
|
|
2588
2638
|
|
|
@@ -2949,9 +2999,9 @@ function createStreamIterator(stream, options) {
|
|
|
2949
2999
|
// The one-shot trio below can never fire again, so this is where that
|
|
2950
3000
|
// verdict is reproduced.
|
|
2951
3001
|
if (stream.readableEnded === true) {
|
|
2952
|
-
|
|
3002
|
+
stream._tick(() => settle(null));
|
|
2953
3003
|
} else if (stream.destroyed === true) {
|
|
2954
|
-
|
|
3004
|
+
stream._tick(() => settle(errorOf('ERR_STREAM_PREMATURE_CLOSE', 'Premature close')));
|
|
2955
3005
|
}
|
|
2956
3006
|
}
|
|
2957
3007
|
|
|
@@ -3433,4 +3483,4 @@ function streamErrorOf(code) {
|
|
|
3433
3483
|
`Stream closed with error code ${frames.ERROR_NAMES[code] || code}`);
|
|
3434
3484
|
}
|
|
3435
3485
|
|
|
3436
|
-
module.exports = { Http2Stream, errorOf, streamErrorOf, emitNT };
|
|
3486
|
+
module.exports = { Http2Stream, errorOf, streamErrorOf, emitNT, emitCloseNT, emitDrainNT };
|
package/package.json
CHANGED