h2lean 0.17.5 → 0.18.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.
@@ -819,6 +819,9 @@ const BODY_MIN = 4096;
819
819
  // First size of the staging buffer; doubled per frame that does not fit, so a batch of
820
820
  // header blocks costs one allocation and one copy of each block.
821
821
  const STAGE_MIN = 8192;
822
+ // Cap on the server-wide pool of completed staging buffers, so the memory retained
823
+ // for reuse is bounded independently of how many sessions the server accepts.
824
+ const MAX_STAGE_POOL = 32;
822
825
 
823
826
  // The 'stream' event carries node's options object. Two constants instead of a
824
827
  // literal per request: the payload never changes, and 1.9M requests is 1.9M
@@ -1367,12 +1370,14 @@ class Http2ServerSession extends EventEmitter {
1367
1370
  this._stage = null;
1368
1371
  this._stageLen = 0; // bytes of _stage not yet queued
1369
1372
  this._stageCap = STAGE_MIN; // size of the next staging buffer
1370
- // Buffers that have been handed to the socket and completed are kept here and
1371
- // handed back to _need instead of allocating a new one. _pending collects the
1372
- // full stage buffers of a batch; _returning is the batch whose write callback
1373
- // is still owed, so only one list is in flight at a time.
1373
+ this._reuseBuffers = opts.reuseBuffers !== false;
1374
+ // The pool belongs to the server: a stage completed on one session can be reused
1375
+ // by another. performServerHandshake has no server and carries its own pool.
1374
1376
  // OPTIMIZATION.md item 69: this buys memory, not request-level time.
1375
- this._recycled = [];
1377
+ this._pool = this._reuseBuffers
1378
+ ? (opts.stagingPool === undefined ? [] : opts.stagingPool) : null;
1379
+ // _pending collects the full stage buffers of a batch; _returning is the batch
1380
+ // whose write callback is still owed, so only one list is in flight at a time.
1376
1381
  this._pending = [];
1377
1382
  this._returning = null;
1378
1383
  this._outLen = 0; // queued bytes the socket has not been given
@@ -1656,7 +1661,7 @@ class Http2ServerSession extends EventEmitter {
1656
1661
  cap *= 2;
1657
1662
  }
1658
1663
  this._stageCap = Math.min(cap, MAX_OUT_BATCH);
1659
- const reused = this._recycled.pop();
1664
+ const reused = this._pool === null ? undefined : this._pool.pop();
1660
1665
  this._stage = reused !== undefined && reused.length >= cap
1661
1666
  ? reused : Buffer.allocUnsafe(cap);
1662
1667
  }
@@ -1728,7 +1733,6 @@ class Http2ServerSession extends EventEmitter {
1728
1733
  this._esRequeue = [];
1729
1734
  this._pending = [];
1730
1735
  this._returning = null;
1731
- this._recycled = [];
1732
1736
  return;
1733
1737
  }
1734
1738
  if (this._writing) {
@@ -1783,9 +1787,12 @@ class Http2ServerSession extends EventEmitter {
1783
1787
  // socket's no longer, so _need can write into one of them again.
1784
1788
  const done = this._returning;
1785
1789
  this._returning = null;
1786
- if (done !== null && this._recycled.length < 4) {
1790
+ if (done !== null && this._pool !== null) {
1787
1791
  for (const b of done) {
1788
- this._recycled.push(b);
1792
+ if (this._pool.length >= MAX_STAGE_POOL) {
1793
+ break;
1794
+ }
1795
+ this._pool.push(b);
1789
1796
  }
1790
1797
  }
1791
1798
  // Dropping the batch is the whole release: the frames were never assembled into
@@ -102,7 +102,14 @@ function initHttp2Server(server, options) {
102
102
  const opts = { ...(options || {}) };
103
103
  validateRemoteCustomSettings(opts);
104
104
  server.settings = { ...OUR_DEFAULTS, ...opts.settings };
105
+ // One pool for every session of this server: a staging buffer completed under one
106
+ // connection can be taken by another, and the cap is server-wide rather than
107
+ // per-session. performServerHandshake carries its own single-session pool.
108
+ server._stagingPool = [];
105
109
  server._sessionOptions = {
110
+ stagingPool: server._stagingPool,
111
+ // Default on; `false` keeps the allocate-per-batch behaviour of the pool opt-out.
112
+ reuseBuffers: opts.reuseBuffers !== false,
106
113
  settings: server.settings,
107
114
  // Which values the application asked for. server.settings is what the session's guards read,
108
115
  // this list is what the peer is told, and the two are deliberately not the same object: node
@@ -601,6 +608,8 @@ function performServerHandshake(socket, options) {
601
608
  throw err;
602
609
  }
603
610
  const opts = { ...options };
611
+ // No server object here: give this one session its own staging-buffer pool.
612
+ opts.stagingPool = [];
604
613
  if (opts.settings !== undefined &&
605
614
  (opts.settings === null || typeof opts.settings !== 'object' || Array.isArray(opts.settings))) {
606
615
  const err = new TypeError(
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "h2lean",
3
- "version": "0.17.5",
3
+ "version": "0.18.0",
4
4
  "description": "Pure-JavaScript HTTP/2 for Node.js — a drop-in node:http2 replacement (server and client, h2c and TLS) with zero dependencies",
5
5
  "main": "index.js",
6
6
  "files": [