nish 0.42.0 → 0.44.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.
@@ -104,14 +104,16 @@
104
104
  * a whole request head (`headTimeout`, against slowloris); the slots are the
105
105
  * pool's size.
106
106
  *
107
- * `Http1Server` builds a slot's connection on the first accept into it and
108
- * keeps it, so a pool sized for its peak costs at start-up only what does not
109
- * depend on the size, and holds at most `size` connections once the peak has
110
- * come; `Http1TlsServer` does the same for a slot's connection and, through
111
- * `TlsTcpServer`, its TLS state. Every buffer is allocated with its slot or
107
+ * `Http1Server` lends a connection to a slot only while it is busy and builds
108
+ * one only when none is spare, so a pool sized for its peak costs at start-up
109
+ * only what does not depend on the size, and holds as many connections as were
110
+ * busy at once; `Http1TlsServer` builds a slot's connection on its first
111
+ * accept and keeps it, with its TLS state through
112
+ * `TlsTcpServer`. Every buffer is allocated with its slot or
112
113
  * grown to its cap on first use and kept, so a warmed slot serves request after
113
- * request with `Arena.used()` flat. Accepting a WebSocket allocates (the accept
114
- * key), once a connection.
114
+ * request with `Arena.used()` flat. Accepting a WebSocket computes the accept
115
+ * key and writes the 101 into storage the slot keeps, built on its first
116
+ * accept, so later handshakes leave the arena flat too.
115
117
  *
116
118
  * **Output is borrowed.** The pools' slots share an `Http1OutputPool`: a
117
119
  * connection takes an `outputSize` buffer when it first needs room to write
@@ -168,9 +170,10 @@ import {
168
170
  WsDecoder,
169
171
  websocketClosePayload,
170
172
  websocketFrameSize,
171
- websocketRequestKey,
172
- websocketUpgradeResponse,
173
+ WsAcceptKey,
174
+ websocketRequestKeyField,
173
175
  websocketWriteFrame,
176
+ websocketWriteUpgradeResponse,
174
177
  } from "nish/net/websocket"
175
178
  import { TlsServerConfig } from "nish/net/tls"
176
179
  import { HTTP_DATE_LAST_SECOND, HTTP_DATE_SIZE, httpDateWrite } from "nish/net/http-fields"
@@ -865,6 +868,11 @@ export class Http1Connection {
865
868
  * the slot's later peers.
866
869
  */
867
870
  ws: WsDecoder | null
871
+ /**
872
+ * The accept key's storage: made on the slot's first WebSocket accept, like
873
+ * `ws`, then kept, so a later handshake allocates nothing.
874
+ */
875
+ private acceptKey: WsAcceptKey | null
868
876
  /** `H1_BODY`'s and `H1_WS_MESSAGE`'s bytes. */
869
877
  data: u8[]
870
878
  /** A ping's payload, kept until its pong fits: empty until the first ping. */
@@ -912,6 +920,11 @@ export class Http1Connection {
912
920
  * still has that count, `respond` neither compares nor keeps. -1 while on.
913
921
  */
914
922
  private headOff: i64 = -1
923
+ /**
924
+ * The accept serial of the peer whose responses the kept head was kept
925
+ * for, -1 for none: `lendTo` drops a kept head lent to another peer.
926
+ */
927
+ private keptFor: i64 = -1
915
928
  /**
916
929
  * How many heads `respond` has copied from the kept one rather than written.
917
930
  *
@@ -960,6 +973,7 @@ export class Http1Connection {
960
973
  this.parser.keepText = false
961
974
  this.parser.maxChunk = config.chunkSize
962
975
  this.ws = null
976
+ this.acceptKey = null
963
977
  this.noOutput = []
964
978
  this.output = this.noOutput
965
979
  this.pool = null
@@ -1019,6 +1033,7 @@ export class Http1Connection {
1019
1033
  this.headStatus = 0
1020
1034
  this.headMisses = 0
1021
1035
  this.headOff = -1
1036
+ this.keptFor = -1
1022
1037
  this.lingerSince = 0
1023
1038
  this.lingered = -1
1024
1039
  this.mss = 0
@@ -1243,6 +1258,64 @@ export class Http1Connection {
1243
1258
  return this.state === H1_S_HTTP && !this.requestOpen && !this.headTimed && !this.wantsWrite()
1244
1259
  }
1245
1260
 
1261
+ /**
1262
+ * Whether the connection holds nothing its peer needs kept between
1263
+ * requests: `betweenRequests`, with no output borrowed, no write held back
1264
+ * or pong owed, the peer's stream not ended, no close asked for, no
1265
+ * lingering close, no program-set `mss`, and no byte fed and unread. Such a
1266
+ * connection is the same for any peer, so `Http1Server` lends it to the
1267
+ * next slot that reads and keeps only the slot's `lastActive` and
1268
+ * `socketMss` while it rests.
1269
+ *
1270
+ * @returns true when the connection may serve another peer's next request as it is
1271
+ * @internal Called by `Http1Server.flush` in this module to free a resting slot's connection.
1272
+ */
1273
+ /**
1274
+ * Takes the connection for the peer accepted as `owner`, dropping its kept
1275
+ * head unless that peer's own responses wrote it, so that how long
1276
+ * `respond` takes tells one peer nothing of another's heads (H1-7).
1277
+ *
1278
+ * @param owner the accept serial of the peer the connection serves next
1279
+ * @internal Called by `Http1Server` in this module as it lends a connection to a slot.
1280
+ */
1281
+ lendTo(owner: i64): void {
1282
+ if (this.keptFor !== owner) {
1283
+ this.headLength = 0
1284
+ this.headStatus = 0
1285
+ this.headMisses = 0
1286
+ this.headOff = -1
1287
+ this.keptFor = owner
1288
+ }
1289
+ }
1290
+
1291
+ /**
1292
+ * Whether the connection holds nothing a resting keep-alive slot must keep:
1293
+ * speaking HTTP with no request open, no head clock started, no output
1294
+ * borrowed or unsent, no write held or pong owed, the peer's stream not
1295
+ * ended, no close asked for or lingering, no program-set `mss`, and its
1296
+ * parser at rest. `Http1Server` then gives the connection back for another
1297
+ * busy slot.
1298
+ *
1299
+ * @returns true when the connection may be lent to another slot.
1300
+ * @internal Called by `Http1Server` in this module at `flush`.
1301
+ */
1302
+ rests(): boolean {
1303
+ return (
1304
+ this.state === H1_S_HTTP &&
1305
+ !this.requestOpen &&
1306
+ !this.headTimed &&
1307
+ this.outputCap === 0 &&
1308
+ this.outputEnd === this.outputStart &&
1309
+ !this.writeHeld &&
1310
+ !this.pongPending &&
1311
+ !this.inputEnded &&
1312
+ !this.closeAfter &&
1313
+ this.lingered < 0 &&
1314
+ this.mss === 0 &&
1315
+ this.parser.resting()
1316
+ )
1317
+ }
1318
+
1246
1319
  /** The room at the end of the output, once what was sent is moved out of the way. */
1247
1320
  private room(): i32 {
1248
1321
  // Tested here, so that the usual case, an output already held, is no call.
@@ -2101,26 +2174,45 @@ export class Http1Connection {
2101
2174
  if (!this.answerable()) {
2102
2175
  return H1_CLOSED
2103
2176
  }
2104
- const key: string | null = websocketRequestKey(this.parser)
2105
- if (key === null) {
2177
+ const p: Http1Parser = this.parser
2178
+ const field: i32 = websocketRequestKeyField(p)
2179
+ const spans: i32[] = p.spans
2180
+ if (field < 0 || 4 * field + 3 >= toI32(spans.length)) {
2106
2181
  return H1_INVALID
2107
2182
  }
2108
- const head: u8[] | null = websocketUpgradeResponse(key, protocol)
2109
- if (head === null) {
2183
+ const accept: WsAcceptKey = this.acceptStorage()
2184
+ if (!accept.compute(p.head, spans[4 * field + 2], spans[4 * field + 3])) {
2185
+ return H1_INVALID
2186
+ }
2187
+ // Written past `outputEnd`, where nothing live is, and kept only when it
2188
+ // fits beside the control reserve.
2189
+ const room: i32 = this.bodyRoom()
2190
+ const end: i32 = websocketWriteUpgradeResponse(this.output, this.outputEnd, accept.value, protocol)
2191
+ if (end === HTTP1_REFUSED) {
2110
2192
  return H1_INVALID
2111
2193
  }
2112
- const n: i32 = toI32(head.length)
2113
- if (n > this.bodyRoom()) {
2194
+ if (end < 0 || end - this.outputEnd > room) {
2114
2195
  this.writeHeld = true
2115
2196
  return H1_AGAIN
2116
2197
  }
2117
- this.outputEnd = http1ServerCopy(this.output, this.outputEnd, head, H1_ZERO, n)
2198
+ this.outputEnd = end
2118
2199
  this.response = H1_R_DONE
2119
2200
  this.upgraded = true
2120
2201
  this.closeAfter = false
2121
2202
  return 0
2122
2203
  }
2123
2204
 
2205
+ /** The slot's `WsAcceptKey`, made on its first WebSocket accept and kept. */
2206
+ private acceptStorage(): WsAcceptKey {
2207
+ const made: WsAcceptKey | null = this.acceptKey
2208
+ if (made !== null) {
2209
+ return made
2210
+ }
2211
+ const fresh: WsAcceptKey = new WsAcceptKey()
2212
+ this.acceptKey = fresh
2213
+ return fresh
2214
+ }
2215
+
2124
2216
  /**
2125
2217
  * Sends one WebSocket frame, unmasked as a server's are (§5.1): a whole
2126
2218
  * text or binary message, a fragment of one (`fin` false, then
@@ -2337,14 +2429,36 @@ const http1ExpireFloor = (config: Http1Config): i64 => {
2337
2429
  * A pool of HTTP/1.1 connections over plain TCP on one listening socket.
2338
2430
  * `slot`s are the indices `accept` answers; a slot is the program's from
2339
2431
  * `accept` until it calls `close`, and its descriptor is `fd(slot)`.
2432
+ *
2433
+ * A slot holds an `Http1Connection` only while it has something to keep: a
2434
+ * request read or answered, bytes fed and unread, output to send, or a
2435
+ * WebSocket. One that rests between requests (`Http1Connection.rests`) gives
2436
+ * its connection back at `flush` and keeps only its descriptor, its
2437
+ * interest, its `lastActive` and its socket's MSS; the next slot that reads
2438
+ * takes a resting connection, keeping its response head only if it was kept
2439
+ * for that slot's own peer (H1-7). So the pool builds
2440
+ * as many connections as are busy at once, not one per open slot, and an
2441
+ * idle keep-alive costs its slot a few words.
2340
2442
  */
2341
2443
  export class Http1Server {
2342
2444
  private config: Http1Config
2343
2445
  /**
2344
- * The connections built so far, slot `k`'s at `k`: one per slot ever
2345
- * accepted into, and slot 0's once `connection` has asked for it.
2446
+ * The connections built so far, as many as were ever busy at once: each is
2447
+ * a slot's (`at`) or spare.
2346
2448
  */
2347
2449
  connections: Http1Connection[]
2450
+ /** Each used slot's connection, an index into `connections`, or -1 while the slot rests. */
2451
+ private at: i32[]
2452
+ /** The indices in `connections` of the connections no slot holds, a stack. */
2453
+ private spare: i32[]
2454
+ /** Each resting slot's `lastActive` (`monotonicNanos`), which its connection carried. */
2455
+ private rested: i64[]
2456
+ /** Each resting slot's `socketMss`, which its connection carried. */
2457
+ private restedMss: i32[]
2458
+ /** Each used slot's accept serial, which keys the kept head of a connection lent to it (H1-7). */
2459
+ private owner: i64[]
2460
+ /** How many connections `accept` has taken: the next one's serial. */
2461
+ private accepts: i64 = 0
2348
2462
  /**
2349
2463
  * The `Date` every slot's responses carry, formatted once a second for all
2350
2464
  * of them and read from the clock once per serve call: `readable` and
@@ -2391,10 +2505,10 @@ export class Http1Server {
2391
2505
 
2392
2506
  /**
2393
2507
  * A pool of `size` slots, at least one, serving `config` on `listener`.
2394
- * No slot is built here: a slot's connection and its buffers are
2395
- * allocated by the first `accept` into it and kept, so a pool sized for a
2396
- * peak costs at start-up only the read scratch, the shared `Date` and the
2397
- * output pool, and at most `size` connections once the peak has come.
2508
+ * No slot is built here: a connection is built the first time a slot
2509
+ * reads and none rests spare, and kept, so a pool sized for a peak costs
2510
+ * at start-up only the read scratch, the shared `Date` and the output
2511
+ * pool, and at most as many connections as were ever busy at once.
2398
2512
  *
2399
2513
  * @param config the caps every slot's connection is held to
2400
2514
  * @param listener the listening socket from `tcpListen`, which stays the program's to close
@@ -2404,6 +2518,11 @@ export class Http1Server {
2404
2518
  this.config = config
2405
2519
  this.listener = listener
2406
2520
  this.connections = []
2521
+ this.at = []
2522
+ this.spare = []
2523
+ this.rested = []
2524
+ this.restedMss = []
2525
+ this.owner = []
2407
2526
  this.fds = []
2408
2527
  this.armed = []
2409
2528
  this.free = []
@@ -2416,7 +2535,7 @@ export class Http1Server {
2416
2535
  this.peer = new Array<u8>(H1_ADDRESS_SIZE)
2417
2536
  }
2418
2537
 
2419
- /** A new connection on this pool's outputs and `Date`, for the next slot built. */
2538
+ /** A new connection on this pool's outputs and `Date`, appended to `connections`. */
2420
2539
  private build(): Http1Connection {
2421
2540
  const conn: Http1Connection = new Http1Connection(this.config, this.date)
2422
2541
  conn.pool = this.outputs
@@ -2473,20 +2592,82 @@ export class Http1Server {
2473
2592
  }
2474
2593
 
2475
2594
  /**
2476
- * The connection of `slot`. A slot never accepted into, or out of range,
2477
- * names slot 0's connection, which this builds when no slot has been yet
2478
- * (the first `accept` then takes it, restarted).
2595
+ * The connection of `slot`. A resting slot is given one here, a spare or
2596
+ * a new one, which it keeps until it rests again at a `flush`: so a
2597
+ * program holds the answer only within a serve call, and asks again in
2598
+ * the next. A free slot, or one out of range, names the first connection
2599
+ * built, which this builds when none has been; it is another slot's or
2600
+ * spare, so read nothing from it.
2479
2601
  *
2480
2602
  * @param slot the slot `accept` answered
2481
2603
  * @returns the slot's connection, which the pool keeps; read its events and write its response through it
2482
2604
  */
2483
2605
  connection(slot: i32): Http1Connection {
2484
- if (slot >= 0 && slot < toI32(this.connections.length)) {
2485
- return this.connections[slot]
2606
+ if (this.holds(slot) && slot < toI32(this.at.length)) {
2607
+ return this.attach(slot)
2486
2608
  }
2487
2609
  return this.connections.length > 0 ? this.connections[0] : this.build()
2488
2610
  }
2489
2611
 
2612
+ /**
2613
+ * Whether `slot` holds a connection that rests between requests and has
2614
+ * given its `Http1Connection` back: nothing is open, unread or unsent on
2615
+ * it, so closing it loses nothing. It does not give the slot a connection,
2616
+ * as `connection` does.
2617
+ *
2618
+ * @param slot the slot index to ask about, any value
2619
+ * @returns true when the slot is accepted into and resting; false when it is free, out of range or busy
2620
+ */
2621
+ resting(slot: i32): boolean {
2622
+ return this.holds(slot) && slot < toI32(this.at.length) && this.at[slot] < 0
2623
+ }
2624
+
2625
+ /**
2626
+ * How many connections no slot holds now, of the `connections` built.
2627
+ *
2628
+ * @returns the count of spare connections, which a resting slot that reads takes before one is built
2629
+ */
2630
+ spares(): i32 {
2631
+ return toI32(this.spare.length)
2632
+ }
2633
+
2634
+ /** `slot`'s connection, which it holds, given one when it rests: carrying its descriptor, `lastActive` and MSS. */
2635
+ private attach(slot: i32): Http1Connection {
2636
+ const k: i32 = this.at[slot]
2637
+ if (k >= 0) {
2638
+ return this.connections[k]
2639
+ }
2640
+ const j: i32 = this.spare.length > 0 ? this.spare[this.spare.length - 1] : -1
2641
+ if (j >= 0) {
2642
+ this.spare.pop()
2643
+ }
2644
+ const conn: Http1Connection = j >= 0 ? this.connections[j] : this.build()
2645
+ this.at[slot] = j >= 0 ? j : toI32(this.connections.length) - 1
2646
+ conn.fd = this.fds[slot]
2647
+ conn.lastActive = this.rested[slot]
2648
+ conn.socketMss = this.restedMss[slot]
2649
+ conn.lendTo(this.owner[slot])
2650
+ return conn
2651
+ }
2652
+
2653
+ /** Gives back `slot`'s connection when it rests, keeping what the slot needs of it. */
2654
+ private rest(slot: i32): void {
2655
+ const k: i32 = this.at[slot]
2656
+ if (k < 0) {
2657
+ return
2658
+ }
2659
+ const conn: Http1Connection = this.connections[k]
2660
+ if (!conn.rests()) {
2661
+ return
2662
+ }
2663
+ this.rested[slot] = conn.lastActive
2664
+ this.restedMss[slot] = conn.socketMss
2665
+ conn.fd = -1
2666
+ conn.socketMss = 0
2667
+ this.at[slot] = -1
2668
+ this.spare.push(k)
2669
+ }
2670
+
2490
2671
  /**
2491
2672
  * Accepts the next waiting connection into a free slot and answers the
2492
2673
  * slot, which the program adds to its loop with `H1_WANT_READ`; -11 when
@@ -2511,23 +2692,34 @@ export class Http1Server {
2511
2692
  k = toI32(this.fds.length)
2512
2693
  this.fds.push(H1_FREE)
2513
2694
  this.armed.push(H1_WANT_READ)
2695
+ this.at.push(toI32(-1))
2696
+ this.rested.push(toI64(0))
2697
+ this.restedMss.push(toI32(0))
2698
+ this.owner.push(toI64(0))
2514
2699
  }
2515
- if (k < 0 || k >= toI32(this.fds.length) || k >= toI32(this.armed.length)) {
2700
+ if (
2701
+ k < 0 ||
2702
+ k >= toI32(this.fds.length) ||
2703
+ k >= toI32(this.armed.length) ||
2704
+ k >= toI32(this.at.length) ||
2705
+ k >= toI32(this.rested.length) ||
2706
+ k >= toI32(this.restedMss.length) ||
2707
+ k >= toI32(this.owner.length)
2708
+ ) {
2516
2709
  netClose(fd)
2517
2710
  return H1_POOL_FULL
2518
2711
  }
2519
- const conn: Http1Connection = k < toI32(this.connections.length) ? this.connections[k] : this.build()
2520
2712
  this.fds[k] = fd
2521
2713
  this.armed[k] = H1_WANT_READ
2522
- conn.restart()
2523
- conn.fd = fd
2714
+ // The slot starts resting: it is given a connection when it first reads.
2715
+ this.at[k] = -1
2716
+ this.rested[k] = monotonicNanos()
2717
+ this.owner[k] = this.accepts
2718
+ this.accepts += 1
2524
2719
  // Once a connection, not a request, so a keep-alive request costs no
2525
2720
  // syscall: a path whose MSS moves after the handshake is not followed.
2526
2721
  const mss: i32 = netMss(fd)
2527
- if (mss > 0) {
2528
- conn.socketMss = mss
2529
- }
2530
- conn.lastActive = monotonicNanos()
2722
+ this.restedMss[k] = mss > 0 ? mss : 0
2531
2723
  return k
2532
2724
  }
2533
2725
 
@@ -2545,7 +2737,7 @@ export class Http1Server {
2545
2737
  return H1_DONE
2546
2738
  }
2547
2739
  this.date.hold()
2548
- const conn: Http1Connection = this.connections[slot]
2740
+ const conn: Http1Connection = this.attach(slot)
2549
2741
  const fd: i32 = this.fds[slot]
2550
2742
  let moved: boolean = false
2551
2743
  while (true) {
@@ -2609,7 +2801,11 @@ export class Http1Server {
2609
2801
  if (!this.holds(slot)) {
2610
2802
  return H1_ERROR
2611
2803
  }
2612
- const conn: Http1Connection = this.connections[slot]
2804
+ if (this.at[slot] < 0) {
2805
+ // A resting slot has read nothing since it was answered.
2806
+ return H1_NEED_MORE
2807
+ }
2808
+ const conn: Http1Connection = this.connections[this.at[slot]]
2613
2809
  if (conn.lingering()) {
2614
2810
  // Its `H1_ERROR` was answered before the lingering close began.
2615
2811
  return H1_NEED_MORE
@@ -2631,6 +2827,7 @@ export class Http1Server {
2631
2827
  * shut and what the client still sends is drained, answering
2632
2828
  * `H1_WANT_READ` until the client's end of stream, `lingerBytes`, or
2633
2829
  * `expire` past `lingerTimeout`; `next` answers `H1_NEED_MORE` meanwhile.
2830
+ * A slot whose connection then rests gives it back (see the class comment).
2634
2831
  *
2635
2832
  * @param slot the slot `accept` answered
2636
2833
  * @returns the `H1_WANT_` bits to arm the slot with, or `H1_DONE` when the program should `close` it
@@ -2638,7 +2835,11 @@ export class Http1Server {
2638
2835
  flush(slot: i32): i32 {
2639
2836
  this.date.unhold()
2640
2837
  const wants: i32 = this.settle(slot)
2641
- if (wants !== H1_DONE || !this.holds(slot)) {
2838
+ if (!this.holds(slot)) {
2839
+ return wants
2840
+ }
2841
+ if (wants !== H1_DONE) {
2842
+ this.rest(slot)
2642
2843
  return wants
2643
2844
  }
2644
2845
  // A refusal is sent: shut the write side and drain what the client still
@@ -2646,7 +2847,7 @@ export class Http1Server {
2646
2847
  // Closed with those bytes unread, the socket would answer them with a
2647
2848
  // reset that can destroy the response before the client reads it (H1-2).
2648
2849
  const now: i64 = monotonicNanos()
2649
- const conn: Http1Connection = this.connections[slot]
2850
+ const conn: Http1Connection = this.attach(slot)
2650
2851
  if (!conn.lingerStart(now) || netShutdown(this.fds[slot], H1_SHUT_WRITE) < 0) {
2651
2852
  return H1_DONE
2652
2853
  }
@@ -2664,7 +2865,7 @@ export class Http1Server {
2664
2865
  * `lastActive` stays, and `expire` closes the slot past `lingerTimeout`.
2665
2866
  */
2666
2867
  private drain(slot: i32): i32 {
2667
- const conn: Http1Connection = this.connections[slot]
2868
+ const conn: Http1Connection = this.attach(slot)
2668
2869
  const fd: i32 = this.fds[slot]
2669
2870
  while (true) {
2670
2871
  const n: i32 = netRead(fd, this.scratch, H1_ZERO, toI32(this.scratch.length))
@@ -2682,7 +2883,11 @@ export class Http1Server {
2682
2883
  if (!this.holds(slot)) {
2683
2884
  return H1_DONE
2684
2885
  }
2685
- const conn: Http1Connection = this.connections[slot]
2886
+ if (this.at[slot] < 0) {
2887
+ // A resting slot holds nothing to send and has room to read.
2888
+ return H1_WANT_READ
2889
+ }
2890
+ const conn: Http1Connection = this.connections[this.at[slot]]
2686
2891
  if (conn.lingering()) {
2687
2892
  return this.drain(slot)
2688
2893
  }
@@ -2708,7 +2913,7 @@ export class Http1Server {
2708
2913
 
2709
2914
  /** Writes what `slot`'s connection holds, which it holds, until the socket would block. */
2710
2915
  private send(slot: i32): void {
2711
- const conn: Http1Connection = this.connections[slot]
2916
+ const conn: Http1Connection = this.attach(slot)
2712
2917
  const fd: i32 = this.fds[slot]
2713
2918
  const pending: i32 = conn.outputEnd - conn.outputStart
2714
2919
  while (conn.wantsWrite()) {
@@ -2750,7 +2955,8 @@ export class Http1Server {
2750
2955
  }
2751
2956
 
2752
2957
  /**
2753
- * Closes `slot`'s socket, whatever is left unsent, and frees the slot.
2958
+ * Closes `slot`'s socket, whatever is left unsent, and frees the slot. Its
2959
+ * connection, when it holds one, is aborted and restarted, and is spare.
2754
2960
  *
2755
2961
  * @param slot the slot to close; a free or unknown one is ignored
2756
2962
  */
@@ -2761,8 +2967,13 @@ export class Http1Server {
2761
2967
  netClose(this.fds[slot])
2762
2968
  this.fds[slot] = H1_FREE
2763
2969
  this.free.push(slot)
2764
- if (slot < toI32(this.connections.length)) {
2765
- this.connections[slot].abort()
2970
+ if (slot < toI32(this.at.length) && this.at[slot] >= 0) {
2971
+ const k: i32 = this.at[slot]
2972
+ const conn: Http1Connection = this.connections[k]
2973
+ conn.abort()
2974
+ conn.restart()
2975
+ this.at[slot] = -1
2976
+ this.spare.push(k)
2766
2977
  }
2767
2978
  }
2768
2979
 
@@ -2780,14 +2991,33 @@ export class Http1Server {
2780
2991
  this.expireScans = this.expireScans + 1
2781
2992
  let soonest: i64 = http1ExpireFloor(this.config)
2782
2993
  let n: i32 = 0
2783
- for (let k: i32 = 0; k < toI32(this.fds.length) && k < toI32(this.connections.length); k++) {
2994
+ const idle: i64 = toI64(this.config.idleTimeout) * toI64(1000000)
2995
+ const head: i64 = now + toI64(this.config.headTimeout) * toI64(1000000)
2996
+ for (
2997
+ let k: i32 = 0;
2998
+ k < toI32(this.fds.length) && k < toI32(this.at.length) && k < toI32(this.rested.length);
2999
+ k++
3000
+ ) {
2784
3001
  if (this.fds[k] >= 0) {
2785
- const conn = this.connections[k]
2786
- if (conn.expired(now)) {
3002
+ const j: i32 = this.at[k]
3003
+ if (j < 0) {
3004
+ // A resting slot is idle past `idleTimeout`, and a head that
3005
+ // starts arriving no sooner than now has `headTimeout` from now.
3006
+ const since: i64 = this.rested[k]
3007
+ if (now - since > idle) {
3008
+ this.close(k)
3009
+ n = n + 1
3010
+ } else {
3011
+ const quiet: i64 = since + idle < head ? since + idle : head
3012
+ if (quiet < soonest) {
3013
+ soonest = quiet
3014
+ }
3015
+ }
3016
+ } else if (this.connections[j].expired(now)) {
2787
3017
  this.close(k)
2788
3018
  n = n + 1
2789
- } else if (conn.expiresNoSooner(now) < soonest) {
2790
- soonest = conn.expiresNoSooner(now)
3019
+ } else if (this.connections[j].expiresNoSooner(now) < soonest) {
3020
+ soonest = this.connections[j].expiresNoSooner(now)
2791
3021
  }
2792
3022
  }
2793
3023
  }
@@ -574,6 +574,19 @@ export class Http3Server {
574
574
  private free: i32[]
575
575
  /** Per slot: 1 while on the ready ring, 2 while on the dirty stack. */
576
576
  private queued: i32[]
577
+ /**
578
+ * The stream buffers every slot's connection borrows from, made by the
579
+ * constructor for the stream table `quicConfig` gives every slot: `null`
580
+ * when its streams have none. Each pool makes a buffer only when a stream
581
+ * first needs one, so an idle server holds none (QUIC-3). It is made even
582
+ * for a configuration `quicConnConfigFits` refuses; a slot's connection
583
+ * shares it only when its stream buffers are the pools' size.
584
+ */
585
+ streamPools: QuicStreamPools | null = null
586
+ /** The `bodyChunk`-byte read buffers every slot's `Http3Connection` borrows, one at most each. */
587
+ dataPool: BufferPool
588
+ /** The `maxFieldSectionSize`-byte HEADERS buffers every slot's `Http3Connection` borrows around a decode. */
589
+ sectionPool: BufferPool
577
590
  /** When the answer budget was last topped up, -1 before the first answer. */
578
591
  private answerRefilled: i64 = -1
579
592
  /** The caller's clock in microseconds, from `clock`, or -1: what each connection's pacer counts. */
@@ -588,8 +601,6 @@ export class Http3Server {
588
601
  private slots: i32 = 1
589
602
  /** The slots ever accepted into, 0 up: one past it has never held a connection, so is free without being on `free`. */
590
603
  private used: i32 = 0
591
- /** Set by `stop`: a slot built after it is released as it is made, so it holds no keys. */
592
- private stopped: boolean = false
593
604
  /** What is left of the answer budget (see `answerRefilled`). */
594
605
  private answerBudget: i32 = 0
595
606
  /** Connections accepted into a slot, for a log or a test; saturating at 2^31 - 1. */
@@ -599,21 +610,18 @@ export class Http3Server {
599
610
  /** Datagrams no slot owns dropped past the answer budget before the listener read them, saturating. */
600
611
  limited: i32 = 0
601
612
  private sendErrors: i32 = 0
602
- /** Whether the platform sends ECN marks: true until a marked send answers -95, and every slot's `ecnMarking` with it. */
603
- ecn: boolean = true
604
613
  /**
605
- * The stream buffers every slot's connection borrows from, made by the
606
- * constructor for the stream table `quicConfig` gives every slot: `null`
607
- * when its streams have none. Each pool makes a buffer only when a stream
608
- * first needs one, so an idle server holds none (QUIC-3). It is made even
609
- * for a configuration `quicConnConfigFits` refuses; a slot's connection
610
- * shares it only when its stream buffers are the pools' size.
614
+ * The ECN bits of the datagram being handed on (`udpRecvFrom`'s second
615
+ * metadata word, 0 where the platform does not report them), which `serve`
616
+ * gives the slot's connection as its `arrivalEcn`, so that the server's
617
+ * ACKs echo the counts (RFC 9000 §13.4.1). `receive` sets it for every
618
+ * datagram it reads; a caller of `datagram` sets it before the call.
611
619
  */
612
- streamPools: QuicStreamPools | null = null
613
- /** The `bodyChunk`-byte read buffers every slot's `Http3Connection` borrows, one at most each. */
614
- dataPool: BufferPool
615
- /** The `maxFieldSectionSize`-byte HEADERS buffers every slot's `Http3Connection` borrows around a decode. */
616
- sectionPool: BufferPool
620
+ arrivalEcn: i32 = 0
621
+ /** Whether the platform sends ECN marks: true until a marked send answers -95, and every slot's `ecnMarking` with it. */
622
+ ecn: boolean = true
623
+ /** Set by `stop`: a slot built after it is released as it is made, so it holds no keys. */
624
+ private stopped: boolean = false
617
625
 
618
626
  /**
619
627
  * A pool of `size` slots serving HTTP/3 under `config` over QUIC under
@@ -859,6 +867,8 @@ export class Http3Server {
859
867
  return count
860
868
  }
861
869
  const segment: i32 = this.meta[0] > 0 ? this.meta[0] : n
870
+ // One receive is one ECN codepoint for every GRO segment of it.
871
+ this.arrivalEcn = this.meta[1] & 3
862
872
  let at: i32 = 0
863
873
  while (at < n && segment > 0) {
864
874
  const length: i32 = n - at < segment ? n - at : segment
@@ -1045,6 +1055,7 @@ export class Http3Server {
1045
1055
  serve(slot: i32, buf: u8[], at: i32, len: i32, address: u8[], now: i64, key: Secret<u8[]>): void {
1046
1056
  const quic: QuicConnection = this.quics[slot]
1047
1057
  quic.recovery.micros = this.micros
1058
+ quic.arrivalEcn = this.arrivalEcn
1048
1059
  quic.receiveWindowFrom(buf, at, len, address, now)
1049
1060
  const input: u8[] | null = quic.signatureInput()
1050
1061
  if (input !== null) {
package/std/net/http3.ts CHANGED
@@ -17,12 +17,12 @@
17
17
  * import { QuicConnection } from "nish/net/quic";
18
18
  *
19
19
  * // Once per slot, beside a server QuicConnection; restart() per connection.
20
- * function open(quic: QuicConnection): Http3Connection {
20
+ * const open = (quic: QuicConnection): Http3Connection => {
21
21
  * return new Http3Connection(new Http3Config(), quic);
22
22
  * }
23
23
  *
24
24
  * // After each quic.receive(...):
25
- * function serve(h3: Http3Connection): void {
25
+ * const serve = (h3: Http3Connection): void => {
26
26
  * const names: u8[][] = [httpFieldBytes("content-type")];
27
27
  * const values: u8[][] = [httpFieldBytes("text/plain")];
28
28
  * const body: u8[] = httpFieldBytes("hello\n");