@amritk/nish-aarch64-linux 0.42.0 → 0.45.1
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/bin/nish +0 -0
- package/docs/AI.md +287 -129
- package/package.json +1 -1
- package/runtime/core/numbers.mjs +3 -1
- package/runtime/core/strings.mjs +90 -38
- package/runtime/core.d.ts +24 -0
- package/runtime/core.mjs +21 -0
- package/runtime/nish.d.ts +167 -24
- package/runtime/nish.mjs +7 -1
- package/runtime/runtime-cross.c +9 -0
- package/runtime/runtime-net.c +51 -13
- package/runtime/runtime-region.c +13 -0
- package/runtime/runtime-text.c +11 -0
- package/runtime/runtime-uring.c +83 -25
- package/runtime/runtime-wasm.c +3 -0
- package/runtime/runtime.c +329 -6
- package/runtime/sqlite/README.md +16 -3
- package/runtime/sqlite/nish-sqlite.c +940 -29
- package/scripts/build.sh +111 -29
- package/std/README.md +6 -6
- package/std/array.ts +10 -2
- package/std/bytes.ts +239 -26
- package/std/cli-args.ts +1072 -606
- package/std/cli-flags.ts +180 -98
- package/std/cli.ts +21 -13
- package/std/collections.ts +245 -127
- package/std/compress/checksum.ts +32 -37
- package/std/compress/deflate.ts +1102 -542
- package/std/compress/gzip.ts +290 -145
- package/std/compress/zlib.ts +57 -21
- package/std/crypto/aes.ts +153 -75
- package/std/crypto/base64url.ts +163 -39
- package/std/crypto/bcrypt-batch.ts +164 -90
- package/std/crypto/bcrypt.ts +185 -107
- package/std/crypto/chacha20poly1305.ts +84 -68
- package/std/crypto/ct.ts +9 -7
- package/std/crypto/ed25519.ts +64 -27
- package/std/crypto/hkdf.ts +105 -63
- package/std/crypto/hmac.ts +95 -89
- package/std/crypto/jwt.ts +248 -126
- package/std/crypto/p256.ts +758 -444
- package/std/crypto/p384.ts +20 -13
- package/std/crypto/pki-roots.ts +95 -38
- package/std/crypto/pki.ts +164 -60
- package/std/crypto/rsa.ts +266 -223
- package/std/crypto/scram.ts +141 -0
- package/std/crypto/sha1.ts +157 -57
- package/std/crypto/sha256.ts +502 -422
- package/std/crypto/sha512.ts +204 -135
- package/std/crypto/x509-random.ts +9 -3
- package/std/crypto/x509.ts +331 -171
- package/std/host/ring.ts +1 -1
- package/std/host/text.ts +1 -1
- package/std/json-equal.ts +35 -19
- package/std/json-format.ts +215 -108
- package/std/json-number.ts +126 -72
- package/std/json-pointer.ts +42 -13
- package/std/json-tokenizer.ts +498 -307
- package/std/json-typed.ts +115 -36
- package/std/json-value.ts +341 -152
- package/std/json.ts +300 -158
- package/std/net/buffer-pool.ts +17 -7
- package/std/net/dns-config.ts +427 -236
- package/std/net/dns-message.ts +114 -47
- package/std/net/dns-resolver.ts +736 -237
- package/std/net/dns.ts +205 -52
- package/std/net/fetch.ts +320 -184
- package/std/net/grpc.ts +948 -0
- package/std/net/handler.ts +1276 -218
- package/std/net/hpack.ts +395 -180
- package/std/net/http-compress.ts +502 -65
- package/std/net/http-fields.ts +85 -49
- package/std/net/http-server.ts +1764 -603
- package/std/net/http1-client-codec.ts +372 -161
- package/std/net/http1-codec.ts +665 -331
- package/std/net/http1-server.ts +881 -269
- package/std/net/http2-cleartext.ts +165 -95
- package/std/net/http2-frame.ts +232 -89
- package/std/net/http2-tls.ts +42 -12
- package/std/net/http2.ts +327 -101
- package/std/net/http3-frame.ts +68 -33
- package/std/net/http3-server.ts +672 -206
- package/std/net/http3.ts +669 -191
- package/std/net/oidc.ts +63 -24
- package/std/net/outbound.ts +207 -142
- package/std/net/postgres-async.ts +1223 -470
- package/std/net/postgres-carrier.ts +575 -372
- package/std/net/postgres.ts +1521 -632
- package/std/net/prefork.ts +254 -98
- package/std/net/protobuf.ts +1017 -0
- package/std/net/qpack.ts +143 -92
- package/std/net/quic-conn-ack.ts +104 -22
- package/std/net/quic-conn-cid.ts +66 -26
- package/std/net/quic-conn-params.ts +13 -7
- package/std/net/quic-datagram.ts +90 -27
- package/std/net/quic-frame.ts +324 -121
- package/std/net/quic-listener.ts +206 -91
- package/std/net/quic-packet.ts +289 -163
- package/std/net/quic-recovery.ts +279 -128
- package/std/net/quic-stream.ts +466 -167
- package/std/net/quic.ts +584 -258
- package/std/net/router.ts +450 -99
- package/std/net/serve.ts +661 -240
- package/std/net/static.ts +1458 -0
- package/std/net/tls/client.ts +140 -45
- package/std/net/tls/codec.ts +510 -260
- package/std/net/tls/record-client.ts +268 -76
- package/std/net/tls/record-server.ts +236 -79
- package/std/net/tls/record.ts +173 -47
- package/std/net/tls/schedule.ts +58 -39
- package/std/net/tls/ticket.ts +295 -103
- package/std/net/tls/verify.ts +1 -1
- package/std/net/tls-tcp.ts +218 -75
- package/std/net/tls.ts +260 -152
- package/std/net/upstream.ts +283 -86
- package/std/net/url.ts +277 -102
- package/std/net/websocket.ts +532 -209
- package/std/net/webtransport.ts +755 -436
- package/std/regex.ts +1659 -829
- package/std/sqlite.ts +325 -0
- package/std/sync/ring.ts +320 -156
- package/std/sync/shm.ts +53 -6
- package/std/text.ts +285 -135
- package/std/threads.ts +22 -2
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
* PgAsyncParams, PgAsyncPool, pgAsyncClaim, pgAsyncConnect, pgAsyncQuery, pgAsyncRelease,
|
|
11
11
|
* } from "nish/net/postgres-async"
|
|
12
12
|
*
|
|
13
|
-
* async
|
|
13
|
+
* const title = async (pool: PgAsyncPool, id: i32): Promise<void> => {
|
|
14
14
|
* const ticket: i32 = await pgAsyncClaim(pool)
|
|
15
15
|
* if (ticket < 0) {
|
|
16
16
|
* return // no live connection, or none came free in time
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
* }
|
|
25
25
|
* }
|
|
26
26
|
*
|
|
27
|
-
* export async
|
|
27
|
+
* export const main = async (): Promise<i32> => {
|
|
28
28
|
* const config = new PgConfig()
|
|
29
29
|
* config.user = "app"
|
|
30
30
|
* const pool = new PgAsyncPool(4)
|
|
@@ -172,7 +172,7 @@ import {
|
|
|
172
172
|
} from "nish:net"
|
|
173
173
|
import { monotonicNanos } from "nish:process"
|
|
174
174
|
import { Secret } from "nish:secret"
|
|
175
|
-
import {
|
|
175
|
+
import { getI32BE, getU16BE, tryCopyBytes } from "nish/bytes"
|
|
176
176
|
import {
|
|
177
177
|
PG_ERR_CLOSED,
|
|
178
178
|
PG_ERR_ENCODING,
|
|
@@ -214,8 +214,8 @@ export const PG_ASYNC_MAX_STATEMENTS: i32 = 64
|
|
|
214
214
|
export const PG_ASYNC_MAX_DEPTH: i32 = 16
|
|
215
215
|
/**
|
|
216
216
|
* `pgAsyncSend`'s answer for a statement it did not send because a parameter
|
|
217
|
-
* holds a NUL
|
|
218
|
-
* connection is reused.
|
|
217
|
+
* holds a NUL, its Bind would pass 2^30 bytes, or its `values` and `nulls`
|
|
218
|
+
* differ in length; `pgAsyncAnswer` fails it with `PG_ERR_ENCODING`, and the connection is reused.
|
|
219
219
|
*/
|
|
220
220
|
export const PG_ASYNC_UNSENT: i32 = -1
|
|
221
221
|
/**
|
|
@@ -236,11 +236,16 @@ const PG_ASYNC_SEQ_WRAP: i32 = 16777216
|
|
|
236
236
|
const PG_ASYNC_QUEUE: i32 = 16384
|
|
237
237
|
const PG_ASYNC_QUEUE_WRAP: i32 = 1073741824
|
|
238
238
|
// The wakes the queue's tasks wait on, a task on the one its number picks, so
|
|
239
|
-
// the queue's head moving wakes it and few others.
|
|
239
|
+
// the queue's head moving wakes it and few others. A seat waiting for its
|
|
240
|
+
// turn waits the same way on a turn lane, the one its seat index picks.
|
|
240
241
|
const PG_ASYNC_LANES: i32 = 128
|
|
242
|
+
// The wake the tasks waiting for a slot, or for a seat outside the queue,
|
|
243
|
+
// wait on: after the turn lanes and before the queue lanes.
|
|
244
|
+
const PG_ASYNC_POOL_WAKE: i32 = PG_ASYNC_LANES
|
|
245
|
+
const PG_ASYNC_WAKES: i32 = PG_ASYNC_LANES * 2 + 1
|
|
241
246
|
// What a query reads first; it grows by doubling up to one `maxMessage`.
|
|
242
247
|
const PG_ASYNC_INPUT_SIZE: i32 = 8192
|
|
243
|
-
// Loop turns a seat yields for its turn before it parks on its
|
|
248
|
+
// Loop turns a seat yields for its turn before it parks on its turn lane.
|
|
244
249
|
const PG_ASYNC_TURN_YIELDS: i32 = 4
|
|
245
250
|
// ReadyForQuery's transaction status for "idle", outside any transaction.
|
|
246
251
|
const PG_ASYNC_IDLE: i32 = 73
|
|
@@ -251,6 +256,41 @@ const PG_ASYNC_MAX_CAP: i32 = 1073741823
|
|
|
251
256
|
const PG_ASYNC_MAX_ROWS: i32 = 16777216
|
|
252
257
|
// What a kept cell costs `maxResultBytes` beyond its bytes: its string and its NULL flag.
|
|
253
258
|
const PG_ASYNC_CELL_COST: i32 = 16
|
|
259
|
+
// Every wrap above is a power of two, so a count steps with a mask one below it.
|
|
260
|
+
const PG_ASYNC_CLAIM_MASK: i32 = PG_ASYNC_CLAIM_WRAP - 1
|
|
261
|
+
const PG_ASYNC_SEAT_MASK: i32 = PG_ASYNC_SEAT_WRAP - 1
|
|
262
|
+
const PG_ASYNC_SEQ_MASK: i32 = PG_ASYNC_SEQ_WRAP - 1
|
|
263
|
+
const PG_ASYNC_QUEUE_MASK: i32 = PG_ASYNC_QUEUE_WRAP - 1
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* `n` plus one, wrapped below `mask` plus one, a power of two: for `n` below
|
|
267
|
+
* that wrap it is `(n + 1) % wrap`. The sum is `u32`, which wraps by
|
|
268
|
+
* definition, so it needs no overflow check.
|
|
269
|
+
*/
|
|
270
|
+
const pgAsyncStep = (n: i32, mask: i32): i32 => toI32((toU32(n) + toU32(1)) & toU32(mask))
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* How far `from` is behind `to`, two counts that wrap below `mask` plus one,
|
|
274
|
+
* a power of two: `(to - from + wrap) % wrap` for counts below the wrap,
|
|
275
|
+
* taken in `u32`, which wraps by definition.
|
|
276
|
+
*/
|
|
277
|
+
const pgAsyncGap = (to: i32, from: i32, mask: i32): i32 => toI32((toU32(to) - toU32(from)) & toU32(mask))
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* Where wake `w`'s socket pair starts in `wakes`, `2w`: in `u32` the double
|
|
281
|
+
* wraps rather than checks, and a wake past 2^30 comes out negative, which
|
|
282
|
+
* every caller checks against `wakes` before it reads.
|
|
283
|
+
*/
|
|
284
|
+
const pgAsyncPair = (w: i32): i32 => toI32(toU32(w) * toU32(2))
|
|
285
|
+
|
|
286
|
+
/** `n` plus one, held at the `i32` ceiling: a statistic that counts on, never back. */
|
|
287
|
+
const pgAsyncCount = (n: i32): i32 => (n < 2147483647 ? n + 1 : n)
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* `n` less 1, but not below 0: a waiter count is above 0 when a task takes
|
|
291
|
+
* itself off it, and a ring's unseen count only ever compares with 0.
|
|
292
|
+
*/
|
|
293
|
+
const pgAsyncUncount = (n: i32): i32 => (n > 0 ? n - 1 : 0)
|
|
254
294
|
|
|
255
295
|
/**
|
|
256
296
|
* Authenticated PostgreSQL connections that spawned tasks share. Build one
|
|
@@ -301,13 +341,20 @@ export class PgAsyncPool {
|
|
|
301
341
|
* 1 to `PG_ASYNC_MAX_DEPTH`.
|
|
302
342
|
*/
|
|
303
343
|
depth: i32 = 8
|
|
304
|
-
/**
|
|
344
|
+
/**
|
|
345
|
+
* How many times `pgAsyncClaim` or `pgAsyncSeat` waited because no live
|
|
346
|
+
* connection had room, held at 2^31 - 1 rather than wrapping.
|
|
347
|
+
*/
|
|
305
348
|
waits: i32 = 0
|
|
306
|
-
/**
|
|
349
|
+
/**
|
|
350
|
+
* How many times `pgAsyncTurn` waited for the answers seated ahead of its
|
|
351
|
+
* own, held at 2^31 - 1 rather than wrapping.
|
|
352
|
+
*/
|
|
307
353
|
turnWaits: i32 = 0
|
|
308
354
|
/**
|
|
309
355
|
* How many connections were closed rather than given back: by
|
|
310
|
-
* `pgAsyncRelease`, or once a lost pipeline emptied.
|
|
356
|
+
* `pgAsyncRelease`, or once a lost pipeline emptied. It is held at
|
|
357
|
+
* 2^31 - 1 rather than wrapping.
|
|
311
358
|
*/
|
|
312
359
|
dropped: i32 = 0
|
|
313
360
|
/**
|
|
@@ -316,7 +363,7 @@ export class PgAsyncPool {
|
|
|
316
363
|
* for as long as the server takes.
|
|
317
364
|
*/
|
|
318
365
|
connectWaitMs: i32 = 10000
|
|
319
|
-
/** How many connections `pgAsyncConnect` made, in all. */
|
|
366
|
+
/** How many connections `pgAsyncConnect` made, in all, held at 2^31 - 1 rather than wrapping. */
|
|
320
367
|
connects: i32 = 0
|
|
321
368
|
/**
|
|
322
369
|
* The queue number of the first task still waiting for a seat. Of the
|
|
@@ -380,7 +427,7 @@ export class PgAsyncPool {
|
|
|
380
427
|
/** Per slot: 1 while a task reads past an abandoned seat's answer. */
|
|
381
428
|
readonly skipping: u8[]
|
|
382
429
|
/**
|
|
383
|
-
* Per
|
|
430
|
+
* Per turn lane, then one for the pool, then one per queue lane: a socket pair
|
|
384
431
|
* whose read end a waiting task waits on, or -1. A pair is opened the first
|
|
385
432
|
* time a task waits on its wake, so a pool nobody waits on holds none.
|
|
386
433
|
*/
|
|
@@ -400,7 +447,7 @@ export class PgAsyncPool {
|
|
|
400
447
|
|
|
401
448
|
/**
|
|
402
449
|
* A pool of `size` slots, none connected yet. A waiting task waits on a
|
|
403
|
-
* wake: one per
|
|
450
|
+
* wake: one per turn lane, one for the pool and one per queue lane. Each opens
|
|
404
451
|
* its socket pair the first time a task waits on it; `close()` closes the
|
|
405
452
|
* pairs opened.
|
|
406
453
|
* @param size The number of connections, 1 to `PG_ASYNC_MAX_SIZE`; any
|
|
@@ -411,31 +458,36 @@ export class PgAsyncPool {
|
|
|
411
458
|
panic("nish/net/postgres-async: a PgAsyncPool size out of range")
|
|
412
459
|
}
|
|
413
460
|
this.size = size
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
461
|
+
// As `u16`s the lengths need no check: the size is 1 to 64 here, so the
|
|
462
|
+
// most any of them holds is 64 seats of 64 statements.
|
|
463
|
+
const slots: u16 = toU16(size)
|
|
464
|
+
const statements: u16 = slots * toU16(PG_ASYNC_MAX_STATEMENTS)
|
|
465
|
+
const seats: u16 = slots * toU16(PG_ASYNC_MAX_DEPTH)
|
|
466
|
+
const wakes: u16 = toU16(PG_ASYNC_WAKES)
|
|
467
|
+
this.fds = new Array<i32>(slots)
|
|
468
|
+
this.claimed = new Array<u8>(slots)
|
|
469
|
+
this.claims = new Array<i32>(slots)
|
|
417
470
|
this.statements = []
|
|
418
|
-
this.prepared = new Array<u8>(
|
|
471
|
+
this.prepared = new Array<u8>(statements)
|
|
419
472
|
this.inputs = []
|
|
420
|
-
this.inStart = new Array<i32>(
|
|
421
|
-
this.inEnd = new Array<i32>(
|
|
422
|
-
this.inAnswer = new Array<i32>(
|
|
423
|
-
this.inParsed = new Array<u8>(
|
|
424
|
-
this.nextSeq = new Array<i32>(
|
|
425
|
-
this.served = new Array<i32>(
|
|
426
|
-
this.doomAt = new Array<i32>(
|
|
427
|
-
this.seated = new Array<i32>(
|
|
428
|
-
this.writing = new Array<u8>(
|
|
429
|
-
this.seatUsed = new Array<u8>(
|
|
430
|
-
this.seatSeq = new Array<i32>(
|
|
431
|
-
this.seatStatement = new Array<i32>(
|
|
432
|
-
this.seatParse = new Array<u8>(
|
|
433
|
-
this.seatWaiting = new Array<u8>(
|
|
434
|
-
this.seatClaims = new Array<i32>(
|
|
435
|
-
this.seatAbandoned = new Array<u8>(
|
|
436
|
-
this.skipping = new Array<u8>(
|
|
437
|
-
|
|
438
|
-
this.wakes = new Array<i32>(wakes * 2)
|
|
473
|
+
this.inStart = new Array<i32>(slots)
|
|
474
|
+
this.inEnd = new Array<i32>(slots)
|
|
475
|
+
this.inAnswer = new Array<i32>(slots)
|
|
476
|
+
this.inParsed = new Array<u8>(slots)
|
|
477
|
+
this.nextSeq = new Array<i32>(slots)
|
|
478
|
+
this.served = new Array<i32>(slots)
|
|
479
|
+
this.doomAt = new Array<i32>(slots)
|
|
480
|
+
this.seated = new Array<i32>(slots)
|
|
481
|
+
this.writing = new Array<u8>(slots)
|
|
482
|
+
this.seatUsed = new Array<u8>(seats)
|
|
483
|
+
this.seatSeq = new Array<i32>(seats)
|
|
484
|
+
this.seatStatement = new Array<i32>(seats)
|
|
485
|
+
this.seatParse = new Array<u8>(seats)
|
|
486
|
+
this.seatWaiting = new Array<u8>(seats)
|
|
487
|
+
this.seatClaims = new Array<i32>(seats)
|
|
488
|
+
this.seatAbandoned = new Array<u8>(seats)
|
|
489
|
+
this.skipping = new Array<u8>(slots)
|
|
490
|
+
this.wakes = new Array<i32>(wakes * toU16(2))
|
|
439
491
|
this.wakePending = new Array<u8>(wakes)
|
|
440
492
|
this.wakeBytes = new Array<u8>(8)
|
|
441
493
|
this.wakePair = new Array<i32>(2)
|
|
@@ -443,12 +495,12 @@ export class PgAsyncPool {
|
|
|
443
495
|
this.wakeRings = new Array<i32>(wakes)
|
|
444
496
|
this.wakeUnseen = new Array<i32>(wakes)
|
|
445
497
|
this.seatGone = new Array<u8>(PG_ASYNC_QUEUE)
|
|
446
|
-
for (let s: i32 = 0; s <
|
|
498
|
+
for (let s: i32 = 0; s < toI32(this.fds.length) && s < toI32(this.doomAt.length); s++) {
|
|
447
499
|
this.fds[s] = -1
|
|
448
500
|
this.doomAt[s] = -1
|
|
449
501
|
this.inputs.push(new Array<u8>(PG_ASYNC_INPUT_SIZE))
|
|
450
502
|
}
|
|
451
|
-
for (let w: i32 = 0; w < wakes
|
|
503
|
+
for (let w: i32 = 0; w < toI32(this.wakes.length); w++) {
|
|
452
504
|
this.wakes[w] = -1
|
|
453
505
|
}
|
|
454
506
|
}
|
|
@@ -458,13 +510,14 @@ export class PgAsyncPool {
|
|
|
458
510
|
* @returns The count, 0 to `size`.
|
|
459
511
|
*/
|
|
460
512
|
live(): i32 {
|
|
461
|
-
|
|
462
|
-
|
|
513
|
+
// A `u32` count wraps rather than checks: it never passes `size`.
|
|
514
|
+
let n: u32 = 0
|
|
515
|
+
for (let s: i32 = 0; s < toI32(this.fds.length); s++) {
|
|
463
516
|
if (this.fds[s] >= 0) {
|
|
464
517
|
n += 1
|
|
465
518
|
}
|
|
466
519
|
}
|
|
467
|
-
return n
|
|
520
|
+
return toI32(n)
|
|
468
521
|
}
|
|
469
522
|
|
|
470
523
|
/**
|
|
@@ -474,28 +527,41 @@ export class PgAsyncPool {
|
|
|
474
527
|
*/
|
|
475
528
|
close(): void {
|
|
476
529
|
const terminate: u8[] = [88, 0, 0, 0, 4]
|
|
477
|
-
for (
|
|
530
|
+
for (
|
|
531
|
+
let s: i32 = 0;
|
|
532
|
+
s < toI32(this.fds.length) && s < toI32(this.claimed.length) && s < toI32(this.claims.length);
|
|
533
|
+
s++
|
|
534
|
+
) {
|
|
535
|
+
// The slot is marked closed before the calls, which the bounds above do not outlive.
|
|
478
536
|
const fd: i32 = this.fds[s]
|
|
537
|
+
this.fds[s] = -1
|
|
538
|
+
this.claimed[s] = 0
|
|
539
|
+
this.claims[s] = pgAsyncStep(this.claims[s], PG_ASYNC_CLAIM_MASK)
|
|
479
540
|
if (fd >= 0) {
|
|
480
|
-
netWrite(fd, terminate, 0,
|
|
541
|
+
netWrite(fd, terminate, 0, toI32(terminate.length))
|
|
481
542
|
netClose(fd)
|
|
482
|
-
this.fds[s] = -1
|
|
483
|
-
}
|
|
484
|
-
this.claimed[s] = 0
|
|
485
|
-
this.claims[s] = (this.claims[s] + 1) % PG_ASYNC_CLAIM_WRAP
|
|
486
|
-
for (let k: i32 = 0; k < PG_ASYNC_MAX_DEPTH; k++) {
|
|
487
|
-
const seat: i32 = s * PG_ASYNC_MAX_DEPTH + k
|
|
488
|
-
this.seatUsed[seat] = 0
|
|
489
|
-
this.seatWaiting[seat] = 0
|
|
490
|
-
this.seatAbandoned[seat] = 0
|
|
491
|
-
this.seatClaims[seat] = (this.seatClaims[seat] + 1) % PG_ASYNC_SEAT_WRAP
|
|
492
543
|
}
|
|
493
544
|
pgAsyncForget(this, s)
|
|
494
545
|
}
|
|
546
|
+
// Every slot's seats, which lie one after another.
|
|
547
|
+
for (
|
|
548
|
+
let seat: i32 = 0;
|
|
549
|
+
seat < toI32(this.seatUsed.length) &&
|
|
550
|
+
seat < toI32(this.seatWaiting.length) &&
|
|
551
|
+
seat < toI32(this.seatAbandoned.length) &&
|
|
552
|
+
seat < toI32(this.seatClaims.length);
|
|
553
|
+
seat++
|
|
554
|
+
) {
|
|
555
|
+
this.seatUsed[seat] = 0
|
|
556
|
+
this.seatWaiting[seat] = 0
|
|
557
|
+
this.seatAbandoned[seat] = 0
|
|
558
|
+
this.seatClaims[seat] = pgAsyncStep(this.seatClaims[seat], PG_ASYNC_SEAT_MASK)
|
|
559
|
+
}
|
|
495
560
|
for (let w: i32 = 0; w < toI32(this.wakes.length); w++) {
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
561
|
+
const fd: i32 = this.wakes[w]
|
|
562
|
+
this.wakes[w] = -1
|
|
563
|
+
if (fd >= 0) {
|
|
564
|
+
netClose(fd)
|
|
499
565
|
}
|
|
500
566
|
}
|
|
501
567
|
}
|
|
@@ -503,6 +569,35 @@ export class PgAsyncPool {
|
|
|
503
569
|
|
|
504
570
|
/** Resets slot `s`'s pipeline: nothing in flight, nothing prepared, nothing buffered. */
|
|
505
571
|
const pgAsyncForget = (pool: PgAsyncPool, s: i32): void => {
|
|
572
|
+
// 64 is PG_ASYNC_MAX_SIZE, written as a literal: the prover takes a bound
|
|
573
|
+
// from a literal but not from a module constant, and the bound proves the
|
|
574
|
+
// `prepared` offsets below cannot overflow.
|
|
575
|
+
if (s < 0 || s >= 64) {
|
|
576
|
+
return
|
|
577
|
+
}
|
|
578
|
+
if (
|
|
579
|
+
s >= toI32(pool.nextSeq.length) ||
|
|
580
|
+
s >= toI32(pool.served.length) ||
|
|
581
|
+
s >= toI32(pool.doomAt.length) ||
|
|
582
|
+
s >= toI32(pool.seated.length) ||
|
|
583
|
+
s >= toI32(pool.writing.length) ||
|
|
584
|
+
s >= toI32(pool.inStart.length) ||
|
|
585
|
+
s >= toI32(pool.inEnd.length) ||
|
|
586
|
+
s >= toI32(pool.inAnswer.length) ||
|
|
587
|
+
s >= toI32(pool.inParsed.length) ||
|
|
588
|
+
s >= toI32(pool.skipping.length)
|
|
589
|
+
) {
|
|
590
|
+
// No such slot: nothing to reset.
|
|
591
|
+
return
|
|
592
|
+
}
|
|
593
|
+
const first: i32 = s * PG_ASYNC_MAX_STATEMENTS
|
|
594
|
+
// 64 is PG_ASYNC_MAX_STATEMENTS, a literal for the same reason.
|
|
595
|
+
for (let q: i32 = 0; q < 64; q++) {
|
|
596
|
+
const at: i32 = first + q
|
|
597
|
+
if (at < toI32(pool.prepared.length)) {
|
|
598
|
+
pool.prepared[at] = 0
|
|
599
|
+
}
|
|
600
|
+
}
|
|
506
601
|
pool.nextSeq[s] = 0
|
|
507
602
|
pool.served[s] = 0
|
|
508
603
|
pool.doomAt[s] = -1
|
|
@@ -513,9 +608,6 @@ const pgAsyncForget = (pool: PgAsyncPool, s: i32): void => {
|
|
|
513
608
|
pool.inAnswer[s] = -1
|
|
514
609
|
pool.inParsed[s] = 0
|
|
515
610
|
pool.skipping[s] = 0
|
|
516
|
-
for (let q: i32 = 0; q < PG_ASYNC_MAX_STATEMENTS; q++) {
|
|
517
|
-
pool.prepared[s * PG_ASYNC_MAX_STATEMENTS + q] = 0
|
|
518
|
-
}
|
|
519
611
|
}
|
|
520
612
|
|
|
521
613
|
/**
|
|
@@ -613,7 +705,11 @@ export class PgAsyncResult {
|
|
|
613
705
|
* @returns The text the server sent.
|
|
614
706
|
*/
|
|
615
707
|
text(row: i32, column: i32): string {
|
|
616
|
-
|
|
708
|
+
const at: i32 = this.cell(row, column)
|
|
709
|
+
if (at < 0 || at >= toI32(this.values.length)) {
|
|
710
|
+
panic("nish/net/postgres-async: a cell out of range")
|
|
711
|
+
}
|
|
712
|
+
return this.values[at]
|
|
617
713
|
}
|
|
618
714
|
|
|
619
715
|
/**
|
|
@@ -623,63 +719,73 @@ export class PgAsyncResult {
|
|
|
623
719
|
* @returns True for a NULL.
|
|
624
720
|
*/
|
|
625
721
|
isNull(row: i32, column: i32): boolean {
|
|
626
|
-
|
|
722
|
+
const at: i32 = this.cell(row, column)
|
|
723
|
+
if (at < 0 || at >= toI32(this.nulls.length)) {
|
|
724
|
+
panic("nish/net/postgres-async: a cell out of range")
|
|
725
|
+
}
|
|
726
|
+
return this.nulls[at]
|
|
627
727
|
}
|
|
628
728
|
|
|
629
729
|
/**
|
|
630
|
-
* A cell read as a decimal `i32`, or `fallback` when it is NULL or not one
|
|
730
|
+
* A cell read as a decimal `i32`, or `fallback` when it is NULL or not one;
|
|
731
|
+
* panics on a row or column out of range.
|
|
631
732
|
* @param row The row, from 0.
|
|
632
733
|
* @param column The column, from 0.
|
|
633
734
|
* @param fallback The answer for a NULL or a cell that is not a decimal `i32`.
|
|
634
735
|
* @returns The number.
|
|
635
736
|
*/
|
|
636
737
|
int(row: i32, column: i32, fallback: i32): i32 {
|
|
637
|
-
|
|
638
|
-
if (this.nulls[at]) {
|
|
738
|
+
if (this.isNull(row, column)) {
|
|
639
739
|
return fallback
|
|
640
740
|
}
|
|
641
|
-
return pgAsyncDecimal(this.
|
|
741
|
+
return pgAsyncDecimal(this.text(row, column), fallback)
|
|
642
742
|
}
|
|
643
743
|
|
|
644
|
-
/**
|
|
744
|
+
/**
|
|
745
|
+
* The index into `values` of a cell, or -1 for a row or column out of
|
|
746
|
+
* range; the caller panics on -1, as its contract says.
|
|
747
|
+
*/
|
|
645
748
|
private cell(row: i32, column: i32): i32 {
|
|
646
749
|
if (row < 0 || row >= this.rows || column < 0 || column >= this.columns) {
|
|
647
|
-
|
|
750
|
+
return -1
|
|
648
751
|
}
|
|
649
|
-
|
|
752
|
+
// In `i64` the sum cannot overflow; past the cells kept it is out of range too.
|
|
753
|
+
const at: i64 = toI64(row) * toI64(this.columns) + toI64(column)
|
|
754
|
+
return at < toI64(this.values.length) ? toI32(at) : -1
|
|
650
755
|
}
|
|
651
756
|
}
|
|
652
757
|
|
|
653
758
|
/** `s` as a decimal `i32`, or `fallback` when it is not one. */
|
|
654
759
|
const pgAsyncDecimal = (s: string, fallback: i32): i32 => {
|
|
655
760
|
const n: i32 = toI32(s.length)
|
|
656
|
-
if (n
|
|
761
|
+
if (n < 1 || n > 11) {
|
|
657
762
|
return fallback
|
|
658
763
|
}
|
|
659
|
-
let
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
return fallback
|
|
665
|
-
}
|
|
666
|
-
}
|
|
667
|
-
let v: i64 = 0
|
|
668
|
-
while (at < n) {
|
|
764
|
+
let negative: boolean = false
|
|
765
|
+
// At most 11 digits fit a `u64` with room to spare, and `u64` arithmetic
|
|
766
|
+
// wraps rather than checks, so the sum needs no overflow test.
|
|
767
|
+
let v: u64 = 0
|
|
768
|
+
for (let at: i32 = 0; at < n; at++) {
|
|
669
769
|
const c: i32 = toI32(s.charCodeAt(at))
|
|
670
|
-
if (
|
|
770
|
+
if (at === 0 && c === 45) {
|
|
771
|
+
negative = true
|
|
772
|
+
} else if (c < 48 || c > 57) {
|
|
671
773
|
return fallback
|
|
774
|
+
} else {
|
|
775
|
+
v = v * toU64(10) + toU64(c - 48)
|
|
672
776
|
}
|
|
673
|
-
v = v * toI64(10) + toI64(c - 48)
|
|
674
|
-
at += 1
|
|
675
777
|
}
|
|
676
|
-
|
|
677
|
-
|
|
778
|
+
const w: i64 = toI64(v)
|
|
779
|
+
if (w < 0 || w > 2147483648) {
|
|
780
|
+
return fallback
|
|
678
781
|
}
|
|
679
|
-
if (
|
|
782
|
+
if (negative && n === 1) {
|
|
680
783
|
return fallback
|
|
681
784
|
}
|
|
682
|
-
|
|
785
|
+
if (negative) {
|
|
786
|
+
return toI32(-w)
|
|
787
|
+
}
|
|
788
|
+
return w > toI64(2147483647) ? fallback : toI32(w)
|
|
683
789
|
}
|
|
684
790
|
|
|
685
791
|
/**
|
|
@@ -700,7 +806,8 @@ const pgAsyncSlot = (pool: PgAsyncPool, ticket: i32): i32 => {
|
|
|
700
806
|
const slot: i32 = ticket >= 0 ? ticket % PG_ASYNC_MAX_SIZE : -1
|
|
701
807
|
if (
|
|
702
808
|
slot < 0 ||
|
|
703
|
-
slot >= pool.
|
|
809
|
+
slot >= toI32(pool.claimed.length) ||
|
|
810
|
+
slot >= toI32(pool.claims.length) ||
|
|
704
811
|
toI32(pool.claimed[slot]) !== 1 ||
|
|
705
812
|
toI32(ticket / PG_ASYNC_MAX_SIZE) !== pool.claims[slot]
|
|
706
813
|
) {
|
|
@@ -738,7 +845,7 @@ export const pgAsyncConnect = async (
|
|
|
738
845
|
panic("nish/net/postgres-async: a TCP host must be a numeric address")
|
|
739
846
|
}
|
|
740
847
|
const nonce: u8[] = new Array<u8>(PG_ASYNC_NONCE_BYTES)
|
|
741
|
-
for (let s: i32 = 0; s < pool.
|
|
848
|
+
for (let s: i32 = 0; s < toI32(pool.fds.length); s++) {
|
|
742
849
|
if (pool.fds[s] >= 0) {
|
|
743
850
|
continue
|
|
744
851
|
}
|
|
@@ -751,10 +858,11 @@ export const pgAsyncConnect = async (
|
|
|
751
858
|
}
|
|
752
859
|
const made: boolean = await pgAsyncStartup(conn, fd, monotonicNanos(), pool.connectWaitMs)
|
|
753
860
|
conn.dispose()
|
|
754
|
-
|
|
861
|
+
// The handshake awaited, so the slot is checked against the arrays again.
|
|
862
|
+
if (made && s < toI32(pool.fds.length) && s < toI32(pool.claimed.length)) {
|
|
755
863
|
pool.fds[s] = fd
|
|
756
864
|
pool.claimed[s] = 0
|
|
757
|
-
pool.connects
|
|
865
|
+
pool.connects = pgAsyncCount(pool.connects)
|
|
758
866
|
pgAsyncForget(pool, s)
|
|
759
867
|
} else {
|
|
760
868
|
netClose(fd)
|
|
@@ -774,13 +882,14 @@ const pgAsyncStartup = async (conn: PgConnection, fd: i32, began: i64, waitMs: i
|
|
|
774
882
|
return false
|
|
775
883
|
}
|
|
776
884
|
while (true) {
|
|
777
|
-
while (
|
|
778
|
-
const
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
885
|
+
while (true) {
|
|
886
|
+
const start: i32 = conn.outputStart()
|
|
887
|
+
const end: i32 = conn.outputEnd()
|
|
888
|
+
if (start < 0 || end <= start) {
|
|
889
|
+
break
|
|
890
|
+
}
|
|
891
|
+
// Both are at least 0 and `end` is past `start`, so the `u32` difference is exact.
|
|
892
|
+
const n: i32 = netWrite(fd, conn.outputBuffer(), start, toI32(toU32(end) - toU32(start)))
|
|
784
893
|
if (n === PG_ASYNC_AGAIN) {
|
|
785
894
|
const waited: boolean = await pgAsyncWaitUntil(fd, true, began, waitMs)
|
|
786
895
|
if (!waited) {
|
|
@@ -832,10 +941,11 @@ const pgAsyncWaitUntil = async (fd: i32, write: boolean, began: i64, waitMs: i32
|
|
|
832
941
|
} else if (waitMs <= 0) {
|
|
833
942
|
waited = await readable(fd)
|
|
834
943
|
} else {
|
|
835
|
-
const
|
|
836
|
-
if (
|
|
944
|
+
const since: i32 = pgAsyncSince(began)
|
|
945
|
+
if (since < 0 || since >= waitMs) {
|
|
837
946
|
return false
|
|
838
947
|
}
|
|
948
|
+
const left: i32 = waitMs - since
|
|
839
949
|
if (write) {
|
|
840
950
|
waited = await writableWithin(fd, left)
|
|
841
951
|
} else {
|
|
@@ -847,19 +957,34 @@ const pgAsyncWaitUntil = async (fd: i32, write: boolean, began: i64, waitMs: i32
|
|
|
847
957
|
|
|
848
958
|
/** Milliseconds since `began`, a `monotonicNanos` reading. */
|
|
849
959
|
const pgAsyncSince = (began: i64): i32 => {
|
|
850
|
-
|
|
960
|
+
// `u64` arithmetic wraps rather than checks; a clock read after `began`
|
|
961
|
+
// is never behind it, so the difference is the real one.
|
|
962
|
+
const ms: i64 = toI64((toU64(monotonicNanos()) - toU64(began)) / toU64(1000000))
|
|
851
963
|
return ms > toI64(1073741823) ? 1073741823 : toI32(ms)
|
|
852
964
|
}
|
|
853
965
|
|
|
854
966
|
/**
|
|
855
|
-
* Wakes the tasks waiting on wake `w` (a
|
|
967
|
+
* Wakes the tasks waiting on wake `w` (a turn lane, the pool's or a queue lane):
|
|
856
968
|
* one byte on its socket pair, unless one is already there or none waits.
|
|
857
969
|
* The byte stays until every task then waiting has woken: the runtime reports
|
|
858
970
|
* at most 64 ready sockets a turn, and a byte read back by the first of
|
|
859
971
|
* many would leave the rest waiting until their time ran out.
|
|
860
972
|
*/
|
|
861
973
|
const pgAsyncSignal = (pool: PgAsyncPool, w: i32): void => {
|
|
862
|
-
|
|
974
|
+
if (w < 0 || w >= toI32(pool.waiters.length) || w >= toI32(pool.wakePending.length)) {
|
|
975
|
+
// No such wake: nobody waits on it.
|
|
976
|
+
return
|
|
977
|
+
}
|
|
978
|
+
// A wake's pair is `wakes[2w]` (read end) and `wakes[2w + 1]` (write end).
|
|
979
|
+
const start: i32 = pgAsyncPair(w)
|
|
980
|
+
if (start < 0 || start >= toI32(pool.wakes.length)) {
|
|
981
|
+
return
|
|
982
|
+
}
|
|
983
|
+
const end: i32 = start + 1
|
|
984
|
+
if (end >= toI32(pool.wakes.length) || toI32(pool.wakeBytes.length) < 1) {
|
|
985
|
+
return
|
|
986
|
+
}
|
|
987
|
+
const fd: i32 = pool.wakes[end]
|
|
863
988
|
if (pool.waiters[w] <= 0 || fd < 0) {
|
|
864
989
|
return
|
|
865
990
|
}
|
|
@@ -868,13 +993,22 @@ const pgAsyncSignal = (pool: PgAsyncPool, w: i32): void => {
|
|
|
868
993
|
if (netWrite(fd, pool.wakeBytes, 0, 1) !== 1) {
|
|
869
994
|
return
|
|
870
995
|
}
|
|
871
|
-
pool.wakePending[w] = 1
|
|
872
996
|
}
|
|
997
|
+
// The write was a call, so the wake is checked against the arrays again.
|
|
998
|
+
if (
|
|
999
|
+
w >= toI32(pool.waiters.length) ||
|
|
1000
|
+
w >= toI32(pool.wakePending.length) ||
|
|
1001
|
+
w >= toI32(pool.wakeUnseen.length) ||
|
|
1002
|
+
w >= toI32(pool.wakeRings.length)
|
|
1003
|
+
) {
|
|
1004
|
+
return
|
|
1005
|
+
}
|
|
1006
|
+
pool.wakePending[w] = 1
|
|
873
1007
|
// Rung again while its byte is there, the byte now stays for every task
|
|
874
1008
|
// waiting at this ring too: one that waited again after the last ring would
|
|
875
1009
|
// otherwise see the byte read back before it was polled, and wait for ever.
|
|
876
1010
|
pool.wakeUnseen[w] = pool.waiters[w]
|
|
877
|
-
pool.wakeRings[w] = (pool.wakeRings[w]
|
|
1011
|
+
pool.wakeRings[w] = pgAsyncStep(pool.wakeRings[w], PG_ASYNC_QUEUE_MASK)
|
|
878
1012
|
}
|
|
879
1013
|
|
|
880
1014
|
/**
|
|
@@ -885,7 +1019,15 @@ const pgAsyncSignal = (pool: PgAsyncPool, w: i32): void => {
|
|
|
885
1019
|
*/
|
|
886
1020
|
const pgAsyncFreeSeats = (pool: PgAsyncPool): i32 => {
|
|
887
1021
|
let free: i32 = 0
|
|
888
|
-
for (
|
|
1022
|
+
for (
|
|
1023
|
+
let s: i32 = 0;
|
|
1024
|
+
s < toI32(pool.fds.length) &&
|
|
1025
|
+
s < toI32(pool.claimed.length) &&
|
|
1026
|
+
s < toI32(pool.doomAt.length) &&
|
|
1027
|
+
s < toI32(pool.writing.length) &&
|
|
1028
|
+
s < toI32(pool.seated.length);
|
|
1029
|
+
s++
|
|
1030
|
+
) {
|
|
889
1031
|
if (
|
|
890
1032
|
pool.fds[s] >= 0 &&
|
|
891
1033
|
toI32(pool.claimed[s]) === 0 &&
|
|
@@ -893,7 +1035,9 @@ const pgAsyncFreeSeats = (pool: PgAsyncPool): i32 => {
|
|
|
893
1035
|
toI32(pool.writing[s]) === 0 &&
|
|
894
1036
|
pool.seated[s] < pool.depth
|
|
895
1037
|
) {
|
|
896
|
-
|
|
1038
|
+
// In `i64` the sum cannot overflow; it never passes 64 slots of 16 seats.
|
|
1039
|
+
const next: i64 = toI64(free) + toI64(pool.depth) - toI64(pool.seated[s])
|
|
1040
|
+
free = next > toI64(2147483647) ? 2147483647 : toI32(next)
|
|
897
1041
|
}
|
|
898
1042
|
}
|
|
899
1043
|
return free
|
|
@@ -905,8 +1049,8 @@ const pgAsyncFreeSeats = (pool: PgAsyncPool): i32 => {
|
|
|
905
1049
|
* seats freed together are taken in one loop turn and not one a turn.
|
|
906
1050
|
*/
|
|
907
1051
|
const pgAsyncWakePool = (pool: PgAsyncPool): void => {
|
|
908
|
-
pgAsyncSignal(pool,
|
|
909
|
-
const queued: i32 = (pool.seatTail
|
|
1052
|
+
pgAsyncSignal(pool, PG_ASYNC_POOL_WAKE)
|
|
1053
|
+
const queued: i32 = pgAsyncGap(pool.seatTail, pool.seatHead, PG_ASYNC_QUEUE_MASK)
|
|
910
1054
|
if (queued === 0) {
|
|
911
1055
|
return
|
|
912
1056
|
}
|
|
@@ -915,16 +1059,26 @@ const pgAsyncWakePool = (pool: PgAsyncPool): void => {
|
|
|
915
1059
|
wake = wake > queued ? queued : wake
|
|
916
1060
|
wake = wake > PG_ASYNC_LANES ? PG_ASYNC_LANES : wake
|
|
917
1061
|
for (let k: i32 = 0; k < wake; k++) {
|
|
918
|
-
|
|
1062
|
+
// The queue lane of the `k`th task waiting, after the pool's own wake.
|
|
1063
|
+
const lane: u32 = (toU32(pool.seatHead) + toU32(k)) & toU32(PG_ASYNC_LANES - 1)
|
|
1064
|
+
pgAsyncSignal(pool, toI32(toU32(PG_ASYNC_POOL_WAKE + 1) + lane))
|
|
919
1065
|
}
|
|
920
1066
|
}
|
|
921
1067
|
|
|
922
1068
|
/** Reads wake `w`'s byte back once every task it was written for has woken, so its waiters wait again. */
|
|
923
1069
|
const pgAsyncDrain = (pool: PgAsyncPool, w: i32): void => {
|
|
924
|
-
|
|
1070
|
+
if (w < 0 || w >= toI32(pool.wakeUnseen.length) || w >= toI32(pool.wakePending.length)) {
|
|
1071
|
+
// No such wake: no byte to read back.
|
|
1072
|
+
return
|
|
1073
|
+
}
|
|
1074
|
+
const start: i32 = pgAsyncPair(w)
|
|
1075
|
+
if (start < 0 || start >= toI32(pool.wakes.length)) {
|
|
1076
|
+
return
|
|
1077
|
+
}
|
|
1078
|
+
const fd: i32 = pool.wakes[start]
|
|
925
1079
|
if (toI32(pool.wakePending[w]) === 1 && pool.wakeUnseen[w] <= 0 && fd >= 0) {
|
|
926
|
-
netRead(fd, pool.wakeBytes, 0, 8)
|
|
927
1080
|
pool.wakePending[w] = 0
|
|
1081
|
+
netRead(fd, pool.wakeBytes, 0, 8)
|
|
928
1082
|
}
|
|
929
1083
|
}
|
|
930
1084
|
|
|
@@ -934,24 +1088,60 @@ const pgAsyncDrain = (pool: PgAsyncPool, w: i32): void => {
|
|
|
934
1088
|
* A cancelled task returns at once.
|
|
935
1089
|
*/
|
|
936
1090
|
const pgAsyncPark = async (pool: PgAsyncPool, w: i32, ms: i32): Promise<void> => {
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
pool.wakes
|
|
1091
|
+
const start: i32 = pgAsyncPair(w)
|
|
1092
|
+
if (
|
|
1093
|
+
w < 0 ||
|
|
1094
|
+
start < 0 ||
|
|
1095
|
+
start >= toI32(pool.wakes.length) ||
|
|
1096
|
+
w >= toI32(pool.waiters.length) ||
|
|
1097
|
+
w >= toI32(pool.wakeRings.length)
|
|
1098
|
+
) {
|
|
1099
|
+
// No such wake: wait as a task with no pair does, then look again.
|
|
1100
|
+
await sleep(1)
|
|
1101
|
+
return
|
|
1102
|
+
}
|
|
1103
|
+
const end: i32 = start + 1
|
|
1104
|
+
if (pool.wakes[start] < 0 && end < toI32(pool.wakes.length) && toI32(pool.wakePair.length) >= 2) {
|
|
1105
|
+
if (unixSocketPair(NET_UNIX_STREAM, pool.wakePair) >= 0) {
|
|
1106
|
+
// Opened before this task counts as waiting, so no byte is written to a
|
|
1107
|
+
// wake without a pair. Out of descriptors, it sleeps 1 ms and tries again.
|
|
1108
|
+
if (
|
|
1109
|
+
start < toI32(pool.wakes.length) &&
|
|
1110
|
+
end < toI32(pool.wakes.length) &&
|
|
1111
|
+
toI32(pool.wakePair.length) >= 2
|
|
1112
|
+
) {
|
|
1113
|
+
pool.wakes[start] = pool.wakePair[0]
|
|
1114
|
+
pool.wakes[end] = pool.wakePair[1]
|
|
1115
|
+
}
|
|
1116
|
+
}
|
|
942
1117
|
}
|
|
943
|
-
|
|
1118
|
+
if (
|
|
1119
|
+
start >= toI32(pool.wakes.length) ||
|
|
1120
|
+
w >= toI32(pool.waiters.length) ||
|
|
1121
|
+
w >= toI32(pool.wakeRings.length)
|
|
1122
|
+
) {
|
|
1123
|
+
return
|
|
1124
|
+
}
|
|
1125
|
+
const fd: i32 = pool.wakes[start]
|
|
944
1126
|
const rings: i32 = pool.wakeRings[w]
|
|
945
|
-
pool.waiters[w]
|
|
1127
|
+
pool.waiters[w] = pgAsyncCount(pool.waiters[w])
|
|
946
1128
|
if (fd < 0) {
|
|
947
1129
|
await sleep(1)
|
|
948
1130
|
} else {
|
|
949
1131
|
await readableWithin(fd, ms)
|
|
950
1132
|
}
|
|
951
|
-
|
|
1133
|
+
// The wait was a call, so the wake is checked against the arrays again.
|
|
1134
|
+
if (
|
|
1135
|
+
w >= toI32(pool.waiters.length) ||
|
|
1136
|
+
w >= toI32(pool.wakeRings.length) ||
|
|
1137
|
+
w >= toI32(pool.wakeUnseen.length)
|
|
1138
|
+
) {
|
|
1139
|
+
return
|
|
1140
|
+
}
|
|
1141
|
+
pool.waiters[w] = pgAsyncUncount(pool.waiters[w])
|
|
952
1142
|
if (pool.wakeRings[w] !== rings) {
|
|
953
1143
|
// Waiting when the byte was written: one fewer it waits for.
|
|
954
|
-
pool.wakeUnseen[w]
|
|
1144
|
+
pool.wakeUnseen[w] = pgAsyncUncount(pool.wakeUnseen[w])
|
|
955
1145
|
}
|
|
956
1146
|
}
|
|
957
1147
|
|
|
@@ -977,36 +1167,49 @@ export const pgAsyncClaim = async (pool: PgAsyncPool): Promise<i32> => {
|
|
|
977
1167
|
return PG_ASYNC_CANCELLED
|
|
978
1168
|
}
|
|
979
1169
|
let live: boolean = false
|
|
980
|
-
for (
|
|
1170
|
+
for (
|
|
1171
|
+
let s: i32 = 0;
|
|
1172
|
+
s < toI32(pool.fds.length) &&
|
|
1173
|
+
s < toI32(pool.claimed.length) &&
|
|
1174
|
+
s < toI32(pool.claims.length) &&
|
|
1175
|
+
s < toI32(pool.seated.length) &&
|
|
1176
|
+
s < toI32(pool.doomAt.length);
|
|
1177
|
+
s++
|
|
1178
|
+
) {
|
|
981
1179
|
if (pool.fds[s] < 0) {
|
|
982
1180
|
continue
|
|
983
1181
|
}
|
|
984
1182
|
live = true
|
|
985
1183
|
if (toI32(pool.claimed[s]) === 0 && pool.seated[s] === 0 && pool.doomAt[s] < 0) {
|
|
1184
|
+
// The claim count wraps below 2^24, so in `u32` the ticket, the count
|
|
1185
|
+
// times 64 slots plus the slot, is exact and below 2^30.
|
|
1186
|
+
const claim: u32 = (toU32(pool.claims[s]) + toU32(1)) & toU32(PG_ASYNC_CLAIM_MASK)
|
|
986
1187
|
pool.claimed[s] = 1
|
|
987
|
-
pool.claims[s] = (
|
|
1188
|
+
pool.claims[s] = toI32(claim)
|
|
988
1189
|
if (waited) {
|
|
989
1190
|
// Another waiter may find room this one did not take.
|
|
990
1191
|
pgAsyncWakePool(pool)
|
|
991
1192
|
}
|
|
992
|
-
return
|
|
1193
|
+
return toI32(claim * toU32(PG_ASYNC_MAX_SIZE) + toU32(s))
|
|
993
1194
|
}
|
|
994
1195
|
}
|
|
995
1196
|
if (!live) {
|
|
996
1197
|
return PG_POOL_NONE
|
|
997
1198
|
}
|
|
998
|
-
const
|
|
999
|
-
|
|
1199
|
+
const limit: i32 = pool.claimWaitMs
|
|
1200
|
+
const since: i32 = pgAsyncSince(began)
|
|
1201
|
+
if (limit <= 0 || since < 0 || since >= limit) {
|
|
1000
1202
|
return PG_CARRIER_FULL
|
|
1001
1203
|
}
|
|
1204
|
+
const left: i32 = limit - since
|
|
1002
1205
|
const skipped: boolean = await pgAsyncSkipAny(pool)
|
|
1003
1206
|
if (skipped) {
|
|
1004
1207
|
continue
|
|
1005
1208
|
}
|
|
1006
|
-
pool.waits
|
|
1209
|
+
pool.waits = pgAsyncCount(pool.waits)
|
|
1007
1210
|
waited = true
|
|
1008
|
-
await pgAsyncPark(pool,
|
|
1009
|
-
pgAsyncDrain(pool,
|
|
1211
|
+
await pgAsyncPark(pool, PG_ASYNC_POOL_WAKE, left)
|
|
1212
|
+
pgAsyncDrain(pool, PG_ASYNC_POOL_WAKE)
|
|
1010
1213
|
}
|
|
1011
1214
|
}
|
|
1012
1215
|
|
|
@@ -1022,14 +1225,24 @@ export const pgAsyncClaim = async (pool: PgAsyncPool): Promise<i32> => {
|
|
|
1022
1225
|
*/
|
|
1023
1226
|
export const pgAsyncRelease = (pool: PgAsyncPool, ticket: i32, reusable: boolean): void => {
|
|
1024
1227
|
const slot: i32 = pgAsyncSlot(pool, ticket)
|
|
1228
|
+
if (
|
|
1229
|
+
slot < 0 ||
|
|
1230
|
+
slot >= toI32(pool.fds.length) ||
|
|
1231
|
+
slot >= toI32(pool.claimed.length) ||
|
|
1232
|
+
slot >= toI32(pool.claims.length)
|
|
1233
|
+
) {
|
|
1234
|
+
// `pgAsyncSlot` panicked already on a ticket with no slot.
|
|
1235
|
+
return
|
|
1236
|
+
}
|
|
1237
|
+
const fd: i32 = pool.fds[slot]
|
|
1238
|
+
pool.claimed[slot] = 0
|
|
1239
|
+
pool.claims[slot] = pgAsyncStep(pool.claims[slot], PG_ASYNC_CLAIM_MASK)
|
|
1025
1240
|
if (!reusable) {
|
|
1026
|
-
netClose(pool.fds[slot])
|
|
1027
1241
|
pool.fds[slot] = -1
|
|
1028
|
-
pool.dropped
|
|
1242
|
+
pool.dropped = pgAsyncCount(pool.dropped)
|
|
1243
|
+
netClose(fd)
|
|
1029
1244
|
pgAsyncForget(pool, slot)
|
|
1030
1245
|
}
|
|
1031
|
-
pool.claimed[slot] = 0
|
|
1032
|
-
pool.claims[slot] = (pool.claims[slot] + 1) % PG_ASYNC_CLAIM_WRAP
|
|
1033
1246
|
pgAsyncWakePool(pool)
|
|
1034
1247
|
}
|
|
1035
1248
|
|
|
@@ -1063,13 +1276,20 @@ const pgAsyncUnqueue = (pool: PgAsyncPool, n: i32): void => {
|
|
|
1063
1276
|
return
|
|
1064
1277
|
}
|
|
1065
1278
|
if (n !== pool.seatHead) {
|
|
1066
|
-
|
|
1279
|
+
const at: i32 = n % PG_ASYNC_QUEUE
|
|
1280
|
+
if (at >= 0 && at < toI32(pool.seatGone.length)) {
|
|
1281
|
+
pool.seatGone[at] = 1
|
|
1282
|
+
}
|
|
1067
1283
|
return
|
|
1068
1284
|
}
|
|
1069
|
-
pool.seatHead = (pool.seatHead
|
|
1070
|
-
while (pool.seatHead !== pool.seatTail
|
|
1071
|
-
pool.
|
|
1072
|
-
pool.
|
|
1285
|
+
pool.seatHead = pgAsyncStep(pool.seatHead, PG_ASYNC_QUEUE_MASK)
|
|
1286
|
+
while (pool.seatHead !== pool.seatTail) {
|
|
1287
|
+
const at: i32 = pool.seatHead % PG_ASYNC_QUEUE
|
|
1288
|
+
if (at < 0 || at >= toI32(pool.seatGone.length) || toI32(pool.seatGone[at]) === 0) {
|
|
1289
|
+
break
|
|
1290
|
+
}
|
|
1291
|
+
pool.seatGone[at] = 0
|
|
1292
|
+
pool.seatHead = pgAsyncStep(pool.seatHead, PG_ASYNC_QUEUE_MASK)
|
|
1073
1293
|
}
|
|
1074
1294
|
pgAsyncWakePool(pool)
|
|
1075
1295
|
}
|
|
@@ -1114,21 +1334,34 @@ export const pgAsyncSeat = async (pool: PgAsyncPool, statement: i32): Promise<i3
|
|
|
1114
1334
|
}
|
|
1115
1335
|
let live: boolean = false
|
|
1116
1336
|
let best: i32 = -1
|
|
1337
|
+
let bestSeated: i32 = 0
|
|
1117
1338
|
let free: i32 = 0
|
|
1118
|
-
for (
|
|
1339
|
+
for (
|
|
1340
|
+
let s: i32 = 0;
|
|
1341
|
+
s < toI32(pool.fds.length) &&
|
|
1342
|
+
s < toI32(pool.claimed.length) &&
|
|
1343
|
+
s < toI32(pool.doomAt.length) &&
|
|
1344
|
+
s < toI32(pool.seated.length) &&
|
|
1345
|
+
s < toI32(pool.writing.length);
|
|
1346
|
+
s++
|
|
1347
|
+
) {
|
|
1119
1348
|
if (pool.fds[s] < 0) {
|
|
1120
1349
|
continue
|
|
1121
1350
|
}
|
|
1122
1351
|
live = true
|
|
1123
|
-
|
|
1352
|
+
const seated: i32 = pool.seated[s]
|
|
1353
|
+
if (toI32(pool.claimed[s]) !== 0 || pool.doomAt[s] >= 0 || seated >= pool.depth) {
|
|
1124
1354
|
continue
|
|
1125
1355
|
}
|
|
1126
|
-
|
|
1356
|
+
// In `i64` the sum cannot overflow; it never passes 64 slots of 16 seats.
|
|
1357
|
+
const next: i64 = toI64(free) + toI64(pool.depth) - toI64(seated)
|
|
1358
|
+
free = next > toI64(2147483647) ? 2147483647 : toI32(next)
|
|
1127
1359
|
if (toI32(pool.writing[s]) !== 0) {
|
|
1128
1360
|
continue
|
|
1129
1361
|
}
|
|
1130
|
-
if (best < 0 ||
|
|
1362
|
+
if (best < 0 || seated < bestSeated) {
|
|
1131
1363
|
best = s
|
|
1364
|
+
bestSeated = seated
|
|
1132
1365
|
}
|
|
1133
1366
|
}
|
|
1134
1367
|
// First come, first seated: under load a task that lost the seat it was
|
|
@@ -1138,52 +1371,77 @@ export const pgAsyncSeat = async (pool: PgAsyncPool, statement: i32): Promise<i3
|
|
|
1138
1371
|
// takes one only while none waits.
|
|
1139
1372
|
const turn: boolean =
|
|
1140
1373
|
queued >= 0
|
|
1141
|
-
? (queued
|
|
1374
|
+
? pgAsyncGap(queued, pool.seatHead, PG_ASYNC_QUEUE_MASK) < free
|
|
1142
1375
|
: pool.seatHead === pool.seatTail
|
|
1143
1376
|
if (best >= 0 && turn) {
|
|
1377
|
+
// `best` is below 64 and `statement` below 64, so in `u32` these
|
|
1378
|
+
// offsets are exact; each is then checked against its arrays.
|
|
1379
|
+
const known: i32 = toI32(toU32(best) * toU32(PG_ASYNC_MAX_STATEMENTS) + toU32(statement))
|
|
1144
1380
|
for (let k: i32 = 0; k < PG_ASYNC_MAX_DEPTH; k++) {
|
|
1145
|
-
const seat: i32 = best
|
|
1381
|
+
const seat: i32 = pgAsyncSeatAt(best, k)
|
|
1382
|
+
if (
|
|
1383
|
+
seat < 0 ||
|
|
1384
|
+
seat >= toI32(pool.seatUsed.length) ||
|
|
1385
|
+
seat >= toI32(pool.seatWaiting.length) ||
|
|
1386
|
+
seat >= toI32(pool.seatSeq.length) ||
|
|
1387
|
+
seat >= toI32(pool.seatStatement.length) ||
|
|
1388
|
+
seat >= toI32(pool.seatParse.length) ||
|
|
1389
|
+
seat >= toI32(pool.seatClaims.length) ||
|
|
1390
|
+
known < 0 ||
|
|
1391
|
+
known >= toI32(pool.prepared.length) ||
|
|
1392
|
+
best >= toI32(pool.nextSeq.length) ||
|
|
1393
|
+
best >= toI32(pool.seated.length) ||
|
|
1394
|
+
best >= toI32(pool.writing.length)
|
|
1395
|
+
) {
|
|
1396
|
+
// Past the slot's seats: none free on it.
|
|
1397
|
+
break
|
|
1398
|
+
}
|
|
1146
1399
|
if (toI32(pool.seatUsed[seat]) !== 0) {
|
|
1147
1400
|
continue
|
|
1148
1401
|
}
|
|
1149
|
-
|
|
1402
|
+
// The seat claim count wraps below 2^20, so the ticket, the count
|
|
1403
|
+
// times 1024 seats plus the seat, is exact in `u32` and below 2^30.
|
|
1404
|
+
const claim: u32 = (toU32(pool.seatClaims[seat]) + toU32(1)) & toU32(PG_ASYNC_SEAT_MASK)
|
|
1150
1405
|
pool.seatUsed[seat] = 1
|
|
1151
1406
|
pool.seatWaiting[seat] = 0
|
|
1152
1407
|
pool.seatSeq[seat] = pool.nextSeq[best]
|
|
1153
1408
|
pool.seatStatement[seat] = statement
|
|
1154
1409
|
pool.seatParse[seat] = toI32(pool.prepared[known]) === 0 ? 1 : 0
|
|
1155
1410
|
pool.prepared[known] = 1
|
|
1156
|
-
pool.nextSeq[best] = (pool.nextSeq[best]
|
|
1157
|
-
pool.seated[best]
|
|
1411
|
+
pool.nextSeq[best] = pgAsyncStep(pool.nextSeq[best], PG_ASYNC_SEQ_MASK)
|
|
1412
|
+
pool.seated[best] = pgAsyncCount(pool.seated[best])
|
|
1158
1413
|
pool.writing[best] = 1
|
|
1159
|
-
pool.seatClaims[seat] = (
|
|
1414
|
+
pool.seatClaims[seat] = toI32(claim)
|
|
1160
1415
|
// The next in the queue may find a seat this one did not take.
|
|
1161
1416
|
pgAsyncUnqueue(pool, queued)
|
|
1162
|
-
return
|
|
1417
|
+
return toI32(claim * toU32(PG_ASYNC_SEATS) + toU32(seat))
|
|
1163
1418
|
}
|
|
1164
1419
|
}
|
|
1165
1420
|
if (!live) {
|
|
1166
1421
|
pgAsyncUnqueue(pool, queued)
|
|
1167
1422
|
return PG_POOL_NONE
|
|
1168
1423
|
}
|
|
1169
|
-
const
|
|
1170
|
-
|
|
1424
|
+
const limit: i32 = pool.claimWaitMs
|
|
1425
|
+
const since: i32 = pgAsyncSince(began)
|
|
1426
|
+
if (limit <= 0 || since < 0 || since >= limit) {
|
|
1171
1427
|
pgAsyncUnqueue(pool, queued)
|
|
1172
1428
|
return PG_CARRIER_FULL
|
|
1173
1429
|
}
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
(pool.seatTail - pool.seatHead + PG_ASYNC_QUEUE_WRAP) % PG_ASYNC_QUEUE_WRAP < PG_ASYNC_QUEUE
|
|
1177
|
-
) {
|
|
1430
|
+
const left: i32 = limit - since
|
|
1431
|
+
if (queued < 0 && pgAsyncGap(pool.seatTail, pool.seatHead, PG_ASYNC_QUEUE_MASK) < PG_ASYNC_QUEUE) {
|
|
1178
1432
|
queued = pool.seatTail
|
|
1179
|
-
pool.seatTail = (pool.seatTail
|
|
1433
|
+
pool.seatTail = pgAsyncStep(pool.seatTail, PG_ASYNC_QUEUE_MASK)
|
|
1180
1434
|
}
|
|
1181
1435
|
const skipped: boolean = await pgAsyncSkipAny(pool)
|
|
1182
1436
|
if (skipped) {
|
|
1183
1437
|
continue
|
|
1184
1438
|
}
|
|
1185
|
-
|
|
1186
|
-
|
|
1439
|
+
// A queued task waits on its queue lane, after the pool's own wake.
|
|
1440
|
+
const w: i32 =
|
|
1441
|
+
queued >= 0
|
|
1442
|
+
? toI32(toU32(PG_ASYNC_POOL_WAKE + 1) + (toU32(queued) & toU32(PG_ASYNC_LANES - 1)))
|
|
1443
|
+
: PG_ASYNC_POOL_WAKE
|
|
1444
|
+
pool.waits = pgAsyncCount(pool.waits)
|
|
1187
1445
|
await pgAsyncPark(pool, w, left)
|
|
1188
1446
|
pgAsyncDrain(pool, w)
|
|
1189
1447
|
}
|
|
@@ -1194,7 +1452,8 @@ const pgAsyncSeatOf = (pool: PgAsyncPool, ticket: i32): i32 => {
|
|
|
1194
1452
|
const seat: i32 = ticket >= 0 ? ticket % PG_ASYNC_SEATS : -1
|
|
1195
1453
|
if (
|
|
1196
1454
|
seat < 0 ||
|
|
1197
|
-
seat >= pool.
|
|
1455
|
+
seat >= toI32(pool.seatUsed.length) ||
|
|
1456
|
+
seat >= toI32(pool.seatClaims.length) ||
|
|
1198
1457
|
toI32(pool.seatUsed[seat]) !== 1 ||
|
|
1199
1458
|
toI32(ticket / PG_ASYNC_SEATS) !== pool.seatClaims[seat]
|
|
1200
1459
|
) {
|
|
@@ -1222,7 +1481,23 @@ export const pgAsyncSend = async (pool: PgAsyncPool, ticket: i32, params: PgAsyn
|
|
|
1222
1481
|
if (cancelled()) {
|
|
1223
1482
|
return PG_ASYNC_UNSENT_CANCELLED
|
|
1224
1483
|
}
|
|
1484
|
+
const s: i32 = toI32(seat / PG_ASYNC_MAX_DEPTH)
|
|
1485
|
+
if (
|
|
1486
|
+
seat < 0 ||
|
|
1487
|
+
seat >= toI32(pool.seatStatement.length) ||
|
|
1488
|
+
seat >= toI32(pool.seatParse.length) ||
|
|
1489
|
+
s < 0 ||
|
|
1490
|
+
s >= toI32(pool.fds.length)
|
|
1491
|
+
) {
|
|
1492
|
+
// `pgAsyncSeatOf` panicked already on a ticket with no seat.
|
|
1493
|
+
return PG_ASYNC_UNSENT
|
|
1494
|
+
}
|
|
1225
1495
|
const statement: i32 = pool.seatStatement[seat]
|
|
1496
|
+
if (statement < 0 || statement >= toI32(pool.statements.length)) {
|
|
1497
|
+
// `pgAsyncSeat` seats only a registered statement.
|
|
1498
|
+
return PG_ASYNC_UNSENT
|
|
1499
|
+
}
|
|
1500
|
+
const fd: i32 = pool.fds[s]
|
|
1226
1501
|
const out: u8[] | null = pgAsyncEncode(
|
|
1227
1502
|
pool.statements[statement],
|
|
1228
1503
|
statement,
|
|
@@ -1232,10 +1507,10 @@ export const pgAsyncSend = async (pool: PgAsyncPool, ticket: i32, params: PgAsyn
|
|
|
1232
1507
|
if (out === null) {
|
|
1233
1508
|
return PG_ASYNC_UNSENT
|
|
1234
1509
|
}
|
|
1235
|
-
const fd: i32 = pool.fds[toI32(seat / PG_ASYNC_MAX_DEPTH)]
|
|
1236
1510
|
let sent: i32 = 0
|
|
1237
|
-
while (sent < toI32(out.length)) {
|
|
1238
|
-
|
|
1511
|
+
while (sent >= 0 && sent < toI32(out.length)) {
|
|
1512
|
+
// Both are at least 0 and `sent` is short of the end, so the `u32` difference is exact.
|
|
1513
|
+
const n: i32 = netWrite(fd, out, sent, toI32(toU32(toI32(out.length)) - toU32(sent)))
|
|
1239
1514
|
if (n === PG_ASYNC_AGAIN) {
|
|
1240
1515
|
const waited: boolean = await writable(fd)
|
|
1241
1516
|
const failed: i32 = pgAsyncWaitFailure(waited)
|
|
@@ -1243,41 +1518,106 @@ export const pgAsyncSend = async (pool: PgAsyncPool, ticket: i32, params: PgAsyn
|
|
|
1243
1518
|
return PG_ASYNC_UNSENT_CANCELLED
|
|
1244
1519
|
}
|
|
1245
1520
|
if (failed !== 0) {
|
|
1246
|
-
return
|
|
1521
|
+
return pgAsyncBroken(failed)
|
|
1247
1522
|
}
|
|
1248
1523
|
} else if (n <= 0) {
|
|
1249
|
-
return
|
|
1524
|
+
return n < PG_ASYNC_BROKEN ? pgAsyncBroken(n) : PG_ASYNC_BROKEN
|
|
1250
1525
|
} else {
|
|
1251
|
-
sent
|
|
1526
|
+
// A write takes no more than it was given, so `sent` stays within `out`.
|
|
1527
|
+
const next: i64 = toI64(sent) + toI64(n)
|
|
1528
|
+
sent = next > toI64(out.length) ? toI32(out.length) : toI32(next)
|
|
1252
1529
|
}
|
|
1253
1530
|
}
|
|
1254
1531
|
return sent
|
|
1255
1532
|
}
|
|
1256
1533
|
|
|
1257
|
-
/**
|
|
1258
|
-
|
|
1259
|
-
|
|
1534
|
+
/**
|
|
1535
|
+
* What `pgAsyncSend` answers for a broken write: `PG_ASYNC_BROKEN` beside
|
|
1536
|
+
* the failure `e` (0 or below), so it stays below -15, and no lower than
|
|
1537
|
+
* the least `i32`.
|
|
1538
|
+
*/
|
|
1539
|
+
const pgAsyncBroken = (e: i32): i32 => {
|
|
1540
|
+
const sum: i64 = toI64(PG_ASYNC_BROKEN) + toI64(e)
|
|
1541
|
+
return sum < toI64(-2147483648) ? -2147483648 : toI32(sum)
|
|
1542
|
+
}
|
|
1543
|
+
|
|
1544
|
+
/**
|
|
1545
|
+
* How far `seq` is past `served`, a slot's next answer, in the order of its
|
|
1546
|
+
* pipeline: both wrap below `PG_ASYNC_SEQ_WRAP`.
|
|
1547
|
+
*/
|
|
1548
|
+
const pgAsyncAhead = (served: i32, seq: i32): i32 => pgAsyncGap(seq, served, PG_ASYNC_SEQ_MASK)
|
|
1549
|
+
|
|
1550
|
+
/**
|
|
1551
|
+
* Where slot `s`'s `k`th seat is in the seat arrays, `s * PG_ASYNC_MAX_DEPTH
|
|
1552
|
+
* + k`: in `u32` the sum wraps rather than checks, and every caller checks
|
|
1553
|
+
* the seat against the arrays it reads.
|
|
1554
|
+
*/
|
|
1555
|
+
const pgAsyncSeatAt = (s: i32, k: i32): i32 => toI32(toU32(s) * toU32(PG_ASYNC_MAX_DEPTH) + toU32(k))
|
|
1260
1556
|
|
|
1261
|
-
/**
|
|
1557
|
+
/**
|
|
1558
|
+
* Whether seat `seat`'s answer is lost: its connection failed at or before
|
|
1559
|
+
* it. A seat that is not in the pool has no answer to read, so it counts as
|
|
1560
|
+
* lost.
|
|
1561
|
+
*/
|
|
1262
1562
|
const pgAsyncLostSeat = (pool: PgAsyncPool, seat: i32): boolean => {
|
|
1263
1563
|
const s: i32 = toI32(seat / PG_ASYNC_MAX_DEPTH)
|
|
1564
|
+
if (
|
|
1565
|
+
seat < 0 ||
|
|
1566
|
+
s < 0 ||
|
|
1567
|
+
seat >= toI32(pool.seatSeq.length) ||
|
|
1568
|
+
s >= toI32(pool.doomAt.length) ||
|
|
1569
|
+
s >= toI32(pool.served.length)
|
|
1570
|
+
) {
|
|
1571
|
+
return true
|
|
1572
|
+
}
|
|
1264
1573
|
const at: i32 = pool.doomAt[s]
|
|
1265
|
-
|
|
1574
|
+
const served: i32 = pool.served[s]
|
|
1575
|
+
return at >= 0 && pgAsyncAhead(served, pool.seatSeq[seat]) >= pgAsyncAhead(served, at)
|
|
1266
1576
|
}
|
|
1267
1577
|
|
|
1578
|
+
/**
|
|
1579
|
+
* Whether seat `seat`'s answer is the next its connection gives, lost or
|
|
1580
|
+
* not. A seat that is not in the pool is never served.
|
|
1581
|
+
*/
|
|
1582
|
+
const pgAsyncServed = (pool: PgAsyncPool, seat: i32): boolean => {
|
|
1583
|
+
const s: i32 = toI32(seat / PG_ASYNC_MAX_DEPTH)
|
|
1584
|
+
if (seat < 0 || s < 0 || seat >= toI32(pool.seatSeq.length) || s >= toI32(pool.served.length)) {
|
|
1585
|
+
return false
|
|
1586
|
+
}
|
|
1587
|
+
return pool.served[s] === pool.seatSeq[seat]
|
|
1588
|
+
}
|
|
1589
|
+
|
|
1590
|
+
/**
|
|
1591
|
+
* Whether seat `seat`'s answer is the next its connection gives and is not
|
|
1592
|
+
* lost. A seat that is not in the pool never has its turn.
|
|
1593
|
+
*/
|
|
1594
|
+
const pgAsyncInTurn = (pool: PgAsyncPool, seat: i32): boolean =>
|
|
1595
|
+
pgAsyncServed(pool, seat) && !pgAsyncLostSeat(pool, seat)
|
|
1596
|
+
|
|
1597
|
+
/** The turn lane seat `seat` waits on: distinct for the seats of any eight slots in a row. */
|
|
1598
|
+
const pgAsyncTurnLane = (seat: i32): i32 => seat % PG_ASYNC_LANES
|
|
1599
|
+
|
|
1268
1600
|
/**
|
|
1269
1601
|
* Whether a task waiting on slot `s`, other than `except`'s, may go on: its
|
|
1270
1602
|
* turn came, its answer is lost, or an abandoned answer ahead of it waits to
|
|
1271
|
-
* be read past.
|
|
1603
|
+
* be read past. No task waits on a slot that is not in the pool.
|
|
1272
1604
|
*/
|
|
1273
1605
|
const pgAsyncReady = (pool: PgAsyncPool, s: i32, except: i32): boolean => {
|
|
1606
|
+
if (s < 0 || s >= toI32(pool.served.length)) {
|
|
1607
|
+
return false
|
|
1608
|
+
}
|
|
1609
|
+
const served: i32 = pool.served[s]
|
|
1274
1610
|
let waiting: boolean = false
|
|
1275
1611
|
for (let k: i32 = 0; k < PG_ASYNC_MAX_DEPTH; k++) {
|
|
1276
|
-
const seat: i32 = s
|
|
1612
|
+
const seat: i32 = pgAsyncSeatAt(s, k)
|
|
1613
|
+
if (seat < 0 || seat >= toI32(pool.seatWaiting.length) || seat >= toI32(pool.seatSeq.length)) {
|
|
1614
|
+
// Past the pool's seats: none more on this slot.
|
|
1615
|
+
break
|
|
1616
|
+
}
|
|
1277
1617
|
if (seat === except || toI32(pool.seatWaiting[seat]) === 0) {
|
|
1278
1618
|
continue
|
|
1279
1619
|
}
|
|
1280
|
-
if (pool.seatSeq[seat] ===
|
|
1620
|
+
if (pool.seatSeq[seat] === served || pgAsyncLostSeat(pool, seat)) {
|
|
1281
1621
|
return true
|
|
1282
1622
|
}
|
|
1283
1623
|
waiting = true
|
|
@@ -1285,18 +1625,80 @@ const pgAsyncReady = (pool: PgAsyncPool, s: i32, except: i32): boolean => {
|
|
|
1285
1625
|
return waiting && pgAsyncSkippable(pool, s) >= 0
|
|
1286
1626
|
}
|
|
1287
1627
|
|
|
1628
|
+
/**
|
|
1629
|
+
* Wakes the seats waiting for their turn on slot `s`, other than `except`'s,
|
|
1630
|
+
* when one of them may go on: each on its turn lane, in the order their
|
|
1631
|
+
* answers come. A seat whose answer is read wakes the next within the same
|
|
1632
|
+
* loop turn only when the loop runs them in that order. The runtime promises
|
|
1633
|
+
* only the kernel's order: Linux epoll hands back distinct descriptors in the
|
|
1634
|
+
* order they became ready, so lanes rung in order run in order there, while
|
|
1635
|
+
* kqueue specifies no order and a seat run early re-parks and costs a turn.
|
|
1636
|
+
* One wake shared by them all ran them in the order they last waited, often
|
|
1637
|
+
* the reverse, so seats freed together were freed one a turn.
|
|
1638
|
+
*/
|
|
1639
|
+
const pgAsyncWakeTurns = (pool: PgAsyncPool, s: i32, except: i32): void => {
|
|
1640
|
+
if (!pgAsyncReady(pool, s, except)) {
|
|
1641
|
+
return
|
|
1642
|
+
}
|
|
1643
|
+
let after: i32 = -1
|
|
1644
|
+
for (let n: i32 = 0; n < PG_ASYNC_MAX_DEPTH; n++) {
|
|
1645
|
+
// Read again each round: the signal below is a call.
|
|
1646
|
+
if (s < 0 || s >= toI32(pool.served.length)) {
|
|
1647
|
+
return
|
|
1648
|
+
}
|
|
1649
|
+
const served: i32 = pool.served[s]
|
|
1650
|
+
let next: i32 = -1
|
|
1651
|
+
let nextAhead: i32 = PG_ASYNC_SEQ_WRAP
|
|
1652
|
+
for (let k: i32 = 0; k < PG_ASYNC_MAX_DEPTH; k++) {
|
|
1653
|
+
const seat: i32 = pgAsyncSeatAt(s, k)
|
|
1654
|
+
if (seat < 0 || seat >= toI32(pool.seatSeq.length) || seat >= toI32(pool.seatWaiting.length)) {
|
|
1655
|
+
// Past the pool's seats: none more on this slot.
|
|
1656
|
+
break
|
|
1657
|
+
}
|
|
1658
|
+
const ahead: i32 = pgAsyncAhead(served, pool.seatSeq[seat])
|
|
1659
|
+
if (seat !== except && toI32(pool.seatWaiting[seat]) === 1 && ahead > after && ahead < nextAhead) {
|
|
1660
|
+
next = seat
|
|
1661
|
+
nextAhead = ahead
|
|
1662
|
+
}
|
|
1663
|
+
}
|
|
1664
|
+
if (next < 0) {
|
|
1665
|
+
return
|
|
1666
|
+
}
|
|
1667
|
+
pgAsyncSignal(pool, pgAsyncTurnLane(next))
|
|
1668
|
+
after = nextAhead
|
|
1669
|
+
}
|
|
1670
|
+
}
|
|
1671
|
+
|
|
1288
1672
|
/**
|
|
1289
1673
|
* Marks slot `s` lost from sequence number `seq` on, settles the abandoned
|
|
1290
|
-
* seats it loses, and wakes those it fails.
|
|
1674
|
+
* seats it loses, and wakes those it fails. A slot that is not in the pool
|
|
1675
|
+
* has nothing to lose.
|
|
1291
1676
|
*/
|
|
1292
1677
|
const pgAsyncDoom = (pool: PgAsyncPool, s: i32, seq: i32): void => {
|
|
1293
|
-
if (
|
|
1678
|
+
if (s < 0 || s >= toI32(pool.doomAt.length) || s >= toI32(pool.served.length)) {
|
|
1679
|
+
return
|
|
1680
|
+
}
|
|
1681
|
+
const at: i32 = pool.doomAt[s]
|
|
1682
|
+
const served: i32 = pool.served[s]
|
|
1683
|
+
if (at < 0 || pgAsyncAhead(served, seq) < pgAsyncAhead(served, at)) {
|
|
1294
1684
|
pool.doomAt[s] = seq
|
|
1295
1685
|
}
|
|
1296
1686
|
// A seat freed here closes the connection only once no seat is left on it;
|
|
1297
1687
|
// the one being read past is settled by its reader.
|
|
1298
1688
|
for (let k: i32 = 0; k < PG_ASYNC_MAX_DEPTH; k++) {
|
|
1299
|
-
const seat: i32 = s
|
|
1689
|
+
const seat: i32 = pgAsyncSeatAt(s, k)
|
|
1690
|
+
// Checked each round: settling a seat is a call.
|
|
1691
|
+
if (
|
|
1692
|
+
seat < 0 ||
|
|
1693
|
+
seat >= toI32(pool.seatAbandoned.length) ||
|
|
1694
|
+
seat >= toI32(pool.seatSeq.length) ||
|
|
1695
|
+
seat >= toI32(pool.seatUsed.length) ||
|
|
1696
|
+
s >= toI32(pool.skipping.length) ||
|
|
1697
|
+
s >= toI32(pool.served.length)
|
|
1698
|
+
) {
|
|
1699
|
+
// Past the pool's seats: none more on this slot.
|
|
1700
|
+
break
|
|
1701
|
+
}
|
|
1300
1702
|
const state: i32 = toI32(pool.seatAbandoned[seat])
|
|
1301
1703
|
const reading: boolean = toI32(pool.skipping[s]) === 1 && pool.seatSeq[seat] === pool.served[s]
|
|
1302
1704
|
if (
|
|
@@ -1308,30 +1710,34 @@ const pgAsyncDoom = (pool: PgAsyncPool, s: i32, seq: i32): void => {
|
|
|
1308
1710
|
pgAsyncSettle(pool, seat)
|
|
1309
1711
|
}
|
|
1310
1712
|
}
|
|
1311
|
-
|
|
1312
|
-
pgAsyncSignal(pool, s)
|
|
1313
|
-
}
|
|
1713
|
+
pgAsyncWakeTurns(pool, s, -1)
|
|
1314
1714
|
}
|
|
1315
1715
|
|
|
1316
1716
|
/**
|
|
1317
1717
|
* Leaves seat `seat` to the pool: its task was cancelled with its answer
|
|
1318
1718
|
* unread, so the next task to wait on the connection, or for a seat on it,
|
|
1319
|
-
* reads past that answer instead of the connection being closed.
|
|
1719
|
+
* reads past that answer instead of the connection being closed. A seat that
|
|
1720
|
+
* is not in the pool has nothing to leave.
|
|
1320
1721
|
*/
|
|
1321
1722
|
const pgAsyncAbandon = (pool: PgAsyncPool, seat: i32): void => {
|
|
1723
|
+
if (seat < 0 || seat >= toI32(pool.seatAbandoned.length)) {
|
|
1724
|
+
return
|
|
1725
|
+
}
|
|
1322
1726
|
const s: i32 = toI32(seat / PG_ASYNC_MAX_DEPTH)
|
|
1323
1727
|
pool.seatAbandoned[seat] = 1
|
|
1324
|
-
|
|
1325
|
-
pgAsyncSignal(pool, s)
|
|
1326
|
-
}
|
|
1728
|
+
pgAsyncWakeTurns(pool, s, seat)
|
|
1327
1729
|
pgAsyncWakePool(pool)
|
|
1328
1730
|
}
|
|
1329
1731
|
|
|
1330
1732
|
/**
|
|
1331
1733
|
* An abandoned seat whose answer is read past or lost: freed once its ticket is
|
|
1332
|
-
* spent, else marked so `pgAsyncDone` frees it.
|
|
1734
|
+
* spent, else marked so `pgAsyncDone` frees it. A seat that is not in the pool
|
|
1735
|
+
* has nothing to settle.
|
|
1333
1736
|
*/
|
|
1334
1737
|
const pgAsyncSettle = (pool: PgAsyncPool, seat: i32): void => {
|
|
1738
|
+
if (seat < 0 || seat >= toI32(pool.seatAbandoned.length)) {
|
|
1739
|
+
return
|
|
1740
|
+
}
|
|
1335
1741
|
if (toI32(pool.seatAbandoned[seat]) === 2) {
|
|
1336
1742
|
pgAsyncFree(pool, seat)
|
|
1337
1743
|
} else {
|
|
@@ -1341,16 +1747,34 @@ const pgAsyncSettle = (pool: PgAsyncPool, seat: i32): void => {
|
|
|
1341
1747
|
|
|
1342
1748
|
/** The abandoned seat in turn on slot `s` when no task reads past it yet, or -1. */
|
|
1343
1749
|
const pgAsyncSkippable = (pool: PgAsyncPool, s: i32): i32 => {
|
|
1750
|
+
if (
|
|
1751
|
+
s < 0 ||
|
|
1752
|
+
s >= toI32(pool.fds.length) ||
|
|
1753
|
+
s >= toI32(pool.skipping.length) ||
|
|
1754
|
+
s >= toI32(pool.served.length)
|
|
1755
|
+
) {
|
|
1756
|
+
return -1
|
|
1757
|
+
}
|
|
1344
1758
|
if (pool.fds[s] < 0 || toI32(pool.skipping[s]) !== 0) {
|
|
1345
1759
|
return -1
|
|
1346
1760
|
}
|
|
1761
|
+
const served: i32 = pool.served[s]
|
|
1347
1762
|
for (let k: i32 = 0; k < PG_ASYNC_MAX_DEPTH; k++) {
|
|
1348
|
-
const seat: i32 = s
|
|
1763
|
+
const seat: i32 = pgAsyncSeatAt(s, k)
|
|
1764
|
+
if (
|
|
1765
|
+
seat < 0 ||
|
|
1766
|
+
seat >= toI32(pool.seatAbandoned.length) ||
|
|
1767
|
+
seat >= toI32(pool.seatUsed.length) ||
|
|
1768
|
+
seat >= toI32(pool.seatSeq.length)
|
|
1769
|
+
) {
|
|
1770
|
+
// Past the pool's seats: none more on this slot.
|
|
1771
|
+
break
|
|
1772
|
+
}
|
|
1349
1773
|
const state: i32 = toI32(pool.seatAbandoned[seat])
|
|
1350
1774
|
if (
|
|
1351
1775
|
toI32(pool.seatUsed[seat]) === 1 &&
|
|
1352
1776
|
(state === 1 || state === 2) &&
|
|
1353
|
-
pool.seatSeq[seat] ===
|
|
1777
|
+
pool.seatSeq[seat] === served &&
|
|
1354
1778
|
!pgAsyncLostSeat(pool, seat)
|
|
1355
1779
|
) {
|
|
1356
1780
|
return seat
|
|
@@ -1359,44 +1783,152 @@ const pgAsyncSkippable = (pool: PgAsyncPool, s: i32): i32 => {
|
|
|
1359
1783
|
return -1
|
|
1360
1784
|
}
|
|
1361
1785
|
|
|
1786
|
+
/**
|
|
1787
|
+
* Moves the unread bytes of slot `s` to the front of `input`, its input, and
|
|
1788
|
+
* answers where the next read goes. It answers -1 when the input has no room
|
|
1789
|
+
* left, when its marks are out of order (`pgAsyncAnswer` then fails the
|
|
1790
|
+
* answer, or the reader past it loses the connection), and for a slot that
|
|
1791
|
+
* is not in the pool.
|
|
1792
|
+
*/
|
|
1793
|
+
const pgAsyncRoom = (pool: PgAsyncPool, s: i32, input: u8[]): i32 => {
|
|
1794
|
+
if (s < 0 || s >= toI32(pool.inStart.length) || s >= toI32(pool.inEnd.length)) {
|
|
1795
|
+
return -1
|
|
1796
|
+
}
|
|
1797
|
+
const start: i32 = pool.inStart[s]
|
|
1798
|
+
const end: i32 = pool.inEnd[s]
|
|
1799
|
+
if (start < 0 || end < start || end > toI32(input.length)) {
|
|
1800
|
+
return -1
|
|
1801
|
+
}
|
|
1802
|
+
let room: i32 = end
|
|
1803
|
+
if (start > 0) {
|
|
1804
|
+
// In `u32`: `end` is at or past `start`, so the difference is the bytes kept.
|
|
1805
|
+
room = toI32(toU32(end) - toU32(start))
|
|
1806
|
+
if (!tryCopyBytes(input, 0, input, start, room)) {
|
|
1807
|
+
return -1
|
|
1808
|
+
}
|
|
1809
|
+
pool.inEnd[s] = room
|
|
1810
|
+
pool.inStart[s] = 0
|
|
1811
|
+
}
|
|
1812
|
+
return room < toI32(input.length) ? room : -1
|
|
1813
|
+
}
|
|
1814
|
+
|
|
1815
|
+
/**
|
|
1816
|
+
* Records `got` more bytes read into slot `s`'s input at `end`, where
|
|
1817
|
+
* `pgAsyncRoom` put the read: a read takes no more than it was given, so the
|
|
1818
|
+
* sum stays within the input's `length`.
|
|
1819
|
+
*/
|
|
1820
|
+
const pgAsyncTook = (pool: PgAsyncPool, s: i32, end: i32, got: i32, length: i32): void => {
|
|
1821
|
+
if (s < 0 || s >= toI32(pool.inEnd.length)) {
|
|
1822
|
+
return
|
|
1823
|
+
}
|
|
1824
|
+
const next: i64 = toI64(end) + toI64(got)
|
|
1825
|
+
pool.inEnd[s] = next > toI64(length) ? length : toI32(next)
|
|
1826
|
+
}
|
|
1827
|
+
|
|
1828
|
+
/**
|
|
1829
|
+
* Marks the statement seat `seat` runs on slot `s` as not prepared, so the
|
|
1830
|
+
* next seat that runs it sends its Parse again. A seat or statement out of
|
|
1831
|
+
* the pool's range has nothing prepared.
|
|
1832
|
+
*/
|
|
1833
|
+
const pgAsyncUnprepare = (pool: PgAsyncPool, s: i32, seat: i32): void => {
|
|
1834
|
+
// 64 is both PG_ASYNC_MAX_SIZE and PG_ASYNC_MAX_STATEMENTS, written as a
|
|
1835
|
+
// literal: the prover takes a bound from a literal but not from a module
|
|
1836
|
+
// constant, and the bounds prove the offset below cannot overflow.
|
|
1837
|
+
if (s < 0 || s >= 64 || seat < 0 || seat >= toI32(pool.seatStatement.length)) {
|
|
1838
|
+
return
|
|
1839
|
+
}
|
|
1840
|
+
const q: i32 = pool.seatStatement[seat]
|
|
1841
|
+
if (q < 0 || q >= 64) {
|
|
1842
|
+
return
|
|
1843
|
+
}
|
|
1844
|
+
const at: i32 = s * PG_ASYNC_MAX_STATEMENTS + q
|
|
1845
|
+
if (at < toI32(pool.prepared.length)) {
|
|
1846
|
+
pool.prepared[at] = 0
|
|
1847
|
+
}
|
|
1848
|
+
}
|
|
1849
|
+
|
|
1850
|
+
/**
|
|
1851
|
+
* Moves slot `s` past the answer in turn, seat `seat`'s at sequence number
|
|
1852
|
+
* `seq`: its input to where the answer ends, and its order to the next seat.
|
|
1853
|
+
* A Parse in the seat's write the answer does not show taken marks its
|
|
1854
|
+
* statement for parsing again. A slot or seat that is not in the pool has
|
|
1855
|
+
* nothing to move past.
|
|
1856
|
+
*/
|
|
1857
|
+
const pgAsyncPast = (pool: PgAsyncPool, s: i32, seat: i32, seq: i32): void => {
|
|
1858
|
+
if (
|
|
1859
|
+
s < 0 ||
|
|
1860
|
+
s >= toI32(pool.inStart.length) ||
|
|
1861
|
+
s >= toI32(pool.inEnd.length) ||
|
|
1862
|
+
s >= toI32(pool.inAnswer.length) ||
|
|
1863
|
+
s >= toI32(pool.inParsed.length) ||
|
|
1864
|
+
s >= toI32(pool.served.length) ||
|
|
1865
|
+
seat < 0 ||
|
|
1866
|
+
seat >= toI32(pool.seatParse.length)
|
|
1867
|
+
) {
|
|
1868
|
+
return
|
|
1869
|
+
}
|
|
1870
|
+
const end: i32 = pool.inEnd[s]
|
|
1871
|
+
const start: i32 = pool.inAnswer[s] >= 0 ? pool.inAnswer[s] : end
|
|
1872
|
+
if (start === end) {
|
|
1873
|
+
pool.inStart[s] = 0
|
|
1874
|
+
pool.inEnd[s] = 0
|
|
1875
|
+
} else {
|
|
1876
|
+
pool.inStart[s] = start
|
|
1877
|
+
}
|
|
1878
|
+
pool.served[s] = pgAsyncStep(seq, PG_ASYNC_SEQ_MASK)
|
|
1879
|
+
if (toI32(pool.seatParse[seat]) === 1 && toI32(pool.inParsed[s]) === 0) {
|
|
1880
|
+
pgAsyncUnprepare(pool, s, seat)
|
|
1881
|
+
}
|
|
1882
|
+
}
|
|
1883
|
+
|
|
1884
|
+
/** Marks slot `s` as read by no task, when it is in the pool. */
|
|
1885
|
+
const pgAsyncUnskip = (pool: PgAsyncPool, s: i32): void => {
|
|
1886
|
+
if (s >= 0 && s < toI32(pool.skipping.length)) {
|
|
1887
|
+
pool.skipping[s] = 0
|
|
1888
|
+
}
|
|
1889
|
+
}
|
|
1890
|
+
|
|
1362
1891
|
/**
|
|
1363
1892
|
* Reads past the answer of `seat`, an abandoned seat in turn on slot `s`,
|
|
1364
1893
|
* and serves the next. An answer that is not a plain one ending idle (a
|
|
1365
1894
|
* frame this module does not expect, a transaction left open, a setting
|
|
1366
1895
|
* changed) or larger than the pool's input loses the connection from it on,
|
|
1367
1896
|
* as does a socket error. A cancelled reader stops and leaves the bytes read
|
|
1368
|
-
* for the next.
|
|
1897
|
+
* for the next. A slot that is not in the pool has nothing to read past.
|
|
1369
1898
|
*/
|
|
1370
1899
|
const pgAsyncSkip = async (pool: PgAsyncPool, s: i32, seat: i32): Promise<void> => {
|
|
1900
|
+
if (
|
|
1901
|
+
s < 0 ||
|
|
1902
|
+
s >= toI32(pool.served.length) ||
|
|
1903
|
+
s >= toI32(pool.fds.length) ||
|
|
1904
|
+
s >= toI32(pool.inputs.length) ||
|
|
1905
|
+
s >= toI32(pool.skipping.length)
|
|
1906
|
+
) {
|
|
1907
|
+
return
|
|
1908
|
+
}
|
|
1371
1909
|
const seq: i32 = pool.served[s]
|
|
1372
1910
|
const fd: i32 = pool.fds[s]
|
|
1373
1911
|
const input: u8[] = pool.inputs[s]
|
|
1374
1912
|
pool.skipping[s] = 1
|
|
1375
1913
|
let fit: boolean = true
|
|
1376
1914
|
while (!pgAsyncScan(pool, s)) {
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
pool.inEnd[s] -= pool.inStart[s]
|
|
1380
|
-
pool.inStart[s] = 0
|
|
1381
|
-
}
|
|
1382
|
-
if (pool.inEnd[s] >= toI32(input.length)) {
|
|
1915
|
+
const end: i32 = pgAsyncRoom(pool, s, input)
|
|
1916
|
+
if (end < 0 || end >= toI32(input.length)) {
|
|
1383
1917
|
fit = false
|
|
1384
1918
|
break
|
|
1385
1919
|
}
|
|
1386
|
-
const got: i32 = netRead(fd, input,
|
|
1920
|
+
const got: i32 = netRead(fd, input, end, toI32(input.length) - end)
|
|
1387
1921
|
if (got === PG_ASYNC_AGAIN) {
|
|
1388
1922
|
const waited: boolean = await readable(fd)
|
|
1389
1923
|
if (pgAsyncLostSeat(pool, seat)) {
|
|
1390
|
-
pool
|
|
1924
|
+
pgAsyncUnskip(pool, s)
|
|
1391
1925
|
pgAsyncSettle(pool, seat)
|
|
1392
1926
|
return
|
|
1393
1927
|
}
|
|
1394
1928
|
if (!waited && cancelled()) {
|
|
1395
1929
|
// Another task reads on from where this one stopped.
|
|
1396
|
-
pool
|
|
1397
|
-
|
|
1398
|
-
pgAsyncSignal(pool, s)
|
|
1399
|
-
}
|
|
1930
|
+
pgAsyncUnskip(pool, s)
|
|
1931
|
+
pgAsyncWakeTurns(pool, s, -1)
|
|
1400
1932
|
pgAsyncWakePool(pool)
|
|
1401
1933
|
return
|
|
1402
1934
|
}
|
|
@@ -1408,49 +1940,65 @@ const pgAsyncSkip = async (pool: PgAsyncPool, s: i32, seat: i32): Promise<void>
|
|
|
1408
1940
|
fit = false
|
|
1409
1941
|
break
|
|
1410
1942
|
} else {
|
|
1411
|
-
pool
|
|
1943
|
+
pgAsyncTook(pool, s, end, got, toI32(input.length))
|
|
1412
1944
|
}
|
|
1413
1945
|
}
|
|
1414
|
-
pool
|
|
1415
|
-
|
|
1946
|
+
pgAsyncUnskip(pool, s)
|
|
1947
|
+
// `pgAsyncPlain` answers false for an answer with no end found.
|
|
1948
|
+
if (!fit || !pgAsyncPlain(pool, s)) {
|
|
1416
1949
|
pgAsyncDoom(pool, s, seq)
|
|
1417
1950
|
return
|
|
1418
1951
|
}
|
|
1419
|
-
pool
|
|
1420
|
-
if (pool.inStart[s] === pool.inEnd[s]) {
|
|
1421
|
-
pool.inStart[s] = 0
|
|
1422
|
-
pool.inEnd[s] = 0
|
|
1423
|
-
}
|
|
1424
|
-
pool.served[s] = (seq + 1) % PG_ASYNC_SEQ_WRAP
|
|
1425
|
-
if (toI32(pool.seatParse[seat]) === 1 && toI32(pool.inParsed[s]) === 0) {
|
|
1426
|
-
pool.prepared[s * PG_ASYNC_MAX_STATEMENTS + pool.seatStatement[seat]] = 0
|
|
1427
|
-
}
|
|
1952
|
+
pgAsyncPast(pool, s, seat, seq)
|
|
1428
1953
|
pgAsyncSettle(pool, seat)
|
|
1429
|
-
|
|
1430
|
-
pgAsyncSignal(pool, s)
|
|
1431
|
-
}
|
|
1954
|
+
pgAsyncWakeTurns(pool, s, -1)
|
|
1432
1955
|
pgAsyncWakePool(pool)
|
|
1433
1956
|
}
|
|
1434
1957
|
|
|
1435
1958
|
/**
|
|
1436
1959
|
* Whether the answer in turn on slot `s`, at `[inStart, inAnswer)`, leaves
|
|
1437
1960
|
* the connection as it found it: only the frames a statement's answer has,
|
|
1438
|
-
* ending with a ReadyForQuery that is idle.
|
|
1961
|
+
* ending with a ReadyForQuery that is idle. An answer with no end found, or
|
|
1962
|
+
* a slot that is not in the pool, is not one.
|
|
1439
1963
|
*/
|
|
1440
1964
|
const pgAsyncPlain = (pool: PgAsyncPool, s: i32): boolean => {
|
|
1965
|
+
if (
|
|
1966
|
+
s < 0 ||
|
|
1967
|
+
s >= toI32(pool.inputs.length) ||
|
|
1968
|
+
s >= toI32(pool.inAnswer.length) ||
|
|
1969
|
+
s >= toI32(pool.inStart.length)
|
|
1970
|
+
) {
|
|
1971
|
+
return false
|
|
1972
|
+
}
|
|
1441
1973
|
const input: u8[] = pool.inputs[s]
|
|
1974
|
+
const length: i32 = toI32(input.length)
|
|
1442
1975
|
const stop: i32 = pool.inAnswer[s]
|
|
1976
|
+
if (stop < 0 || stop > length) {
|
|
1977
|
+
return false
|
|
1978
|
+
}
|
|
1443
1979
|
let at: i32 = pool.inStart[s]
|
|
1444
|
-
|
|
1980
|
+
// `at < length - 5` and `at <= stop - 5` are `at + 5 < length` and
|
|
1981
|
+
// `at + 5 <= stop`, written so they cannot overflow.
|
|
1982
|
+
while (at >= 0 && at < length - 5 && at <= stop - 5) {
|
|
1445
1983
|
const type: i32 = toI32(input[at])
|
|
1446
|
-
|
|
1447
|
-
//
|
|
1448
|
-
|
|
1449
|
-
|
|
1984
|
+
// The frame's length, read here rather than with `getI32BE`, whose call
|
|
1985
|
+
// would end what the prover knows of `input`.
|
|
1986
|
+
const size: i32 =
|
|
1987
|
+
(toI32(input[at + 1]) << 24) |
|
|
1988
|
+
(toI32(input[at + 2]) << 16) |
|
|
1989
|
+
(toI32(input[at + 3]) << 8) |
|
|
1990
|
+
toI32(input[at + 4])
|
|
1991
|
+
// Not reached for a length under 4: `pgAsyncScan` refused one before this
|
|
1992
|
+
// answer could be read past. Kept so this walk does not rest on that. A
|
|
1993
|
+
// frame past `stop` is not one of this answer's.
|
|
1994
|
+
const left: i32 = stop - at
|
|
1995
|
+
if (size < 4 || size >= left) {
|
|
1450
1996
|
return false
|
|
1451
1997
|
}
|
|
1998
|
+
// In `u32`: the frame ends at or before `stop`, so the sum cannot wrap.
|
|
1999
|
+
const next: i32 = toI32(toU32(at) + toU32(1) + toU32(size))
|
|
1452
2000
|
if (type === 90) {
|
|
1453
|
-
return next === stop &&
|
|
2001
|
+
return next === stop && size === 5 && toI32(input[at + 5]) === PG_ASYNC_IDLE
|
|
1454
2002
|
}
|
|
1455
2003
|
if (
|
|
1456
2004
|
type !== 49 &&
|
|
@@ -1488,20 +2036,35 @@ const pgAsyncSkipAny = async (pool: PgAsyncPool): Promise<boolean> => {
|
|
|
1488
2036
|
|
|
1489
2037
|
/**
|
|
1490
2038
|
* Gives seat `seat` back; the connection closes once a lost one has no seat
|
|
1491
|
-
* left. Wakes the tasks waiting for room.
|
|
2039
|
+
* left. Wakes the tasks waiting for room. A seat that is not in the pool has
|
|
2040
|
+
* nothing to give back.
|
|
1492
2041
|
*/
|
|
1493
2042
|
const pgAsyncFree = (pool: PgAsyncPool, seat: i32): void => {
|
|
1494
2043
|
const s: i32 = toI32(seat / PG_ASYNC_MAX_DEPTH)
|
|
2044
|
+
if (
|
|
2045
|
+
seat < 0 ||
|
|
2046
|
+
s < 0 ||
|
|
2047
|
+
seat >= toI32(pool.seatUsed.length) ||
|
|
2048
|
+
seat >= toI32(pool.seatWaiting.length) ||
|
|
2049
|
+
seat >= toI32(pool.seatAbandoned.length) ||
|
|
2050
|
+
seat >= toI32(pool.seatClaims.length) ||
|
|
2051
|
+
s >= toI32(pool.seated.length) ||
|
|
2052
|
+
s >= toI32(pool.doomAt.length) ||
|
|
2053
|
+
s >= toI32(pool.fds.length)
|
|
2054
|
+
) {
|
|
2055
|
+
return
|
|
2056
|
+
}
|
|
1495
2057
|
pool.seatUsed[seat] = 0
|
|
1496
2058
|
pool.seatWaiting[seat] = 0
|
|
1497
2059
|
pool.seatAbandoned[seat] = 0
|
|
1498
|
-
pool.seatClaims[seat] = (pool.seatClaims[seat]
|
|
1499
|
-
pool.seated[s]
|
|
2060
|
+
pool.seatClaims[seat] = pgAsyncStep(pool.seatClaims[seat], PG_ASYNC_SEAT_MASK)
|
|
2061
|
+
pool.seated[s] = pgAsyncUncount(pool.seated[s])
|
|
1500
2062
|
if (pool.doomAt[s] >= 0 && pool.seated[s] === 0) {
|
|
1501
|
-
|
|
1502
|
-
|
|
2063
|
+
const fd: i32 = pool.fds[s]
|
|
2064
|
+
if (fd >= 0) {
|
|
1503
2065
|
pool.fds[s] = -1
|
|
1504
|
-
pool.dropped
|
|
2066
|
+
pool.dropped = pgAsyncCount(pool.dropped)
|
|
2067
|
+
netClose(fd)
|
|
1505
2068
|
}
|
|
1506
2069
|
pgAsyncForget(pool, s)
|
|
1507
2070
|
}
|
|
@@ -1511,24 +2074,53 @@ const pgAsyncFree = (pool: PgAsyncPool, seat: i32): void => {
|
|
|
1511
2074
|
/**
|
|
1512
2075
|
* Whether the answer in turn on slot `s` lies whole in its input, from
|
|
1513
2076
|
* `inStart`: `inAnswer` is set to where it ends, past its ReadyForQuery, or
|
|
1514
|
-
* to -1 for a frame length no answer has, which `pgAsyncAnswer` fails.
|
|
2077
|
+
* to -1 for a frame length no answer has, which `pgAsyncAnswer` fails. A
|
|
2078
|
+
* slot that is not in the pool, or whose marks are out of order, holds no
|
|
2079
|
+
* whole answer.
|
|
1515
2080
|
*/
|
|
1516
2081
|
const pgAsyncScan = (pool: PgAsyncPool, s: i32): boolean => {
|
|
2082
|
+
if (
|
|
2083
|
+
s < 0 ||
|
|
2084
|
+
s >= toI32(pool.inputs.length) ||
|
|
2085
|
+
s >= toI32(pool.inEnd.length) ||
|
|
2086
|
+
s >= toI32(pool.inStart.length) ||
|
|
2087
|
+
s >= toI32(pool.inParsed.length) ||
|
|
2088
|
+
s >= toI32(pool.inAnswer.length)
|
|
2089
|
+
) {
|
|
2090
|
+
return false
|
|
2091
|
+
}
|
|
1517
2092
|
const input: u8[] = pool.inputs[s]
|
|
2093
|
+
const length: i32 = toI32(input.length)
|
|
1518
2094
|
const end: i32 = pool.inEnd[s]
|
|
2095
|
+
const most: i32 = pool.maxMessage
|
|
2096
|
+
if (end < 0 || end > length) {
|
|
2097
|
+
return false
|
|
2098
|
+
}
|
|
1519
2099
|
let at: i32 = pool.inStart[s]
|
|
1520
2100
|
pool.inParsed[s] = 0
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
2101
|
+
// `at < length - 4` is `at + 5 <= length`, written so it cannot overflow.
|
|
2102
|
+
while (at >= 0 && at < length - 4) {
|
|
2103
|
+
const left: i32 = end - at
|
|
2104
|
+
if (left < 5) {
|
|
2105
|
+
return false
|
|
2106
|
+
}
|
|
2107
|
+
// Read here rather than with `getI32BE`, whose call would end what the
|
|
2108
|
+
// prover knows of `input` and the pool's arrays.
|
|
2109
|
+
const size: i32 =
|
|
2110
|
+
(toI32(input[at + 1]) << 24) |
|
|
2111
|
+
(toI32(input[at + 2]) << 16) |
|
|
2112
|
+
(toI32(input[at + 3]) << 8) |
|
|
2113
|
+
toI32(input[at + 4])
|
|
2114
|
+
if (size < 4 || size > most) {
|
|
1524
2115
|
pool.inAnswer[s] = -1
|
|
1525
2116
|
return true
|
|
1526
2117
|
}
|
|
1527
|
-
if (
|
|
2118
|
+
if (left - 1 < size) {
|
|
1528
2119
|
return false
|
|
1529
2120
|
}
|
|
1530
2121
|
const type: i32 = toI32(input[at])
|
|
1531
|
-
at
|
|
2122
|
+
// In `u32`: the frame ends at or before `end`, so the sum cannot wrap.
|
|
2123
|
+
at = toI32(toU32(at) + toU32(1) + toU32(size))
|
|
1532
2124
|
if (type === 49) {
|
|
1533
2125
|
pool.inParsed[s] = 1
|
|
1534
2126
|
}
|
|
@@ -1563,6 +2155,17 @@ export const pgAsyncTurn = async (pool: PgAsyncPool, ticket: i32, sent: i32): Pr
|
|
|
1563
2155
|
pgAsyncCheckCaps(pool)
|
|
1564
2156
|
const seat: i32 = pgAsyncSeatOf(pool, ticket)
|
|
1565
2157
|
const s: i32 = toI32(seat / PG_ASYNC_MAX_DEPTH)
|
|
2158
|
+
if (
|
|
2159
|
+
seat < 0 ||
|
|
2160
|
+
s < 0 ||
|
|
2161
|
+
seat >= toI32(pool.seatSeq.length) ||
|
|
2162
|
+
seat >= toI32(pool.seatWaiting.length) ||
|
|
2163
|
+
s >= toI32(pool.writing.length)
|
|
2164
|
+
) {
|
|
2165
|
+
// Not reached: `pgAsyncSeatOf` panicked on a ticket of no seat. A seat
|
|
2166
|
+
// out of the pool's arrays has no answer to wait for.
|
|
2167
|
+
return PG_ASYNC_BROKEN
|
|
2168
|
+
}
|
|
1566
2169
|
const seq: i32 = pool.seatSeq[seat]
|
|
1567
2170
|
if (sent === PG_ASYNC_UNSENT || sent === PG_ASYNC_UNSENT_CANCELLED) {
|
|
1568
2171
|
// Nothing went out; `pgAsyncDone` hands its place in the order back
|
|
@@ -1570,15 +2173,17 @@ export const pgAsyncTurn = async (pool: PgAsyncPool, ticket: i32, sent: i32): Pr
|
|
|
1570
2173
|
return sent
|
|
1571
2174
|
}
|
|
1572
2175
|
pool.writing[s] = 0
|
|
2176
|
+
if (sent >= 0) {
|
|
2177
|
+
pool.seatWaiting[seat] = 1
|
|
2178
|
+
}
|
|
1573
2179
|
pgAsyncWakePool(pool)
|
|
1574
2180
|
if (sent < 0) {
|
|
1575
2181
|
pgAsyncDoom(pool, s, seq)
|
|
1576
2182
|
return sent <= PG_ASYNC_BROKEN ? sent : PG_ASYNC_BROKEN
|
|
1577
2183
|
}
|
|
1578
|
-
pool.seatWaiting[seat] = 1
|
|
1579
2184
|
let gaveUp: boolean = false
|
|
1580
2185
|
let yields: i32 = 0
|
|
1581
|
-
while (pool
|
|
2186
|
+
while (!pgAsyncServed(pool, seat) && !pgAsyncLostSeat(pool, seat)) {
|
|
1582
2187
|
if (cancelled()) {
|
|
1583
2188
|
gaveUp = true
|
|
1584
2189
|
break
|
|
@@ -1592,56 +2197,60 @@ export const pgAsyncTurn = async (pool: PgAsyncPool, ticket: i32, sent: i32): Pr
|
|
|
1592
2197
|
yields += 1
|
|
1593
2198
|
await sleep(0)
|
|
1594
2199
|
} else {
|
|
1595
|
-
pool.turnWaits
|
|
1596
|
-
await pgAsyncPark(pool,
|
|
2200
|
+
pool.turnWaits = pgAsyncCount(pool.turnWaits)
|
|
2201
|
+
await pgAsyncPark(pool, pgAsyncTurnLane(seat), -1)
|
|
1597
2202
|
// The last of the waiters the byte woke reads it, as in the claim and
|
|
1598
2203
|
// seat loops: a seat that waits again must not park on a byte nobody reads.
|
|
1599
|
-
pgAsyncDrain(pool,
|
|
2204
|
+
pgAsyncDrain(pool, pgAsyncTurnLane(seat))
|
|
1600
2205
|
}
|
|
1601
2206
|
}
|
|
1602
|
-
|
|
1603
|
-
if (
|
|
1604
|
-
|
|
2207
|
+
// Checked again: the waits above are calls.
|
|
2208
|
+
if (seat < toI32(pool.seatWaiting.length)) {
|
|
2209
|
+
pool.seatWaiting[seat] = 0
|
|
1605
2210
|
}
|
|
2211
|
+
pgAsyncDrain(pool, pgAsyncTurnLane(seat))
|
|
1606
2212
|
if (pgAsyncLostSeat(pool, seat)) {
|
|
1607
|
-
return
|
|
2213
|
+
return pgAsyncBroken(cancelled() ? PG_ASYNC_CANCELLED : 0)
|
|
1608
2214
|
}
|
|
1609
2215
|
if (gaveUp) {
|
|
1610
2216
|
// Its answer is on its way and this task will not read it: the next one does.
|
|
1611
2217
|
pgAsyncAbandon(pool, seat)
|
|
1612
2218
|
return PG_ASYNC_BROKEN + PG_ASYNC_CANCELLED
|
|
1613
2219
|
}
|
|
2220
|
+
if (s >= toI32(pool.fds.length) || s >= toI32(pool.inputs.length)) {
|
|
2221
|
+
// Not reached: the slot's arrays are as long as `seatSeq` is in slots.
|
|
2222
|
+
pgAsyncDoom(pool, s, seq)
|
|
2223
|
+
return PG_ASYNC_BROKEN
|
|
2224
|
+
}
|
|
1614
2225
|
const fd: i32 = pool.fds[s]
|
|
1615
2226
|
const input: u8[] = pool.inputs[s]
|
|
1616
2227
|
while (!pgAsyncScan(pool, s)) {
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
pool.inEnd[s] -= pool.inStart[s]
|
|
1620
|
-
pool.inStart[s] = 0
|
|
1621
|
-
}
|
|
1622
|
-
if (pool.inEnd[s] >= toI32(input.length)) {
|
|
2228
|
+
const end: i32 = pgAsyncRoom(pool, s, input)
|
|
2229
|
+
if (end < 0 || end >= toI32(input.length)) {
|
|
1623
2230
|
// Past the input: `pgAsyncAnswer` reads the rest of it alone.
|
|
1624
|
-
pool.inAnswer
|
|
2231
|
+
if (s < toI32(pool.inAnswer.length)) {
|
|
2232
|
+
pool.inAnswer[s] = -1
|
|
2233
|
+
}
|
|
1625
2234
|
return 0
|
|
1626
2235
|
}
|
|
1627
|
-
const got: i32 = netRead(fd, input,
|
|
2236
|
+
const got: i32 = netRead(fd, input, end, toI32(input.length) - end)
|
|
1628
2237
|
if (got === PG_ASYNC_AGAIN) {
|
|
1629
2238
|
const waited: boolean = await readable(fd)
|
|
1630
2239
|
const failed: i32 = pgAsyncWaitFailure(waited)
|
|
1631
2240
|
if (failed === PG_ASYNC_CANCELLED) {
|
|
1632
2241
|
// The bytes read stay in the input for the task that reads past the rest.
|
|
1633
2242
|
pgAsyncAbandon(pool, seat)
|
|
1634
|
-
return
|
|
2243
|
+
return pgAsyncBroken(failed)
|
|
1635
2244
|
}
|
|
1636
2245
|
if (failed !== 0) {
|
|
1637
2246
|
pgAsyncDoom(pool, s, seq)
|
|
1638
|
-
return
|
|
2247
|
+
return pgAsyncBroken(failed)
|
|
1639
2248
|
}
|
|
1640
2249
|
} else if (got <= 0) {
|
|
1641
2250
|
pgAsyncDoom(pool, s, seq)
|
|
1642
|
-
return
|
|
2251
|
+
return pgAsyncBroken(got)
|
|
1643
2252
|
} else {
|
|
1644
|
-
pool
|
|
2253
|
+
pgAsyncTook(pool, s, end, got, toI32(input.length))
|
|
1645
2254
|
}
|
|
1646
2255
|
}
|
|
1647
2256
|
return 0
|
|
@@ -1657,8 +2266,9 @@ export const pgAsyncTurn = async (pool: PgAsyncPool, ticket: i32, sent: i32): Pr
|
|
|
1657
2266
|
* with `reusable` false. It only reads the pool, so a spawned task may call
|
|
1658
2267
|
* it.
|
|
1659
2268
|
* @param pool The pool the ticket came from; a cap out of its range panics.
|
|
1660
|
-
* @param ticket The ticket `pgAsyncTurn` used; any other panics
|
|
1661
|
-
*
|
|
2269
|
+
* @param ticket The ticket `pgAsyncTurn` used; any other panics. One whose
|
|
2270
|
+
* turn has not come, or whose connection failed at or before it, fails the
|
|
2271
|
+
* answer without reading, with `PG_ERR_CLOSED` and `reusable` false.
|
|
1662
2272
|
* @param turn What `pgAsyncTurn` answered; one below 0 fails the answer
|
|
1663
2273
|
* without reading, with `PG_ERR_ENCODING` for a parameter that could not be
|
|
1664
2274
|
* sent and `PG_ERR_CLOSED` otherwise.
|
|
@@ -1679,20 +2289,39 @@ export const pgAsyncAnswer = async (pool: PgAsyncPool, ticket: i32, turn: i32):
|
|
|
1679
2289
|
}
|
|
1680
2290
|
return pgAsyncLost(result, PG_ERR_CLOSED, turn <= PG_ASYNC_BROKEN ? turn - PG_ASYNC_BROKEN : 0)
|
|
1681
2291
|
}
|
|
1682
|
-
if (
|
|
1683
|
-
|
|
2292
|
+
if (
|
|
2293
|
+
!pgAsyncInTurn(pool, seat) ||
|
|
2294
|
+
s < 0 ||
|
|
2295
|
+
s >= toI32(pool.inputs.length) ||
|
|
2296
|
+
s >= toI32(pool.inStart.length) ||
|
|
2297
|
+
s >= toI32(pool.inEnd.length) ||
|
|
2298
|
+
s >= toI32(pool.inAnswer.length) ||
|
|
2299
|
+
s >= toI32(pool.fds.length)
|
|
2300
|
+
) {
|
|
2301
|
+
// Read before its turn, or after its connection failed: there is no
|
|
2302
|
+
// answer of its own to read.
|
|
2303
|
+
return pgAsyncLost(new PgAsyncResult(), PG_ERR_CLOSED, 0)
|
|
1684
2304
|
}
|
|
1685
2305
|
// Copied into the task's memory: the spawn rules read an array of the pool
|
|
1686
2306
|
// handed across an `await` as one the callee may grow.
|
|
1687
2307
|
const input: u8[] = pool.inputs[s]
|
|
2308
|
+
const fd: i32 = pool.fds[s]
|
|
1688
2309
|
const from: i32 = pool.inStart[s]
|
|
1689
2310
|
const to: i32 = pool.inAnswer[s] >= 0 ? pool.inAnswer[s] : pool.inEnd[s]
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
)
|
|
1693
|
-
|
|
2311
|
+
// In `u32`: `to` is at or past `from` here, so the difference is the bytes.
|
|
2312
|
+
const count: i32 = from >= 0 && from <= to && to <= toI32(input.length) ? toI32(toU32(to) - toU32(from)) : 0
|
|
2313
|
+
if (count < 0 || count > 65535) {
|
|
2314
|
+
// Not reached: an input is `PG_ASYNC_INPUT_SIZE` bytes. `pgAsyncZeroed`
|
|
2315
|
+
// would build a longer one by `push`, which the spawn rules read as
|
|
2316
|
+
// growing the pool, so this answer is failed as one too big to carry.
|
|
2317
|
+
return pgAsyncLost(new PgAsyncResult(), PG_ERR_TOO_BIG, 0)
|
|
2318
|
+
}
|
|
2319
|
+
const carried: u8[] = new Array<u8>(toU16(count))
|
|
2320
|
+
if (!tryCopyBytes(carried, 0, input, from, toI32(carried.length))) {
|
|
2321
|
+
return pgAsyncLost(new PgAsyncResult(), PG_ERR_CLOSED, 0)
|
|
2322
|
+
}
|
|
1694
2323
|
return await pgAsyncRead(
|
|
1695
|
-
|
|
2324
|
+
fd,
|
|
1696
2325
|
carried,
|
|
1697
2326
|
true,
|
|
1698
2327
|
pool.maxMessage,
|
|
@@ -1719,13 +2348,27 @@ export const pgAsyncAnswer = async (pool: PgAsyncPool, ticket: i32, turn: i32):
|
|
|
1719
2348
|
export const pgAsyncDone = (pool: PgAsyncPool, ticket: i32, turn: i32, reusable: boolean): void => {
|
|
1720
2349
|
const seat: i32 = pgAsyncSeatOf(pool, ticket)
|
|
1721
2350
|
const s: i32 = toI32(seat / PG_ASYNC_MAX_DEPTH)
|
|
2351
|
+
if (
|
|
2352
|
+
seat < 0 ||
|
|
2353
|
+
s < 0 ||
|
|
2354
|
+
seat >= toI32(pool.seatSeq.length) ||
|
|
2355
|
+
seat >= toI32(pool.seatAbandoned.length) ||
|
|
2356
|
+
seat >= toI32(pool.seatClaims.length) ||
|
|
2357
|
+
seat >= toI32(pool.seatParse.length) ||
|
|
2358
|
+
s >= toI32(pool.nextSeq.length) ||
|
|
2359
|
+
s >= toI32(pool.writing.length) ||
|
|
2360
|
+
s >= toI32(pool.served.length)
|
|
2361
|
+
) {
|
|
2362
|
+
// Not reached: `pgAsyncSeatOf` panicked on a ticket of no seat. A seat
|
|
2363
|
+
// out of the pool's arrays holds nothing to spend.
|
|
2364
|
+
return
|
|
2365
|
+
}
|
|
1722
2366
|
const seq: i32 = pool.seatSeq[seat]
|
|
1723
|
-
const statement: i32 = s * PG_ASYNC_MAX_STATEMENTS + pool.seatStatement[seat]
|
|
1724
2367
|
const abandoned: i32 = toI32(pool.seatAbandoned[seat])
|
|
1725
2368
|
if (abandoned === 1) {
|
|
1726
2369
|
// Its answer is not read past yet: the ticket is spent, the seat stays until it is.
|
|
1727
2370
|
pool.seatAbandoned[seat] = 2
|
|
1728
|
-
pool.seatClaims[seat] = (pool.seatClaims[seat]
|
|
2371
|
+
pool.seatClaims[seat] = pgAsyncStep(pool.seatClaims[seat], PG_ASYNC_SEAT_MASK)
|
|
1729
2372
|
return
|
|
1730
2373
|
}
|
|
1731
2374
|
if (abandoned === 3) {
|
|
@@ -1736,32 +2379,27 @@ export const pgAsyncDone = (pool: PgAsyncPool, ticket: i32, turn: i32, reusable:
|
|
|
1736
2379
|
// No other seat was taken here while it was being written, so its place
|
|
1737
2380
|
// in the order is handed back.
|
|
1738
2381
|
pool.nextSeq[s] = seq
|
|
2382
|
+
pool.writing[s] = 0
|
|
1739
2383
|
if (toI32(pool.seatParse[seat]) === 1) {
|
|
1740
|
-
pool
|
|
2384
|
+
pgAsyncUnprepare(pool, s, seat)
|
|
1741
2385
|
}
|
|
1742
|
-
pool.writing[s] = 0
|
|
1743
2386
|
pgAsyncFree(pool, seat)
|
|
1744
2387
|
return
|
|
1745
2388
|
}
|
|
1746
2389
|
if (turn >= 0 && pool.served[s] === seq) {
|
|
1747
|
-
pool
|
|
1748
|
-
if (pool.inStart[s] === pool.inEnd[s]) {
|
|
1749
|
-
pool.inStart[s] = 0
|
|
1750
|
-
pool.inEnd[s] = 0
|
|
1751
|
-
}
|
|
1752
|
-
pool.served[s] = (seq + 1) % PG_ASYNC_SEQ_WRAP
|
|
1753
|
-
if (toI32(pool.seatParse[seat]) === 1 && toI32(pool.inParsed[s]) === 0) {
|
|
1754
|
-
pool.prepared[statement] = 0
|
|
1755
|
-
}
|
|
2390
|
+
pgAsyncPast(pool, s, seat, seq)
|
|
1756
2391
|
if (!reusable) {
|
|
1757
2392
|
// A lost frame, a transaction left open or a setting changed: the
|
|
1758
2393
|
// statements behind this one would be read out of step, or run in a
|
|
1759
2394
|
// session they did not ask for.
|
|
1760
|
-
pgAsyncDoom(pool, s,
|
|
1761
|
-
}
|
|
1762
|
-
if (pgAsyncReady(pool, s, seat)) {
|
|
1763
|
-
pgAsyncSignal(pool, s)
|
|
2395
|
+
pgAsyncDoom(pool, s, pgAsyncStep(seq, PG_ASYNC_SEQ_MASK))
|
|
1764
2396
|
}
|
|
2397
|
+
pgAsyncWakeTurns(pool, s, seat)
|
|
2398
|
+
} else if (turn >= 0) {
|
|
2399
|
+
// Its answer was asked for before its turn and never read: nothing reads
|
|
2400
|
+
// past it, so the seats behind it would wait for good. The connection is
|
|
2401
|
+
// lost from here, as for an answer read out of step.
|
|
2402
|
+
pgAsyncDoom(pool, s, seq)
|
|
1765
2403
|
}
|
|
1766
2404
|
pgAsyncFree(pool, seat)
|
|
1767
2405
|
}
|
|
@@ -1777,7 +2415,7 @@ export const pgAsyncDone = (pool: PgAsyncPool, ticket: i32, turn: i32, reusable:
|
|
|
1777
2415
|
* @param sql One statement, `$1`, `$2`, ... naming the parameters; a NUL in
|
|
1778
2416
|
* it fails the query with `PG_ERR_ENCODING` without sending anything.
|
|
1779
2417
|
* @param params The parameters, as many as the statement names; a NUL in
|
|
1780
|
-
* a text one fails
|
|
2418
|
+
* a text one, a Bind past 2^30 bytes, or unequal `values` and `nulls` fails it the same way.
|
|
1781
2419
|
* @returns The answer; its `reusable` goes to `pgAsyncRelease`.
|
|
1782
2420
|
*/
|
|
1783
2421
|
export const pgAsyncQuery = async (
|
|
@@ -1787,8 +2425,14 @@ export const pgAsyncQuery = async (
|
|
|
1787
2425
|
params: PgAsyncParams
|
|
1788
2426
|
): Promise<PgAsyncResult> => {
|
|
1789
2427
|
pgAsyncCheckCaps(pool)
|
|
1790
|
-
const
|
|
2428
|
+
const slot: i32 = pgAsyncSlot(pool, ticket)
|
|
1791
2429
|
const result = new PgAsyncResult()
|
|
2430
|
+
if (slot < 0 || slot >= toI32(pool.fds.length)) {
|
|
2431
|
+
// Not reached: `pgAsyncSlot` panicked on a ticket of no slot. A slot out
|
|
2432
|
+
// of the pool's arrays has no connection to send on.
|
|
2433
|
+
return pgAsyncLost(result, PG_ERR_CLOSED, 0)
|
|
2434
|
+
}
|
|
2435
|
+
const fd: i32 = pool.fds[slot]
|
|
1792
2436
|
if (cancelled()) {
|
|
1793
2437
|
// Nothing is sent, so the connection is still in step and is given back.
|
|
1794
2438
|
result.code = PG_CARRIER_FAILED
|
|
@@ -1806,8 +2450,9 @@ export const pgAsyncQuery = async (
|
|
|
1806
2450
|
return result
|
|
1807
2451
|
}
|
|
1808
2452
|
let sent: i32 = 0
|
|
1809
|
-
while (sent < toI32(out.length)) {
|
|
1810
|
-
|
|
2453
|
+
while (sent >= 0 && sent < toI32(out.length)) {
|
|
2454
|
+
// In `u32`: `sent` is below the `length` here, so the difference is the rest.
|
|
2455
|
+
const n: i32 = netWrite(fd, out, sent, toI32(toU32(out.length) - toU32(sent)))
|
|
1811
2456
|
if (n === PG_ASYNC_AGAIN) {
|
|
1812
2457
|
const waited: boolean = await writable(fd)
|
|
1813
2458
|
const failed: i32 = pgAsyncWaitFailure(waited)
|
|
@@ -1817,7 +2462,9 @@ export const pgAsyncQuery = async (
|
|
|
1817
2462
|
} else if (n <= 0) {
|
|
1818
2463
|
return pgAsyncLost(result, PG_ERR_CLOSED, n)
|
|
1819
2464
|
} else {
|
|
1820
|
-
sent
|
|
2465
|
+
// A write takes no more than it was given, so `sent` stays within `out`.
|
|
2466
|
+
const next: i64 = toI64(sent) + toI64(n)
|
|
2467
|
+
sent = next > toI64(out.length) ? toI32(out.length) : toI32(next)
|
|
1821
2468
|
}
|
|
1822
2469
|
}
|
|
1823
2470
|
const none: u8[] = []
|
|
@@ -1863,119 +2510,142 @@ const pgAsyncLost = (result: PgAsyncResult, failure: i32, socketError: i32): PgA
|
|
|
1863
2510
|
|
|
1864
2511
|
/**
|
|
1865
2512
|
* Parse (when `parse`), Bind, Execute and Sync for the statement `sql` and
|
|
1866
|
-
* `params`, or null for a NUL in a parameter
|
|
2513
|
+
* `params`, or null for a NUL in a parameter, more than 65535 of them, a
|
|
2514
|
+
* value without its null mark or one past them, or SQL or a Bind past 2^30
|
|
2515
|
+
* bytes, more than one message holds. `name` -1 is the unnamed
|
|
1867
2516
|
* statement; 0 or more names it `n<name>`, which the connection keeps.
|
|
1868
2517
|
*/
|
|
1869
2518
|
const pgAsyncEncode = (sql: u8[], name: i32, parse: boolean, params: PgAsyncParams): u8[] | null => {
|
|
1870
|
-
const
|
|
1871
|
-
|
|
2519
|
+
const values: string[] = params.values
|
|
2520
|
+
const nulls: boolean[] = params.nulls
|
|
2521
|
+
const count: i32 = toI32(values.length)
|
|
2522
|
+
// A parameter without its null mark (an array pushed to directly, past the
|
|
2523
|
+
// methods) would send a Bind whose count is not what follows it.
|
|
2524
|
+
if (count > 65535 || count !== toI32(nulls.length)) {
|
|
1872
2525
|
return null
|
|
1873
2526
|
}
|
|
1874
|
-
//
|
|
1875
|
-
const
|
|
1876
|
-
const
|
|
1877
|
-
|
|
2527
|
+
// A message's length is an i32 on the wire; each kept under 2^30 leaves the sums below room.
|
|
2528
|
+
const cap: i32 = 1073741823
|
|
2529
|
+
const sqlLength: i32 = toI32(sql.length)
|
|
2530
|
+
if (sqlLength > cap) {
|
|
2531
|
+
return null
|
|
2532
|
+
}
|
|
2533
|
+
// The statement's name: none, or `n` and its decimal digits (at most 2), as `pgAsyncLabel` writes it.
|
|
2534
|
+
let named: i32 = 0
|
|
2535
|
+
if (name >= 10) {
|
|
2536
|
+
named = 3
|
|
2537
|
+
} else if (name >= 0) {
|
|
2538
|
+
named = 2
|
|
2539
|
+
}
|
|
1878
2540
|
// The length, "" portal, the statement's name and NUL, no parameter formats,
|
|
1879
|
-
// the count, no result formats: 4 + 1 + named + 1 + 2 + 2 + 2
|
|
1880
|
-
//
|
|
2541
|
+
// the count, no result formats: 4 + 1 + named + 1 + 2 + 2 + 2. Each sum is
|
|
2542
|
+
// counted in i64, where no two i32s can wrap it, before it is kept.
|
|
1881
2543
|
let bind: i32 = toI32(12) + named
|
|
1882
|
-
for (let i: i32 = 0; i <
|
|
1883
|
-
const
|
|
1884
|
-
|
|
2544
|
+
for (let i: i32 = 0; i < toI32(values.length) && i < toI32(nulls.length); i++) {
|
|
2545
|
+
const size: i32 = nulls[i] ? toI32(0) : toI32(values[i].length)
|
|
2546
|
+
const next: i64 = toI64(bind) + toI64(4) + toI64(size)
|
|
2547
|
+
if (next > toI64(cap)) {
|
|
1885
2548
|
return null
|
|
1886
2549
|
}
|
|
1887
|
-
|
|
1888
|
-
bind += toI32(4)
|
|
1889
|
-
if (!params.nulls[i]) {
|
|
1890
|
-
bind += toI32(v.length)
|
|
1891
|
-
}
|
|
2550
|
+
bind = toI32(next)
|
|
1892
2551
|
}
|
|
1893
|
-
|
|
1894
|
-
|
|
1895
|
-
const parseLength: i32 = parse ? sqlLength + named + toI32(8) : 0
|
|
1896
|
-
// Parse, Bind, Execute ("" portal, no row limit: 4 + 1 + 4), Sync (4).
|
|
1897
|
-
const total: i32 = (parse ? toI32(1) + parseLength : toI32(0)) + toI32(16) + bind
|
|
1898
|
-
const out: u8[] = new Array<u8>(total)
|
|
1899
|
-
let at: i32 = 0
|
|
2552
|
+
// Each part is pushed in order, so no write needs a position the prover must follow.
|
|
2553
|
+
const out: u8[] = []
|
|
1900
2554
|
if (parse) {
|
|
1901
|
-
|
|
1902
|
-
|
|
1903
|
-
|
|
1904
|
-
|
|
1905
|
-
|
|
1906
|
-
|
|
1907
|
-
|
|
1908
|
-
|
|
1909
|
-
|
|
1910
|
-
|
|
1911
|
-
|
|
1912
|
-
|
|
1913
|
-
|
|
1914
|
-
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
|
|
1918
|
-
|
|
1919
|
-
out
|
|
1920
|
-
|
|
1921
|
-
|
|
1922
|
-
|
|
1923
|
-
|
|
1924
|
-
|
|
1925
|
-
|
|
1926
|
-
out[at + i] = label[i]
|
|
1927
|
-
}
|
|
1928
|
-
at += named
|
|
1929
|
-
out[at] = 0
|
|
1930
|
-
out[at + 1] = 0
|
|
1931
|
-
out[at + 2] = 0
|
|
1932
|
-
setU16BE(out, at + 3, toU16(count))
|
|
1933
|
-
at += 5
|
|
1934
|
-
// Bounded by `values` rather than `count`, which it equals, so the prover reads the index.
|
|
1935
|
-
const encoded: i32 = toI32(values.length)
|
|
1936
|
-
for (let i: i32 = 0; i < encoded; i++) {
|
|
1937
|
-
if (params.nulls[i]) {
|
|
1938
|
-
setI32BE(out, at, -1)
|
|
1939
|
-
at += 4
|
|
2555
|
+
// The length, the statement's name and NUL, the SQL and its NUL, no parameter types.
|
|
2556
|
+
out.push(80)
|
|
2557
|
+
pgAsyncPushI32(out, sqlLength + named + toI32(8))
|
|
2558
|
+
pgAsyncLabel(out, name)
|
|
2559
|
+
out.push(0)
|
|
2560
|
+
for (let i: i32 = 0; i < toI32(sql.length); i++) {
|
|
2561
|
+
out.push(sql[i])
|
|
2562
|
+
}
|
|
2563
|
+
out.push(0)
|
|
2564
|
+
out.push(0)
|
|
2565
|
+
out.push(0)
|
|
2566
|
+
}
|
|
2567
|
+
// The unnamed portal, the statement's name, no parameter format codes (all text), the count.
|
|
2568
|
+
out.push(66)
|
|
2569
|
+
pgAsyncPushI32(out, bind)
|
|
2570
|
+
out.push(0)
|
|
2571
|
+
pgAsyncLabel(out, name)
|
|
2572
|
+
out.push(0)
|
|
2573
|
+
out.push(0)
|
|
2574
|
+
out.push(0)
|
|
2575
|
+
out.push(toU8(count >> 8))
|
|
2576
|
+
out.push(toU8(count & 255))
|
|
2577
|
+
for (let i: i32 = 0; i < toI32(values.length) && i < toI32(nulls.length); i++) {
|
|
2578
|
+
if (nulls[i]) {
|
|
2579
|
+
pgAsyncPushI32(out, -1)
|
|
1940
2580
|
continue
|
|
1941
2581
|
}
|
|
1942
|
-
const v:
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
|
|
2582
|
+
const v: string = values[i]
|
|
2583
|
+
pgAsyncPushI32(out, toI32(v.length))
|
|
2584
|
+
for (let j: i32 = 0; j < toI32(v.length); j++) {
|
|
2585
|
+
const c: i32 = toI32(v.charCodeAt(j))
|
|
2586
|
+
if (c === 0) {
|
|
2587
|
+
return null
|
|
2588
|
+
}
|
|
2589
|
+
out.push(toU8(c))
|
|
2590
|
+
}
|
|
1946
2591
|
}
|
|
1947
2592
|
// No result format codes: every column in text.
|
|
1948
|
-
out
|
|
1949
|
-
out
|
|
1950
|
-
|
|
1951
|
-
out
|
|
1952
|
-
|
|
1953
|
-
out
|
|
1954
|
-
|
|
1955
|
-
|
|
1956
|
-
out
|
|
1957
|
-
setI32BE(out, at + 1, 4)
|
|
2593
|
+
out.push(0)
|
|
2594
|
+
out.push(0)
|
|
2595
|
+
// Execute ("" portal, no row limit) and Sync.
|
|
2596
|
+
out.push(69)
|
|
2597
|
+
pgAsyncPushI32(out, 9)
|
|
2598
|
+
out.push(0)
|
|
2599
|
+
pgAsyncPushI32(out, 0)
|
|
2600
|
+
out.push(83)
|
|
2601
|
+
pgAsyncPushI32(out, 4)
|
|
1958
2602
|
return out
|
|
1959
2603
|
}
|
|
1960
2604
|
|
|
2605
|
+
/** Appends `v`'s four bytes to `out`, most significant first, as the protocol sends an Int32. */
|
|
2606
|
+
const pgAsyncPushI32 = (out: u8[], v: i32): void => {
|
|
2607
|
+
const u: u32 = toU32(v)
|
|
2608
|
+
out.push(toU8(u >> 24))
|
|
2609
|
+
out.push(toU8((u >> 16) & 255))
|
|
2610
|
+
out.push(toU8((u >> 8) & 255))
|
|
2611
|
+
out.push(toU8(u & 255))
|
|
2612
|
+
}
|
|
2613
|
+
|
|
1961
2614
|
/**
|
|
1962
|
-
*
|
|
1963
|
-
* 2), or
|
|
2615
|
+
* Appends a prepared statement's name to `out`: `n` and the decimal digits
|
|
2616
|
+
* of `name` (at most 2), or nothing for a negative `name`.
|
|
1964
2617
|
*/
|
|
1965
|
-
const pgAsyncLabel = (name: i32):
|
|
2618
|
+
const pgAsyncLabel = (out: u8[], name: i32): void => {
|
|
1966
2619
|
if (name >= 10) {
|
|
1967
|
-
|
|
2620
|
+
out.push(110)
|
|
2621
|
+
out.push(toU8(toI32(48) + toI32(name / 10)))
|
|
2622
|
+
out.push(toU8(toI32(48) + (name % 10)))
|
|
2623
|
+
} else if (name >= 0) {
|
|
2624
|
+
out.push(110)
|
|
2625
|
+
out.push(toU8(toI32(48) + name))
|
|
1968
2626
|
}
|
|
1969
|
-
|
|
1970
|
-
|
|
2627
|
+
}
|
|
2628
|
+
|
|
2629
|
+
/**
|
|
2630
|
+
* A zeroed byte array of `n` bytes, or an empty one for a negative `n`. Up to
|
|
2631
|
+
* 64 KiB it is one `new Array` of a `u16` length, which needs no check; a
|
|
2632
|
+
* longer one, the rare buffer for a message past that, is built by `push`.
|
|
2633
|
+
*/
|
|
2634
|
+
const pgAsyncZeroed = (n: i32): u8[] => {
|
|
2635
|
+
if (n >= 0 && n <= 65535) {
|
|
2636
|
+
return new Array<u8>(toU16(n))
|
|
2637
|
+
}
|
|
2638
|
+
const out: u8[] = []
|
|
2639
|
+
while (toI32(out.length) < n) {
|
|
2640
|
+
out.push(0)
|
|
1971
2641
|
}
|
|
1972
|
-
return
|
|
2642
|
+
return out
|
|
1973
2643
|
}
|
|
1974
2644
|
|
|
1975
2645
|
/** `s`'s UTF-8 bytes, or null when one is zero, which a C string in the protocol cannot hold. */
|
|
1976
2646
|
const pgAsyncBytes = (s: string): u8[] | null => {
|
|
1977
2647
|
const n: i32 = toI32(s.length)
|
|
1978
|
-
const b: u8[] =
|
|
2648
|
+
const b: u8[] = pgAsyncZeroed(n)
|
|
1979
2649
|
const room: i32 = toI32(b.length)
|
|
1980
2650
|
// Bounded by each length it indexes, which are equal, so the prover reads both.
|
|
1981
2651
|
for (let i: i32 = 0; i < n && i < room; i++) {
|
|
@@ -2008,24 +2678,65 @@ const pgAsyncRead = async (
|
|
|
2008
2678
|
maxResultBytes: i32
|
|
2009
2679
|
): Promise<PgAsyncResult> => {
|
|
2010
2680
|
const result = new PgAsyncResult()
|
|
2681
|
+
// `pgAsyncCheckCaps` has already held every cap inside its range, so this
|
|
2682
|
+
// never fails; it is what lets the prover bound the sums below by the caps.
|
|
2683
|
+
if (
|
|
2684
|
+
maxMessage < 4 ||
|
|
2685
|
+
maxMessage > PG_ASYNC_MAX_CAP ||
|
|
2686
|
+
maxRows > PG_ASYNC_MAX_ROWS ||
|
|
2687
|
+
maxResultBytes < 0 ||
|
|
2688
|
+
maxResultBytes > PG_ASYNC_MAX_CAP
|
|
2689
|
+
) {
|
|
2690
|
+
return pgAsyncLost(result, PG_ERR_TOO_BIG, 0)
|
|
2691
|
+
}
|
|
2692
|
+
// The seed is part of the pool's input, far shorter than this.
|
|
2693
|
+
if (toI32(seed.length) > PG_ASYNC_MAX_CAP) {
|
|
2694
|
+
return pgAsyncLost(result, PG_ERR_TOO_BIG, 0)
|
|
2695
|
+
}
|
|
2696
|
+
// `PG_ASYNC_MAX_CAP`, one past it, and five short of one past it, as
|
|
2697
|
+
// literals: inside a loop the prover takes a bound from a literal or a local
|
|
2698
|
+
// constant, not from a module constant.
|
|
2699
|
+
const cap: i32 = 1073741823
|
|
2700
|
+
const capEnd: i32 = 1073741824
|
|
2701
|
+
const capHead: i32 = 1073741819
|
|
2011
2702
|
// The buffer starts the size of the seed, not a zero-filled 8 KiB: an answer
|
|
2012
2703
|
// already scanned whole is parsed without clearing memory it never reads, and
|
|
2013
2704
|
// only a read past it grows the buffer.
|
|
2014
|
-
let buf: u8[] =
|
|
2015
|
-
|
|
2705
|
+
let buf: u8[] = pgAsyncZeroed(toI32(seed.length))
|
|
2706
|
+
if (!tryCopyBytes(buf, 0, seed, 0, toI32(seed.length))) {
|
|
2707
|
+
return pgAsyncLost(result, PG_ERR_FRAME, 0)
|
|
2708
|
+
}
|
|
2016
2709
|
let start: i32 = 0
|
|
2017
2710
|
let end: i32 = toI32(seed.length)
|
|
2018
2711
|
let refused: boolean = false
|
|
2019
2712
|
let tooBig: boolean = false
|
|
2020
2713
|
let changed: boolean = false
|
|
2021
2714
|
let shaped: boolean = false
|
|
2022
|
-
//
|
|
2023
|
-
let
|
|
2715
|
+
// What is left of `maxResultBytes` for the rows, tags and error fields kept.
|
|
2716
|
+
let left: i32 = maxResultBytes
|
|
2024
2717
|
while (true) {
|
|
2025
|
-
//
|
|
2026
|
-
|
|
2027
|
-
|
|
2028
|
-
|
|
2718
|
+
// Always false: `start` never passes `end`, `end` never passes the
|
|
2719
|
+
// buffer, the buffer is never longer than the seed or `maxMessage + 1`, and
|
|
2720
|
+
// `left` only shrinks from `maxResultBytes`. Each bound is stated on its
|
|
2721
|
+
// own because the prover reads ranges, not the chain between them.
|
|
2722
|
+
if (
|
|
2723
|
+
start < 0 ||
|
|
2724
|
+
end < 0 ||
|
|
2725
|
+
start > end ||
|
|
2726
|
+
start > capEnd ||
|
|
2727
|
+
end > capEnd ||
|
|
2728
|
+
end > toI32(buf.length) ||
|
|
2729
|
+
left < 0 ||
|
|
2730
|
+
left > cap
|
|
2731
|
+
) {
|
|
2732
|
+
return pgAsyncLost(result, PG_ERR_FRAME, 0)
|
|
2733
|
+
}
|
|
2734
|
+
// A whole message at `start` is taken here; anything short of one reads more below.
|
|
2735
|
+
const have: i32 = end - start
|
|
2736
|
+
if (have >= 5) {
|
|
2737
|
+
// Always false, since five bytes lie between `start` and `end`; it
|
|
2738
|
+
// proves the header's reads and, with `length`, the frame's end.
|
|
2739
|
+
if (start > toI32(buf.length) - 5 || start > capHead) {
|
|
2029
2740
|
return pgAsyncLost(result, PG_ERR_FRAME, 0)
|
|
2030
2741
|
}
|
|
2031
2742
|
const type: i32 = toI32(buf[start])
|
|
@@ -2033,119 +2744,142 @@ const pgAsyncRead = async (
|
|
|
2033
2744
|
if (length < 4) {
|
|
2034
2745
|
return pgAsyncLost(result, PG_ERR_FRAME, 0)
|
|
2035
2746
|
}
|
|
2036
|
-
|
|
2747
|
+
// `maxMessage` is never past `PG_ASYNC_MAX_CAP`; the second test bounds the sums below.
|
|
2748
|
+
if (length > maxMessage || length > cap) {
|
|
2037
2749
|
return pgAsyncLost(result, PG_ERR_TOO_BIG, 0)
|
|
2038
2750
|
}
|
|
2039
|
-
if (
|
|
2040
|
-
|
|
2041
|
-
|
|
2042
|
-
|
|
2043
|
-
|
|
2044
|
-
|
|
2045
|
-
|
|
2046
|
-
|
|
2047
|
-
|
|
2048
|
-
|
|
2049
|
-
|
|
2050
|
-
|
|
2051
|
-
|
|
2052
|
-
|
|
2053
|
-
|
|
2054
|
-
|
|
2055
|
-
|
|
2056
|
-
|
|
2057
|
-
|
|
2058
|
-
|
|
2059
|
-
|
|
2060
|
-
}
|
|
2061
|
-
if (type === 68) {
|
|
2062
|
-
// 'D': DataRow, a count then each column's length (-1 for NULL) and bytes.
|
|
2063
|
-
if (stop - body < 2) {
|
|
2064
|
-
return pgAsyncLost(result, PG_ERR_FRAME, 0)
|
|
2751
|
+
if (have - 1 >= length) {
|
|
2752
|
+
const body: i32 = start + 5
|
|
2753
|
+
const stop: i32 = start + 1 + length
|
|
2754
|
+
// The body's length, `stop - body`, which the prover bounds by `maxMessage`.
|
|
2755
|
+
const size: i32 = length - 4
|
|
2756
|
+
start = stop
|
|
2757
|
+
if (type === 90) {
|
|
2758
|
+
// 'Z': ReadyForQuery, the end of the answer.
|
|
2759
|
+
if (size !== 1 || body < 0 || body >= toI32(buf.length)) {
|
|
2760
|
+
return pgAsyncLost(result, PG_ERR_FRAME, 0)
|
|
2761
|
+
}
|
|
2762
|
+
// Bytes after the ReadyForQuery answer no statement this one sent,
|
|
2763
|
+
// so the connection is out of step and closed rather than given
|
|
2764
|
+
// back. A pipelined read stops at the ReadyForQuery, so none are
|
|
2765
|
+
// read here unless the server sent them inside this answer.
|
|
2766
|
+
result.reusable = toI32(buf[body]) === PG_ASYNC_IDLE && !changed && start === end
|
|
2767
|
+
if (refused || tooBig) {
|
|
2768
|
+
result.code = PG_CARRIER_FAILED
|
|
2769
|
+
result.failure = tooBig ? PG_ERR_TOO_BIG : PG_ERR_NONE
|
|
2770
|
+
}
|
|
2771
|
+
return result
|
|
2065
2772
|
}
|
|
2066
|
-
|
|
2067
|
-
|
|
2068
|
-
|
|
2069
|
-
|
|
2070
|
-
|
|
2071
|
-
|
|
2072
|
-
|
|
2073
|
-
|
|
2074
|
-
|
|
2075
|
-
|
|
2076
|
-
|
|
2773
|
+
if (type === 68) {
|
|
2774
|
+
// 'D': DataRow, a count then each column's length (-1 for NULL) and bytes.
|
|
2775
|
+
if (size < 2) {
|
|
2776
|
+
return pgAsyncLost(result, PG_ERR_FRAME, 0)
|
|
2777
|
+
}
|
|
2778
|
+
const columns: i32 = toI32(getU16BE(buf, body))
|
|
2779
|
+
if (columns > maxColumns) {
|
|
2780
|
+
return pgAsyncLost(result, PG_ERR_TOO_BIG, 0)
|
|
2781
|
+
}
|
|
2782
|
+
if (shaped && columns !== result.columns) {
|
|
2783
|
+
return pgAsyncLost(result, PG_ERR_FRAME, 0)
|
|
2784
|
+
}
|
|
2785
|
+
// The frame's length bounds its cells' bytes; each cell costs its string besides.
|
|
2786
|
+
const cost: i32 = size + columns * PG_ASYNC_CELL_COST
|
|
2787
|
+
const rows: i32 = result.rows
|
|
2788
|
+
if (tooBig || rows < 0 || rows >= maxRows || cost < 0 || cost > left) {
|
|
2789
|
+
tooBig = true
|
|
2790
|
+
continue
|
|
2791
|
+
}
|
|
2792
|
+
left -= cost
|
|
2793
|
+
if (!pgAsyncRow(buf, body + 2, stop, columns, result)) {
|
|
2794
|
+
return pgAsyncLost(result, PG_ERR_FRAME, 0)
|
|
2795
|
+
}
|
|
2796
|
+
result.columns = columns
|
|
2797
|
+
shaped = true
|
|
2798
|
+
result.rows = rows + 1
|
|
2077
2799
|
continue
|
|
2078
2800
|
}
|
|
2079
|
-
|
|
2080
|
-
|
|
2081
|
-
|
|
2082
|
-
|
|
2083
|
-
|
|
2084
|
-
|
|
2085
|
-
|
|
2086
|
-
|
|
2087
|
-
|
|
2088
|
-
|
|
2089
|
-
|
|
2090
|
-
|
|
2091
|
-
|
|
2801
|
+
if (type === 67) {
|
|
2802
|
+
// 'C': CommandComplete, a NUL-ended tag.
|
|
2803
|
+
if (size < 1 || stop > toI32(buf.length) || toI32(buf[stop - 1]) !== 0) {
|
|
2804
|
+
return pgAsyncLost(result, PG_ERR_FRAME, 0)
|
|
2805
|
+
}
|
|
2806
|
+
// A tag is kept, so it costs the budget too; past it, the answer fails.
|
|
2807
|
+
if (size > left) {
|
|
2808
|
+
tooBig = true
|
|
2809
|
+
continue
|
|
2810
|
+
}
|
|
2811
|
+
left -= size
|
|
2812
|
+
result.tag = String.fromBytes(buf, body, stop - 1)
|
|
2813
|
+
continue
|
|
2092
2814
|
}
|
|
2093
|
-
|
|
2094
|
-
|
|
2095
|
-
|
|
2815
|
+
if (type === 69) {
|
|
2816
|
+
// 'E': ErrorResponse, fields of a code byte and a NUL-ended value.
|
|
2817
|
+
// Its kept fields cost the budget, bounded by the frame; past it, none is kept.
|
|
2818
|
+
refused = true
|
|
2819
|
+
if (size > left) {
|
|
2820
|
+
tooBig = true
|
|
2821
|
+
continue
|
|
2822
|
+
}
|
|
2823
|
+
left -= size
|
|
2824
|
+
if (!pgAsyncError(buf, body, stop, result)) {
|
|
2825
|
+
return pgAsyncLost(result, PG_ERR_FRAME, 0)
|
|
2826
|
+
}
|
|
2096
2827
|
continue
|
|
2097
2828
|
}
|
|
2098
|
-
|
|
2099
|
-
|
|
2100
|
-
|
|
2101
|
-
}
|
|
2102
|
-
if (type === 69) {
|
|
2103
|
-
// 'E': ErrorResponse, fields of a code byte and a NUL-ended value.
|
|
2104
|
-
// Its kept fields cost the budget, bounded by the frame; past it, none is kept.
|
|
2105
|
-
refused = true
|
|
2106
|
-
if (stop - body > maxResultBytes - kept) {
|
|
2107
|
-
tooBig = true
|
|
2829
|
+
if (type === 83) {
|
|
2830
|
+
// 'S': a ParameterStatus after startup is a session setting a statement changed.
|
|
2831
|
+
changed = true
|
|
2108
2832
|
continue
|
|
2109
2833
|
}
|
|
2110
|
-
|
|
2111
|
-
|
|
2112
|
-
|
|
2834
|
+
if (type === 49 || type === 50 || type === 73 || type === 78 || type === 65) {
|
|
2835
|
+
// ParseComplete, BindComplete, EmptyQueryResponse, a notice, a notification.
|
|
2836
|
+
continue
|
|
2113
2837
|
}
|
|
2114
|
-
|
|
2838
|
+
return pgAsyncLost(result, PG_ERR_UNEXPECTED, 0)
|
|
2115
2839
|
}
|
|
2116
|
-
if (type === 83) {
|
|
2117
|
-
// 'S': a ParameterStatus after startup is a session setting a statement changed.
|
|
2118
|
-
changed = true
|
|
2119
|
-
continue
|
|
2120
|
-
}
|
|
2121
|
-
if (type === 49 || type === 50 || type === 73 || type === 78 || type === 65) {
|
|
2122
|
-
// ParseComplete, BindComplete, EmptyQueryResponse, a notice, a notification.
|
|
2123
|
-
continue
|
|
2124
|
-
}
|
|
2125
|
-
return pgAsyncLost(result, PG_ERR_UNEXPECTED, 0)
|
|
2126
2840
|
}
|
|
2127
2841
|
// Room for the rest of the message at `start`, then more input.
|
|
2128
2842
|
if (start > 0) {
|
|
2129
|
-
|
|
2843
|
+
if (!tryCopyBytes(buf, 0, buf, start, end - start)) {
|
|
2844
|
+
return pgAsyncLost(result, PG_ERR_FRAME, 0)
|
|
2845
|
+
}
|
|
2130
2846
|
end -= start
|
|
2131
2847
|
start = 0
|
|
2132
2848
|
}
|
|
2133
2849
|
if (end === toI32(buf.length)) {
|
|
2134
|
-
|
|
2135
|
-
|
|
2136
|
-
|
|
2137
|
-
|
|
2138
|
-
|
|
2850
|
+
// A full buffer holds less than one whole message, so it is shorter
|
|
2851
|
+
// than `maxMessage + 1`; the test lets the prover read that.
|
|
2852
|
+
if (end < 0 || end > maxMessage || end > cap || maxMessage > cap) {
|
|
2853
|
+
return pgAsyncLost(result, PG_ERR_TOO_BIG, 0)
|
|
2854
|
+
}
|
|
2855
|
+
const twice: i32 = end < PG_ASYNC_INPUT_SIZE ? PG_ASYNC_INPUT_SIZE : end * 2
|
|
2856
|
+
const grown: u8[] = pgAsyncZeroed(twice > maxMessage + 1 ? maxMessage + 1 : twice)
|
|
2857
|
+
if (!tryCopyBytes(grown, 0, buf, 0, end)) {
|
|
2858
|
+
return pgAsyncLost(result, PG_ERR_FRAME, 0)
|
|
2859
|
+
}
|
|
2139
2860
|
buf = grown
|
|
2140
2861
|
}
|
|
2862
|
+
// Always false, as above; it proves the read's range below.
|
|
2863
|
+
if (start < 0 || end < 0 || start > end || start > capEnd || end > capEnd || end > toI32(buf.length)) {
|
|
2864
|
+
return pgAsyncLost(result, PG_ERR_FRAME, 0)
|
|
2865
|
+
}
|
|
2141
2866
|
const room: i32 = toI32(buf.length) - end
|
|
2142
2867
|
let want: i32 = room
|
|
2143
2868
|
if (bounded) {
|
|
2144
2869
|
// The rest of the frame at `start`, and the next header unless it is the ReadyForQuery.
|
|
2145
|
-
|
|
2146
|
-
|
|
2870
|
+
const held: i32 = end - start
|
|
2871
|
+
want = toI32(5) - held
|
|
2872
|
+
if (held >= 5 && start < toI32(buf.length) - 5) {
|
|
2147
2873
|
const length: i32 = getI32BE(buf, start + 1)
|
|
2148
|
-
|
|
2874
|
+
// A length past `maxMessage` was failed before this read.
|
|
2875
|
+
if (length < 4) {
|
|
2876
|
+
return pgAsyncLost(result, PG_ERR_FRAME, 0)
|
|
2877
|
+
}
|
|
2878
|
+
if (length > maxMessage || length > cap) {
|
|
2879
|
+
return pgAsyncLost(result, PG_ERR_FRAME, 0)
|
|
2880
|
+
}
|
|
2881
|
+
const rest: i32 = toI32(1) + length - held
|
|
2882
|
+
want = toI32(buf[start]) === 90 ? rest : rest + 5
|
|
2149
2883
|
}
|
|
2150
2884
|
if (want > room) {
|
|
2151
2885
|
want = room
|
|
@@ -2163,32 +2897,51 @@ const pgAsyncRead = async (
|
|
|
2163
2897
|
} else if (got <= 0) {
|
|
2164
2898
|
return pgAsyncLost(result, PG_ERR_CLOSED, got)
|
|
2165
2899
|
} else {
|
|
2166
|
-
|
|
2900
|
+
// A read never passes the room it was given, so this never fails; the
|
|
2901
|
+
// sum is taken in `i64` because the prover cannot chain `got <= want <= room`.
|
|
2902
|
+
const next: i64 = toI64(end) + toI64(got)
|
|
2903
|
+
if (next > toI64(buf.length)) {
|
|
2904
|
+
return pgAsyncLost(result, PG_ERR_FRAME, 0)
|
|
2905
|
+
}
|
|
2906
|
+
end = toI32(next)
|
|
2167
2907
|
}
|
|
2168
2908
|
}
|
|
2169
2909
|
}
|
|
2170
2910
|
|
|
2171
2911
|
/** One DataRow's `columns` cells in `[at, stop)` into `result`; false for a frame that does not add up. */
|
|
2172
2912
|
const pgAsyncRow = (buf: u8[], at: i32, stop: i32, columns: i32, result: PgAsyncResult): boolean => {
|
|
2913
|
+
// `stop` never passes the buffer; the test lets the prover read each cell's range.
|
|
2914
|
+
if (at < 0 || at > stop || stop > toI32(buf.length)) {
|
|
2915
|
+
return false
|
|
2916
|
+
}
|
|
2917
|
+
// `PG_ASYNC_MAX_CAP`, and the last place a cell's length can start in a
|
|
2918
|
+
// frame that ends by one past it, as literals, which the prover reads inside
|
|
2919
|
+
// a loop where it does not read a module constant.
|
|
2920
|
+
const cap: i32 = 1073741823
|
|
2921
|
+
const lastCell: i32 = 1073741820
|
|
2173
2922
|
let p: i32 = at
|
|
2174
2923
|
for (let c: i32 = 0; c < columns; c++) {
|
|
2175
|
-
if (
|
|
2924
|
+
if (p < 0 || p > stop - 4 || p > toI32(buf.length) - 4 || p > lastCell) {
|
|
2176
2925
|
return false
|
|
2177
2926
|
}
|
|
2178
2927
|
const n: i32 =
|
|
2179
2928
|
(toI32(buf[p]) << 24) | (toI32(buf[p + 1]) << 16) | (toI32(buf[p + 2]) << 8) | toI32(buf[p + 3])
|
|
2180
|
-
p
|
|
2929
|
+
const from: i32 = p + 4
|
|
2181
2930
|
if (n === -1) {
|
|
2182
2931
|
result.values.push("")
|
|
2183
2932
|
result.nulls.push(true)
|
|
2933
|
+
p = from
|
|
2184
2934
|
continue
|
|
2185
2935
|
}
|
|
2186
|
-
|
|
2936
|
+
// A cell never passes the frame, which is never past `PG_ASYNC_MAX_CAP`;
|
|
2937
|
+
// the second test is what lets the prover bound the sum below.
|
|
2938
|
+
if (n < 0 || n > stop - from || n > cap) {
|
|
2187
2939
|
return false
|
|
2188
2940
|
}
|
|
2189
|
-
|
|
2941
|
+
const cellEnd: i32 = from + n
|
|
2942
|
+
result.values.push(String.fromBytes(buf, from, cellEnd))
|
|
2190
2943
|
result.nulls.push(false)
|
|
2191
|
-
p
|
|
2944
|
+
p = cellEnd
|
|
2192
2945
|
}
|
|
2193
2946
|
return p === stop
|
|
2194
2947
|
}
|