@noldova/context.data.sql.postgresql 0.0.0-stage → 0.0.7
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/LICENSE +21 -0
- package/README.md +102 -2
- package/THIRD-PARTY-NOTICES.txt +33 -0
- package/api/index.d.ts +94 -0
- package/api/index.js +12 -0
- package/api/index.js.map +1 -0
- package/enums/backend-message.js +35 -0
- package/enums/backend-message.js.map +1 -0
- package/enums/channel-binding.js +14 -0
- package/enums/channel-binding.js.map +1 -0
- package/enums/cursor-action.js +13 -0
- package/enums/cursor-action.js.map +1 -0
- package/enums/exchange-kind.js +17 -0
- package/enums/exchange-kind.js.map +1 -0
- package/enums/frontend-message.js +24 -0
- package/enums/frontend-message.js.map +1 -0
- package/enums/lease-kind.js +13 -0
- package/enums/lease-kind.js.map +1 -0
- package/enums/message-target.js +13 -0
- package/enums/message-target.js.map +1 -0
- package/enums/prepared-state.js +14 -0
- package/enums/prepared-state.js.map +1 -0
- package/enums/row-mode.js +14 -0
- package/enums/row-mode.js.map +1 -0
- package/enums/ssl-mode.js +16 -0
- package/enums/ssl-mode.js.map +1 -0
- package/enums/token-kind.js +15 -0
- package/enums/token-kind.js.map +1 -0
- package/enums/transaction-status.js +14 -0
- package/enums/transaction-status.js.map +1 -0
- package/enums/write-outcome.js +14 -0
- package/enums/write-outcome.js.map +1 -0
- package/exceptions/batch-skipped.exception.js +19 -0
- package/exceptions/batch-skipped.exception.js.map +1 -0
- package/exceptions/batch.exception.js +19 -0
- package/exceptions/batch.exception.js.map +1 -0
- package/exceptions/cancel-unanswered.exception.js +20 -0
- package/exceptions/cancel-unanswered.exception.js.map +1 -0
- package/exceptions/connection-closed.exception.js +23 -0
- package/exceptions/connection-closed.exception.js.map +1 -0
- package/exceptions/connection-ended.exception.js +20 -0
- package/exceptions/connection-ended.exception.js.map +1 -0
- package/exceptions/postgresql.exception.js +32 -0
- package/exceptions/postgresql.exception.js.map +1 -0
- package/exceptions/query-canceled.exception.js +16 -0
- package/exceptions/query-canceled.exception.js.map +1 -0
- package/exceptions/query-timeout.exception.js +19 -0
- package/exceptions/query-timeout.exception.js.map +1 -0
- package/exceptions/queue-timeout.exception.js +18 -0
- package/exceptions/queue-timeout.exception.js.map +1 -0
- package/exceptions/transaction-rolled-back.exception.js +15 -0
- package/exceptions/transaction-rolled-back.exception.js.map +1 -0
- package/exceptions/type-not-found.exception.js +20 -0
- package/exceptions/type-not-found.exception.js.map +1 -0
- package/exceptions/unsafe-transaction.exception.js +15 -0
- package/exceptions/unsafe-transaction.exception.js.map +1 -0
- package/extensions/db-context-options-builder.extensions.js +17 -0
- package/extensions/db-context-options-builder.extensions.js.map +1 -0
- package/models/backend-key.js +16 -0
- package/models/backend-key.js.map +1 -0
- package/models/binary-columns.js +16 -0
- package/models/binary-columns.js.map +1 -0
- package/models/built-query.js +18 -0
- package/models/built-query.js.map +1 -0
- package/models/channel-listener.js +16 -0
- package/models/channel-listener.js.map +1 -0
- package/models/client/client-listener.js +158 -0
- package/models/client/client-listener.js.map +1 -0
- package/models/client/client-query.js +54 -0
- package/models/client/client-query.js.map +1 -0
- package/models/client/client-timers.js +43 -0
- package/models/client/client-timers.js.map +1 -0
- package/models/client/connection-socket.js +13 -0
- package/models/client/connection-socket.js.map +1 -0
- package/models/client/copy-both-stream.js +33 -0
- package/models/client/copy-both-stream.js.map +1 -0
- package/models/client/copy-in-stream.js +30 -0
- package/models/client/copy-in-stream.js.map +1 -0
- package/models/client/copy-out-stream.js +28 -0
- package/models/client/copy-out-stream.js.map +1 -0
- package/models/client/deferred.js +25 -0
- package/models/client/deferred.js.map +1 -0
- package/models/client/exchange.js +100 -0
- package/models/client/exchange.js.map +1 -0
- package/models/client/listen-subscription.js +26 -0
- package/models/client/listen-subscription.js.map +1 -0
- package/models/client/pool-job.js +24 -0
- package/models/client/pool-job.js.map +1 -0
- package/models/client/pool-lease.js +88 -0
- package/models/client/pool-lease.js.map +1 -0
- package/models/client/pool-queue.js +56 -0
- package/models/client/pool-queue.js.map +1 -0
- package/models/client/postgresql-client.js +83 -0
- package/models/client/postgresql-client.js.map +1 -0
- package/models/client/postgresql-connection.js +1557 -0
- package/models/client/postgresql-connection.js.map +1 -0
- package/models/client/postgresql-pool.js +896 -0
- package/models/client/postgresql-pool.js.map +1 -0
- package/models/client/query.js +216 -0
- package/models/client/query.js.map +1 -0
- package/models/client/reserved-client.js +29 -0
- package/models/client/reserved-client.js.map +1 -0
- package/models/client/statement-cache.js +111 -0
- package/models/client/statement-cache.js.map +1 -0
- package/models/client/transaction-scope.js +136 -0
- package/models/client/transaction-scope.js.map +1 -0
- package/models/client-configuration.js +16 -0
- package/models/client-configuration.js.map +1 -0
- package/models/client-options.js +69 -0
- package/models/client-options.js.map +1 -0
- package/models/column.js +30 -0
- package/models/column.js.map +1 -0
- package/models/command-result.js +16 -0
- package/models/command-result.js.map +1 -0
- package/models/connection-options.js +47 -0
- package/models/connection-options.js.map +1 -0
- package/models/date-parts.js +28 -0
- package/models/date-parts.js.map +1 -0
- package/models/der-element.js +18 -0
- package/models/der-element.js.map +1 -0
- package/models/listen-channel.js +17 -0
- package/models/listen-channel.js.map +1 -0
- package/models/notification.js +18 -0
- package/models/notification.js.map +1 -0
- package/models/parsed-url.js +22 -0
- package/models/parsed-url.js.map +1 -0
- package/models/pool-stats.js +22 -0
- package/models/pool-stats.js.map +1 -0
- package/models/postgresql-connection.js +21 -0
- package/models/postgresql-connection.js.map +1 -0
- package/models/postgresql-session.js +59 -0
- package/models/postgresql-session.js.map +1 -0
- package/models/postgresql-socket.js +129 -0
- package/models/postgresql-socket.js.map +1 -0
- package/models/prepared-statement.js +25 -0
- package/models/prepared-statement.js.map +1 -0
- package/models/query-batch.js +25 -0
- package/models/query-batch.js.map +1 -0
- package/models/query-cost.js +15 -0
- package/models/query-cost.js.map +1 -0
- package/models/query-options.js +16 -0
- package/models/query-options.js.map +1 -0
- package/models/query-statement.js +23 -0
- package/models/query-statement.js.map +1 -0
- package/models/query-wire.js +16 -0
- package/models/query-wire.js.map +1 -0
- package/models/result.js +32 -0
- package/models/result.js.map +1 -0
- package/models/row-column.js +16 -0
- package/models/row-column.js.map +1 -0
- package/models/started-session.js +31 -0
- package/models/started-session.js.map +1 -0
- package/models/statement-description.js +20 -0
- package/models/statement-description.js.map +1 -0
- package/models/statement-shape.js +16 -0
- package/models/statement-shape.js.map +1 -0
- package/models/system-certificates.js +17 -0
- package/models/system-certificates.js.map +1 -0
- package/models/transaction-state.js +18 -0
- package/models/transaction-state.js.map +1 -0
- package/models/transform-options.js +28 -0
- package/models/transform-options.js.map +1 -0
- package/models/type-definition.js +23 -0
- package/models/type-definition.js.map +1 -0
- package/models/type-registry.js +287 -0
- package/models/type-registry.js.map +1 -0
- package/models/typed-parameter.js +16 -0
- package/models/typed-parameter.js.map +1 -0
- package/package.json +26 -4
- package/resources.js +278 -0
- package/resources.js.map +1 -0
- package/services/authenticator.js +145 -0
- package/services/authenticator.js.map +1 -0
- package/services/certificate-hash.js +81 -0
- package/services/certificate-hash.js.map +1 -0
- package/services/client/connection-events.js +10 -0
- package/services/client/connection-events.js.map +1 -0
- package/services/client/connection-lease.js +10 -0
- package/services/client/connection-lease.js.map +1 -0
- package/services/client/copy-host.js +10 -0
- package/services/client/copy-host.js.map +1 -0
- package/services/client/cursor-iterator.js +46 -0
- package/services/client/cursor-iterator.js.map +1 -0
- package/services/client/file-query-handler.js +32 -0
- package/services/client/file-query-handler.js.map +1 -0
- package/services/client/query-handler.js +10 -0
- package/services/client/query-handler.js.map +1 -0
- package/services/client/row.reader.js +87 -0
- package/services/client/row.reader.js.map +1 -0
- package/services/client/sql-runner.js +64 -0
- package/services/client/sql-runner.js.map +1 -0
- package/services/client/type-loader-events.js +61 -0
- package/services/client/type-loader-events.js.map +1 -0
- package/services/client-options.parser.js +187 -0
- package/services/client-options.parser.js.map +1 -0
- package/services/location.parser.js +25 -0
- package/services/location.parser.js.map +1 -0
- package/services/parameter-rewriter.js +158 -0
- package/services/parameter-rewriter.js.map +1 -0
- package/services/postgresql-dialect.js +24 -0
- package/services/postgresql-dialect.js.map +1 -0
- package/services/postgresql-provider.js +8 -0
- package/services/postgresql-provider.js.map +1 -0
- package/services/protocol/backend.js +135 -0
- package/services/protocol/backend.js.map +1 -0
- package/services/protocol/frame.reader.js +167 -0
- package/services/protocol/frame.reader.js.map +1 -0
- package/services/protocol/frontend.js +177 -0
- package/services/protocol/frontend.js.map +1 -0
- package/services/protocol/protocol-writer.js +118 -0
- package/services/protocol/protocol-writer.js.map +1 -0
- package/services/session-starter.js +166 -0
- package/services/session-starter.js.map +1 -0
- package/services/tls-options.reader.js +60 -0
- package/services/tls-options.reader.js.map +1 -0
- package/services/types/array-serializer.js +35 -0
- package/services/types/array-serializer.js.map +1 -0
- package/services/types/array.parser.js +64 -0
- package/services/types/array.parser.js.map +1 -0
- package/services/types/binary-readers.js +135 -0
- package/services/types/binary-readers.js.map +1 -0
- package/services/types/date.parser.js +86 -0
- package/services/types/date.parser.js.map +1 -0
- package/services/value-converter.js +37 -0
- package/services/value-converter.js.map +1 -0
- package/types/binary-decoder.js +9 -0
- package/types/binary-decoder.js.map +1 -0
- package/types/client-settings.js +9 -0
- package/types/client-settings.js.map +1 -0
- package/types/host-target.js +9 -0
- package/types/host-target.js.map +1 -0
- package/types/option-values.js +9 -0
- package/types/option-values.js.map +1 -0
- package/types/query-outcome.js +9 -0
- package/types/query-outcome.js.map +1 -0
- package/types/query-settings.js +9 -0
- package/types/query-settings.js.map +1 -0
- package/types/transaction-result.js +9 -0
- package/types/transaction-result.js.map +1 -0
- package/types/transform-settings.js +9 -0
- package/types/transform-settings.js.map +1 -0
- package/types/value-parser.js +9 -0
- package/types/value-parser.js.map +1 -0
- package/types/value-serializer.js +9 -0
- package/types/value-serializer.js.map +1 -0
|
@@ -0,0 +1,896 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) Noldova.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
import "@noldova/context.core";
|
|
9
|
+
import { DataException } from "@noldova/context.data";
|
|
10
|
+
import { ExceptionOptions } from "@noldova/context.exceptions";
|
|
11
|
+
import { TransactionStatus } from "../../enums/transaction-status.js";
|
|
12
|
+
import { BatchException } from "../../exceptions/batch.exception.js";
|
|
13
|
+
import { ConnectionClosedException } from "../../exceptions/connection-closed.exception.js";
|
|
14
|
+
import { ConnectionEndedException } from "../../exceptions/connection-ended.exception.js";
|
|
15
|
+
import { PostgreSQLException } from "../../exceptions/postgresql.exception.js";
|
|
16
|
+
import { QueryCanceledException } from "../../exceptions/query-canceled.exception.js";
|
|
17
|
+
import { QueueTimeoutException } from "../../exceptions/queue-timeout.exception.js";
|
|
18
|
+
import { TypeNotFoundException } from "../../exceptions/type-not-found.exception.js";
|
|
19
|
+
import { BuiltQuery } from "../built-query.js";
|
|
20
|
+
import { PoolStats } from "../pool-stats.js";
|
|
21
|
+
import { QueryOptions } from "../query-options.js";
|
|
22
|
+
import { QueryWire } from "../query-wire.js";
|
|
23
|
+
import { Result } from "../result.js";
|
|
24
|
+
import { StatementDescription } from "../statement-description.js";
|
|
25
|
+
import { Resources } from "../../resources.js";
|
|
26
|
+
import { ConnectionEvents } from "../../services/client/connection-events.js";
|
|
27
|
+
import { PoolJob } from "./pool-job.js";
|
|
28
|
+
import { PoolLease } from "./pool-lease.js";
|
|
29
|
+
import { PoolQueue } from "./pool-queue.js";
|
|
30
|
+
import { PostgreSQLConnection } from "./postgresql-connection.js";
|
|
31
|
+
import { Query } from "./query.js";
|
|
32
|
+
import { StatementCache } from "./statement-cache.js";
|
|
33
|
+
import { TypeLoaderEvents } from "../../services/client/type-loader-events.js";
|
|
34
|
+
export class PostgreSQLPool extends ConnectionEvents {
|
|
35
|
+
static window = 2000;
|
|
36
|
+
static retypeDelay = 1000;
|
|
37
|
+
parameters = new Map();
|
|
38
|
+
clientOptions;
|
|
39
|
+
connection;
|
|
40
|
+
types;
|
|
41
|
+
starter;
|
|
42
|
+
timers;
|
|
43
|
+
statements;
|
|
44
|
+
connections = new Set();
|
|
45
|
+
waiting = new PoolQueue();
|
|
46
|
+
starting = new Set();
|
|
47
|
+
paused = new Set();
|
|
48
|
+
eager;
|
|
49
|
+
workFast = 0;
|
|
50
|
+
workSlow = 0;
|
|
51
|
+
workAt = 0;
|
|
52
|
+
aborted = false;
|
|
53
|
+
opening = 0;
|
|
54
|
+
cancelling = 0;
|
|
55
|
+
cancelAbort = new AbortController();
|
|
56
|
+
ending = null;
|
|
57
|
+
ended = null;
|
|
58
|
+
used = false;
|
|
59
|
+
endTimer = null;
|
|
60
|
+
retries = 0;
|
|
61
|
+
failingSince = null;
|
|
62
|
+
nextAttempt = 0;
|
|
63
|
+
retryTimer = null;
|
|
64
|
+
typesLoaded;
|
|
65
|
+
isTyped;
|
|
66
|
+
typesError = null;
|
|
67
|
+
typesFailed = 0;
|
|
68
|
+
failure = null;
|
|
69
|
+
typesLoading = false;
|
|
70
|
+
separate = false;
|
|
71
|
+
loader = null;
|
|
72
|
+
retryTypes = null;
|
|
73
|
+
typeRetries = 0;
|
|
74
|
+
typesFailing = null;
|
|
75
|
+
constructor(options, connection, types, starter) {
|
|
76
|
+
super();
|
|
77
|
+
this.clientOptions = options;
|
|
78
|
+
this.connection = connection;
|
|
79
|
+
this.types = types;
|
|
80
|
+
this.starter = starter;
|
|
81
|
+
this.timers = options.timers;
|
|
82
|
+
this.statements = new StatementCache(options.scheduler);
|
|
83
|
+
this.eager = types.named || options.eagerTypes;
|
|
84
|
+
this.typesLoaded = !options.fetchTypes;
|
|
85
|
+
this.isTyped = this.typesLoaded;
|
|
86
|
+
}
|
|
87
|
+
get options() {
|
|
88
|
+
return this.clientOptions;
|
|
89
|
+
}
|
|
90
|
+
get stats() {
|
|
91
|
+
const all = [...this.connections];
|
|
92
|
+
return new PoolStats(all.length, this.opening, all.filter(connection => connection.inflight === 0 && Object.isNull(connection.lease)).length, all.filter(connection => !Object.isNull(connection.lease)).length, this.waiting.length);
|
|
93
|
+
}
|
|
94
|
+
get typed() {
|
|
95
|
+
return this.isTyped;
|
|
96
|
+
}
|
|
97
|
+
get plain() {
|
|
98
|
+
return this.types.plain;
|
|
99
|
+
}
|
|
100
|
+
get cancels() {
|
|
101
|
+
return this.cancelAbort.signal;
|
|
102
|
+
}
|
|
103
|
+
findParser(oid) {
|
|
104
|
+
return this.types.findParser(oid);
|
|
105
|
+
}
|
|
106
|
+
findDecoder(oid, parser) {
|
|
107
|
+
return this.types.findDecoder(oid, parser);
|
|
108
|
+
}
|
|
109
|
+
findBinaryDecoder(oid) {
|
|
110
|
+
return this.types.findBinaryDecoder(oid);
|
|
111
|
+
}
|
|
112
|
+
findSerializer(oid) {
|
|
113
|
+
return this.types.findSerializer(oid);
|
|
114
|
+
}
|
|
115
|
+
hasUnknownTypes(oids) {
|
|
116
|
+
return !this.typesLoaded && this.types.hasUnknownTypes(oids);
|
|
117
|
+
}
|
|
118
|
+
handle(query) {
|
|
119
|
+
this.query(query, null);
|
|
120
|
+
}
|
|
121
|
+
query(query, lease) {
|
|
122
|
+
if (this.clientOptions.queryTimeout > 0 && Object.isNull(query.timer) && !query.settled && Object.isNull(query.cursorFn) && !query.streaming)
|
|
123
|
+
this.armDeadline(query);
|
|
124
|
+
if (!Object.isNull(this.ending) && Object.isNull(lease)) {
|
|
125
|
+
query.reject(this.createEndedError());
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
if (!Object.isNull(query.parts)) {
|
|
129
|
+
this.batch(query, query.parts, lease);
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
let built;
|
|
133
|
+
try {
|
|
134
|
+
built = query.build();
|
|
135
|
+
}
|
|
136
|
+
catch (error) {
|
|
137
|
+
this.rejectUnbuilt(query, PostgreSQLPool.toError(error), lease);
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
const free = Object.isNull(lease) && this.clientOptions.max !== 1;
|
|
141
|
+
if (query.options.simple && built.values.length === 0) {
|
|
142
|
+
const known = free || this.clientOptions.scheduler ? this.statements.readCost(built.text) : null;
|
|
143
|
+
query.alone = free && !Object.isNull(known) && StatementCache.isAlone(known, built.text);
|
|
144
|
+
if (this.clientOptions.scheduler)
|
|
145
|
+
query.cost = known;
|
|
146
|
+
const text = built.text;
|
|
147
|
+
const job = this.createJob(query, connection => connection.sendSimple(query, text));
|
|
148
|
+
job.plain = true;
|
|
149
|
+
job.exclusive = query.streaming || query.alone;
|
|
150
|
+
this.place(job, lease);
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
if (query.onlyDescribe) {
|
|
154
|
+
this.describeOnly(query, built, lease);
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
const statement = this.statements.get(built, this.clientOptions.prepare && query.options.prepare !== false);
|
|
158
|
+
query.alone = free && StatementCache.isAlone(statement.live ? statement : this.statements.readCost(built.text), built.text);
|
|
159
|
+
query.wire = new QueryWire(statement, built);
|
|
160
|
+
if (this.clientOptions.scheduler)
|
|
161
|
+
query.cost = statement.cost;
|
|
162
|
+
const job = this.createJob(query, connection => connection.send(query));
|
|
163
|
+
job.plain = true;
|
|
164
|
+
job.statement = statement;
|
|
165
|
+
job.typed = !this.typesLoaded && this.hasUntypedOids(built.oids);
|
|
166
|
+
job.exclusive = !Object.isNull(query.cursorFn) || query.streaming || query.alone;
|
|
167
|
+
this.place(job, lease);
|
|
168
|
+
}
|
|
169
|
+
async acquireLeaseAsync(kind) {
|
|
170
|
+
this.used = true;
|
|
171
|
+
await Promise.resolve();
|
|
172
|
+
if (!Object.isNull(this.ending))
|
|
173
|
+
throw this.createEndedError();
|
|
174
|
+
const failing = this.readFastFailure();
|
|
175
|
+
if (!Object.isNull(failing))
|
|
176
|
+
throw failing;
|
|
177
|
+
const missing = this.retype();
|
|
178
|
+
if (!Object.isNull(missing))
|
|
179
|
+
throw missing;
|
|
180
|
+
return new Promise((resolve, reject) => {
|
|
181
|
+
const job = new PoolJob(null, connection => resolve(this.grant(connection, kind)), reject);
|
|
182
|
+
job.lease = true;
|
|
183
|
+
this.waiting.push(this.wait(job));
|
|
184
|
+
this.drain();
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
cancelAsync(query) {
|
|
188
|
+
const removed = this.waiting.remove(job => {
|
|
189
|
+
if (job.query !== query)
|
|
190
|
+
return false;
|
|
191
|
+
this.markStarted(job);
|
|
192
|
+
return true;
|
|
193
|
+
});
|
|
194
|
+
if (removed) {
|
|
195
|
+
query.reject(new QueryCanceledException());
|
|
196
|
+
return Promise.resolve();
|
|
197
|
+
}
|
|
198
|
+
const connection = query.connection;
|
|
199
|
+
if (!Object.isNull(connection))
|
|
200
|
+
return connection.cancelAsync(query);
|
|
201
|
+
query.cancelled = true;
|
|
202
|
+
return Promise.resolve();
|
|
203
|
+
}
|
|
204
|
+
endAsync(timeout = null) {
|
|
205
|
+
if (!Object.isNull(this.ending))
|
|
206
|
+
return this.ending;
|
|
207
|
+
this.ending = new Promise(resolve => {
|
|
208
|
+
this.ended = () => resolve();
|
|
209
|
+
if (!Object.isNull(timeout))
|
|
210
|
+
this.endTimer = this.timers.setTimeout(() => this.destroy(), timeout * 1000);
|
|
211
|
+
this.timers.clearTimeout(this.retryTimer);
|
|
212
|
+
this.retryTimer = null;
|
|
213
|
+
queueMicrotask(() => this.finishEnding());
|
|
214
|
+
});
|
|
215
|
+
return this.ending;
|
|
216
|
+
}
|
|
217
|
+
async closeAsync() {
|
|
218
|
+
this.used = false;
|
|
219
|
+
await Promise.all([...this.connections].map(connection => Object.isNull(connection.lease)
|
|
220
|
+
? connection.closeAsync()
|
|
221
|
+
: new Promise(resolve => {
|
|
222
|
+
connection.retiring = true;
|
|
223
|
+
connection.retired.push(() => resolve());
|
|
224
|
+
})));
|
|
225
|
+
}
|
|
226
|
+
requestTypes(connection) {
|
|
227
|
+
if (!Object.isNull(connection))
|
|
228
|
+
this.paused.add(connection);
|
|
229
|
+
if (this.typesLoaded) {
|
|
230
|
+
this.resume();
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
if (this.typesLoading)
|
|
234
|
+
return;
|
|
235
|
+
this.typesLoading = true;
|
|
236
|
+
const idle = [...this.connections].find(item => !this.paused.has(item) && this.isUsable(item) && item.inflight === 0);
|
|
237
|
+
if (Object.isUndefined(idle))
|
|
238
|
+
this.watchTypeLoad(this.loadTypesSeparatelyAsync());
|
|
239
|
+
else
|
|
240
|
+
this.watchTypeLoad(this.types.loadAsync(this.createRunner(idle, false)));
|
|
241
|
+
}
|
|
242
|
+
onReady(connection) {
|
|
243
|
+
if (connection.releasing) {
|
|
244
|
+
if (connection.inflight > 0)
|
|
245
|
+
return;
|
|
246
|
+
connection.releasing = false;
|
|
247
|
+
if (connection.status !== TransactionStatus.Idle) {
|
|
248
|
+
connection.closeAsync();
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
if (connection.inflight === 0 && Object.isNull(connection.lease)
|
|
253
|
+
&& (connection.retiring || (!Object.isNull(this.ending) && this.waiting.length === 0))) {
|
|
254
|
+
connection.closeAsync();
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
this.handleIdle(connection);
|
|
258
|
+
for (let job = this.waiting.peek(); !Object.isUndefined(job) && this.waiting.length > this.connections.size + this.opening && this.isUsable(connection)
|
|
259
|
+
&& connection.inflight < this.clientOptions.maxPipeline && job.plain && !job.exclusive; job = this.waiting.peek()) {
|
|
260
|
+
this.waiting.shift();
|
|
261
|
+
this.run(connection, job);
|
|
262
|
+
}
|
|
263
|
+
if (this.waiting.length > 0 && Object.isNull(connection.lease) && connection.inflight < this.clientOptions.maxPipeline)
|
|
264
|
+
this.drain();
|
|
265
|
+
if (!Object.isNull(this.ending) && this.waiting.length === 0)
|
|
266
|
+
this.finishEnding();
|
|
267
|
+
}
|
|
268
|
+
onClose(connection, error) {
|
|
269
|
+
this.connections.delete(connection);
|
|
270
|
+
if ((connection.loading || !Object.isNull(connection.typing)) && !this.aborted)
|
|
271
|
+
this.recordFailedAttempt(error);
|
|
272
|
+
this.paused.delete(connection);
|
|
273
|
+
for (const resolve of connection.retired)
|
|
274
|
+
resolve();
|
|
275
|
+
this.timers.clearTimeout(connection.idleTimer);
|
|
276
|
+
this.timers.clearTimeout(connection.lifeTimer);
|
|
277
|
+
const lease = connection.lease;
|
|
278
|
+
if (!Object.isNull(lease))
|
|
279
|
+
lease.onClose(PostgreSQLPool.createLostError(connection, error));
|
|
280
|
+
this.clientOptions.onclose?.(connection.id);
|
|
281
|
+
if (this.connections.size === 0 && this.opening === 0)
|
|
282
|
+
this.statements.clear();
|
|
283
|
+
this.drain();
|
|
284
|
+
if (!Object.isNull(this.ending))
|
|
285
|
+
this.finishEnding();
|
|
286
|
+
}
|
|
287
|
+
onNotify(channel, payload, pid) {
|
|
288
|
+
this.clientOptions.onnotify?.(channel, payload, pid);
|
|
289
|
+
}
|
|
290
|
+
onParameter(name, value) {
|
|
291
|
+
if (this.parameters.get(name) === value)
|
|
292
|
+
return;
|
|
293
|
+
this.parameters.set(name, value);
|
|
294
|
+
this.clientOptions.onparameter?.(name, value);
|
|
295
|
+
}
|
|
296
|
+
onCancelling(cancel) {
|
|
297
|
+
this.cancelling += 1;
|
|
298
|
+
const settled = () => {
|
|
299
|
+
this.cancelling -= 1;
|
|
300
|
+
this.finishEnding();
|
|
301
|
+
};
|
|
302
|
+
cancel.then(settled, settled);
|
|
303
|
+
}
|
|
304
|
+
onTimed(sample, time) {
|
|
305
|
+
this.decay(time);
|
|
306
|
+
if (sample < PostgreSQLConnection.slow)
|
|
307
|
+
this.workFast += sample;
|
|
308
|
+
else
|
|
309
|
+
this.workSlow += sample;
|
|
310
|
+
}
|
|
311
|
+
createJob(query, send) {
|
|
312
|
+
return new PoolJob(query, connection => {
|
|
313
|
+
query.connection = connection;
|
|
314
|
+
send(connection);
|
|
315
|
+
query.onwrite?.();
|
|
316
|
+
}, error => query.reject(error));
|
|
317
|
+
}
|
|
318
|
+
hasUntypedOids(oids) {
|
|
319
|
+
return oids.some(oid => oid !== 0 && !this.types.isKnown(oid));
|
|
320
|
+
}
|
|
321
|
+
armDeadline(query) {
|
|
322
|
+
query.timer = this.timers.setTimeout(() => {
|
|
323
|
+
query.timer = null;
|
|
324
|
+
if (!query.settled) {
|
|
325
|
+
query.timedOut = true;
|
|
326
|
+
this.cancelAsync(query).catch(PostgreSQLPool.ignore);
|
|
327
|
+
}
|
|
328
|
+
}, this.clientOptions.queryTimeout * 1000);
|
|
329
|
+
}
|
|
330
|
+
batch(query, parts, lease) {
|
|
331
|
+
if (parts.length === 0) {
|
|
332
|
+
query.resolve([]);
|
|
333
|
+
return;
|
|
334
|
+
}
|
|
335
|
+
for (const [index, part] of parts.entries()) {
|
|
336
|
+
try {
|
|
337
|
+
part.build();
|
|
338
|
+
}
|
|
339
|
+
catch (error) {
|
|
340
|
+
this.rejectUnbuilt(query, new BatchException(index, PostgreSQLPool.toError(error)), lease);
|
|
341
|
+
return;
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
for (const part of parts) {
|
|
345
|
+
const built = part.build();
|
|
346
|
+
part.wire = new QueryWire(this.statements.get(built, this.clientOptions.prepare && part.options.prepare !== false), built);
|
|
347
|
+
}
|
|
348
|
+
const job = this.createJob(query, connection => connection.batch(query, parts));
|
|
349
|
+
job.typed = !this.typesLoaded && parts.some(part => this.hasUntypedOids(part.build().oids));
|
|
350
|
+
this.place(job, lease);
|
|
351
|
+
}
|
|
352
|
+
describeOnly(query, built, lease) {
|
|
353
|
+
const describe = (connection) => {
|
|
354
|
+
connection.describeAsync(built.text, built.oids, query).then(shape => query.resolve(new StatementDescription("", built.text, shape.params, shape.columns ?? [])), (error) => query.reject(PostgreSQLPool.toError(error)));
|
|
355
|
+
};
|
|
356
|
+
this.place(this.createJob(query, describe), lease);
|
|
357
|
+
}
|
|
358
|
+
rejectUnbuilt(query, error, lease) {
|
|
359
|
+
if (Object.isNull(lease) || lease.released || !Object.isNull(lease.connection.closed)) {
|
|
360
|
+
query.reject(error);
|
|
361
|
+
return;
|
|
362
|
+
}
|
|
363
|
+
query.connection = lease.connection;
|
|
364
|
+
lease.connection.failLocally(query, error);
|
|
365
|
+
query.onwrite?.();
|
|
366
|
+
}
|
|
367
|
+
place(job, lease) {
|
|
368
|
+
this.used = true;
|
|
369
|
+
const missing = Object.isNull(lease) ? this.retype() : null;
|
|
370
|
+
if (!Object.isNull(missing)) {
|
|
371
|
+
this.reject(job, missing);
|
|
372
|
+
return;
|
|
373
|
+
}
|
|
374
|
+
if (job.query?.cancelled === true) {
|
|
375
|
+
this.reject(job, new QueryCanceledException());
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
378
|
+
if (!Object.isNull(lease)) {
|
|
379
|
+
const closed = lease.connection.closed;
|
|
380
|
+
if (!Object.isNull(closed))
|
|
381
|
+
this.reject(job, closed);
|
|
382
|
+
else if (lease.released)
|
|
383
|
+
this.reject(job, lease.createEndedError());
|
|
384
|
+
else
|
|
385
|
+
this.run(lease.connection, job);
|
|
386
|
+
return;
|
|
387
|
+
}
|
|
388
|
+
const failing = this.readFastFailure();
|
|
389
|
+
if (!Object.isNull(failing)) {
|
|
390
|
+
this.reject(job, failing);
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
const connection = this.waiting.length === 0 ? this.pick(job) : null;
|
|
394
|
+
if (!Object.isNull(connection)) {
|
|
395
|
+
this.run(connection, job);
|
|
396
|
+
return;
|
|
397
|
+
}
|
|
398
|
+
this.waiting.push(this.wait(job));
|
|
399
|
+
this.drain();
|
|
400
|
+
}
|
|
401
|
+
wait(job) {
|
|
402
|
+
const seconds = this.clientOptions.queueTimeout;
|
|
403
|
+
if (seconds <= 0)
|
|
404
|
+
return job;
|
|
405
|
+
const timer = this.timers.setTimeout(() => {
|
|
406
|
+
job.timer = null;
|
|
407
|
+
if (this.waiting.remove(item => item === job))
|
|
408
|
+
this.reject(job, new QueueTimeoutException(seconds));
|
|
409
|
+
}, seconds * 1000);
|
|
410
|
+
this.timers.unref(timer);
|
|
411
|
+
job.timer = timer;
|
|
412
|
+
return job;
|
|
413
|
+
}
|
|
414
|
+
markStarted(job) {
|
|
415
|
+
this.timers.clearTimeout(job.timer);
|
|
416
|
+
job.timer = null;
|
|
417
|
+
}
|
|
418
|
+
run(connection, job) {
|
|
419
|
+
this.markStarted(job);
|
|
420
|
+
this.hold(connection);
|
|
421
|
+
job.run(connection);
|
|
422
|
+
}
|
|
423
|
+
drain() {
|
|
424
|
+
for (let job = this.waiting.peek(); !Object.isUndefined(job); job = this.waiting.peek()) {
|
|
425
|
+
const connection = this.pick(job);
|
|
426
|
+
if (Object.isNull(connection)) {
|
|
427
|
+
if (!this.isEveryConnectionCopying(job))
|
|
428
|
+
break;
|
|
429
|
+
this.waiting.shift();
|
|
430
|
+
this.reject(job, new DataException(Resources.copyInProgress));
|
|
431
|
+
}
|
|
432
|
+
else {
|
|
433
|
+
this.waiting.shift();
|
|
434
|
+
this.run(connection, job);
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
this.grow();
|
|
438
|
+
}
|
|
439
|
+
isEveryConnectionCopying(job) {
|
|
440
|
+
if (Object.isNull(job.query) || this.connections.size + this.opening < this.clientOptions.max || this.connections.size === 0)
|
|
441
|
+
return false;
|
|
442
|
+
for (const connection of this.connections)
|
|
443
|
+
if (!connection.copying)
|
|
444
|
+
return false;
|
|
445
|
+
return true;
|
|
446
|
+
}
|
|
447
|
+
isUsable(connection) {
|
|
448
|
+
return Object.isNull(connection.closed) && !connection.closing && Object.isNull(connection.lease) && !connection.releasing && !connection.retiring
|
|
449
|
+
&& !connection.exclusive && !connection.loading;
|
|
450
|
+
}
|
|
451
|
+
pick(job) {
|
|
452
|
+
if (!this.typesLoaded && (this.eager ? job.lease || job.typed : job.typed)) {
|
|
453
|
+
if (!this.eager)
|
|
454
|
+
this.requestTypes(null);
|
|
455
|
+
return null;
|
|
456
|
+
}
|
|
457
|
+
const name = Object.isNull(job.statement) ? "" : job.statement.name;
|
|
458
|
+
let best = null;
|
|
459
|
+
let soon = false;
|
|
460
|
+
for (const connection of this.connections) {
|
|
461
|
+
if (connection.loading && Object.isNull(connection.closed))
|
|
462
|
+
soon = true;
|
|
463
|
+
const free = this.isUsable(connection);
|
|
464
|
+
if (free && PostgreSQLPool.isQuiet(connection)) {
|
|
465
|
+
if (name.length === 0 || connection.prepared.has(name))
|
|
466
|
+
return connection;
|
|
467
|
+
best = !Object.isNull(best) && PostgreSQLPool.isQuiet(best) ? best : connection;
|
|
468
|
+
}
|
|
469
|
+
else if (free && !job.lease && !job.exclusive && connection.inflight < this.clientOptions.maxPipeline
|
|
470
|
+
&& (Object.isNull(best) || connection.inflight < best.inflight))
|
|
471
|
+
best = connection;
|
|
472
|
+
}
|
|
473
|
+
if (!Object.isNull(best) && PostgreSQLPool.isQuiet(best))
|
|
474
|
+
return best;
|
|
475
|
+
if (soon || (this.connections.size + this.opening < this.clientOptions.max && Object.isNull(this.failingSince)))
|
|
476
|
+
return null;
|
|
477
|
+
const query = job.query;
|
|
478
|
+
if (!Object.isNull(best) && this.clientOptions.scheduler && !Object.isNull(query) && this.needsScheduling(query)) {
|
|
479
|
+
const scheduled = this.chooseConnection(query);
|
|
480
|
+
if (!Object.isNull(scheduled))
|
|
481
|
+
best = scheduled;
|
|
482
|
+
}
|
|
483
|
+
return !Object.isNull(best) && this.opening > 0 && best.inflight >= Math.ceil(this.waiting.length / (this.opening + 1)) ? null : best;
|
|
484
|
+
}
|
|
485
|
+
needsScheduling(query) {
|
|
486
|
+
const estimate = query.cost?.estimate ?? null;
|
|
487
|
+
if (Object.isNull(estimate) || estimate >= PostgreSQLConnection.notable)
|
|
488
|
+
return true;
|
|
489
|
+
for (const connection of this.connections)
|
|
490
|
+
if (connection.notable)
|
|
491
|
+
return true;
|
|
492
|
+
return false;
|
|
493
|
+
}
|
|
494
|
+
chooseConnection(query) {
|
|
495
|
+
const time = this.timers.readTime();
|
|
496
|
+
const estimate = query.cost?.estimate ?? null;
|
|
497
|
+
if (Object.isNull(estimate) || estimate < PostgreSQLConnection.slow)
|
|
498
|
+
return this.findSoonest(time, Infinity, false);
|
|
499
|
+
let heavy = 0;
|
|
500
|
+
for (const connection of this.connections)
|
|
501
|
+
if (connection.inflight > 0 && connection.isHeavy())
|
|
502
|
+
heavy += 1;
|
|
503
|
+
this.decay(time);
|
|
504
|
+
const size = this.connections.size;
|
|
505
|
+
const reserve = Math.max(1, Math.min(Math.ceil(this.workFast / PostgreSQLPool.window * 1.5) + 1, size - Math.ceil(this.workSlow / PostgreSQLPool.window)));
|
|
506
|
+
return heavy < size - reserve
|
|
507
|
+
? this.findSoonest(time, Infinity, false)
|
|
508
|
+
: this.findSoonest(time, 2 * estimate, true) ?? this.findSoonest(time, Infinity, false);
|
|
509
|
+
}
|
|
510
|
+
findSoonest(time, most, heavy) {
|
|
511
|
+
let value = Infinity;
|
|
512
|
+
let tied = [];
|
|
513
|
+
for (const connection of this.connections) {
|
|
514
|
+
const wait = this.isUsable(connection) && connection.inflight < this.clientOptions.maxPipeline && (!heavy || connection.isHeavy())
|
|
515
|
+
? connection.computeBacklog(time)
|
|
516
|
+
: Infinity;
|
|
517
|
+
if (wait === Infinity || wait > most)
|
|
518
|
+
continue;
|
|
519
|
+
if (wait < value) {
|
|
520
|
+
value = wait;
|
|
521
|
+
tied = [connection];
|
|
522
|
+
}
|
|
523
|
+
else if (wait === value)
|
|
524
|
+
tied.push(connection);
|
|
525
|
+
}
|
|
526
|
+
return tied[Math.floor(Math.random() * tied.length)] ?? null;
|
|
527
|
+
}
|
|
528
|
+
decay(time) {
|
|
529
|
+
const factor = Math.exp((this.workAt - time) / PostgreSQLPool.window);
|
|
530
|
+
this.workFast *= factor;
|
|
531
|
+
this.workSlow *= factor;
|
|
532
|
+
this.workAt = time;
|
|
533
|
+
}
|
|
534
|
+
grow() {
|
|
535
|
+
if (!Object.isNull(this.ending) && this.waiting.length === 0)
|
|
536
|
+
return;
|
|
537
|
+
if (!Object.isNull(this.failingSince)) {
|
|
538
|
+
if (this.opening > 0 || this.waiting.length === 0 || !Object.isNull(this.retryTimer) || [...this.connections].some(connection => connection.loading))
|
|
539
|
+
return;
|
|
540
|
+
const wait = this.nextAttempt - this.timers.readTime();
|
|
541
|
+
if (wait > 0)
|
|
542
|
+
this.retryTimer = this.timers.setTimeout(() => {
|
|
543
|
+
this.retryTimer = null;
|
|
544
|
+
this.grow();
|
|
545
|
+
}, wait);
|
|
546
|
+
else
|
|
547
|
+
this.connect();
|
|
548
|
+
return;
|
|
549
|
+
}
|
|
550
|
+
let loading = 0;
|
|
551
|
+
let idle = 0;
|
|
552
|
+
for (const connection of this.connections)
|
|
553
|
+
if (connection.loading)
|
|
554
|
+
loading += 1;
|
|
555
|
+
else if (!this.typesLoaded && this.isUsable(connection) && connection.inflight === 0)
|
|
556
|
+
idle += 1;
|
|
557
|
+
const max = this.clientOptions.max;
|
|
558
|
+
const size = this.connections.size;
|
|
559
|
+
const wanted = Math.max(Math.min(this.waiting.length - this.opening - loading - idle, max - size - this.opening), this.used && Object.isNull(this.ending) ? Math.min(this.clientOptions.min, max) - size - this.opening : 0);
|
|
560
|
+
for (let index = 0; index < wanted; index += 1)
|
|
561
|
+
this.connect();
|
|
562
|
+
}
|
|
563
|
+
connect() {
|
|
564
|
+
this.opening += 1;
|
|
565
|
+
this.timers.clearTimeout(this.retryTimer);
|
|
566
|
+
this.retryTimer = null;
|
|
567
|
+
this.startAsync().then(session => this.handleOpened(session), (error) => this.handleOpenFailure(PostgreSQLPool.toError(error)));
|
|
568
|
+
}
|
|
569
|
+
async startAsync() {
|
|
570
|
+
const starter = this.starter(this.connection);
|
|
571
|
+
this.starting.add(starter);
|
|
572
|
+
try {
|
|
573
|
+
return await starter.startAsync();
|
|
574
|
+
}
|
|
575
|
+
finally {
|
|
576
|
+
this.starting.delete(starter);
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
readFastFailure() {
|
|
580
|
+
return this.retries > 1 && this.opening === 0 && this.timers.readTime() < this.nextAttempt && this.isStuck() ? this.failure : null;
|
|
581
|
+
}
|
|
582
|
+
isStuck() {
|
|
583
|
+
for (const connection of this.connections)
|
|
584
|
+
if (Object.isNull(connection.lease))
|
|
585
|
+
return false;
|
|
586
|
+
return true;
|
|
587
|
+
}
|
|
588
|
+
handleOpened(session) {
|
|
589
|
+
this.opening -= 1;
|
|
590
|
+
if (this.aborted || (!Object.isNull(this.ending) && this.waiting.length === 0)) {
|
|
591
|
+
session.socket.destroy();
|
|
592
|
+
this.finishEnding();
|
|
593
|
+
return;
|
|
594
|
+
}
|
|
595
|
+
for (const [name, value] of session.parameters)
|
|
596
|
+
this.onParameter(name, value);
|
|
597
|
+
const connection = new PostgreSQLConnection(session, this.clientOptions, this);
|
|
598
|
+
this.connections.add(connection);
|
|
599
|
+
this.scheduleLifetime(connection);
|
|
600
|
+
if (!this.typesLoaded && this.eager) {
|
|
601
|
+
if (this.typesLoading)
|
|
602
|
+
connection.loading = true;
|
|
603
|
+
else
|
|
604
|
+
this.loadTypes(connection);
|
|
605
|
+
}
|
|
606
|
+
if (!connection.loading && Object.isNull(connection.typing) && !(this.eager && this.typesLoading))
|
|
607
|
+
this.markRecovered();
|
|
608
|
+
this.drain();
|
|
609
|
+
}
|
|
610
|
+
markRecovered() {
|
|
611
|
+
this.retries = 0;
|
|
612
|
+
this.failingSince = null;
|
|
613
|
+
this.failure = null;
|
|
614
|
+
}
|
|
615
|
+
async loadTypesSeparatelyAsync() {
|
|
616
|
+
this.separate = true;
|
|
617
|
+
try {
|
|
618
|
+
await this.loadWith(await this.startAsync());
|
|
619
|
+
}
|
|
620
|
+
finally {
|
|
621
|
+
this.separate = false;
|
|
622
|
+
this.loader = null;
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
loadWith(session) {
|
|
626
|
+
const connection = new PostgreSQLConnection(session, this.clientOptions, new TypeLoaderEvents(this));
|
|
627
|
+
this.loader = connection;
|
|
628
|
+
return this.types.loadAsync(this.createRunner(connection, false)).finally(() => connection.closeAsync());
|
|
629
|
+
}
|
|
630
|
+
watchTypeLoad(loading) {
|
|
631
|
+
loading.then(() => {
|
|
632
|
+
this.typesLoaded = true;
|
|
633
|
+
return null;
|
|
634
|
+
}, (error) => {
|
|
635
|
+
if (!(error instanceof PostgreSQLException))
|
|
636
|
+
return PostgreSQLPool.toError(error);
|
|
637
|
+
this.typesLoaded = true;
|
|
638
|
+
return null;
|
|
639
|
+
}).then(failure => {
|
|
640
|
+
this.handleTypesFetched(failure);
|
|
641
|
+
this.finishEnding();
|
|
642
|
+
});
|
|
643
|
+
}
|
|
644
|
+
handleTypesFetched(failure) {
|
|
645
|
+
this.typesLoading = false;
|
|
646
|
+
this.isTyped = this.typesLoaded;
|
|
647
|
+
if (this.typesLoaded) {
|
|
648
|
+
this.typeRetries = 0;
|
|
649
|
+
this.typesFailing = null;
|
|
650
|
+
this.resume();
|
|
651
|
+
return;
|
|
652
|
+
}
|
|
653
|
+
const now = this.timers.readTime();
|
|
654
|
+
const failing = this.typesFailing ?? now;
|
|
655
|
+
this.typesFailing = failing;
|
|
656
|
+
this.typeRetries += 1;
|
|
657
|
+
const timeout = this.connection.connectTimeout;
|
|
658
|
+
if (timeout > 0 && now - failing >= timeout * 1000) {
|
|
659
|
+
this.typeRetries = 0;
|
|
660
|
+
this.typesFailing = null;
|
|
661
|
+
for (const connection of [...this.paused]) {
|
|
662
|
+
this.paused.delete(connection);
|
|
663
|
+
connection.destroy(failure);
|
|
664
|
+
}
|
|
665
|
+
return;
|
|
666
|
+
}
|
|
667
|
+
if (this.paused.size > 0)
|
|
668
|
+
this.retryTypes = this.timers.setTimeout(() => {
|
|
669
|
+
this.retryTypes = null;
|
|
670
|
+
this.requestTypes(null);
|
|
671
|
+
}, 1000 * this.computeBackoff(this.typeRetries));
|
|
672
|
+
}
|
|
673
|
+
resume() {
|
|
674
|
+
const all = [...this.paused];
|
|
675
|
+
this.paused.clear();
|
|
676
|
+
for (const connection of all)
|
|
677
|
+
if (Object.isNull(connection.closed))
|
|
678
|
+
connection.go();
|
|
679
|
+
this.drain();
|
|
680
|
+
}
|
|
681
|
+
createRunner(connection, typing) {
|
|
682
|
+
return async (text) => {
|
|
683
|
+
const query = new Query(new BuiltQuery(text, [], []), this, new QueryOptions(true, false));
|
|
684
|
+
query.executed = true;
|
|
685
|
+
if (typing)
|
|
686
|
+
connection.typing = query;
|
|
687
|
+
this.armQueryTimeout(connection, query);
|
|
688
|
+
connection.sendSimple(query, text);
|
|
689
|
+
const outcome = await query.promise;
|
|
690
|
+
return outcome instanceof Result ? outcome : [];
|
|
691
|
+
};
|
|
692
|
+
}
|
|
693
|
+
armQueryTimeout(connection, query) {
|
|
694
|
+
const seconds = this.clientOptions.queryTimeout;
|
|
695
|
+
if (seconds <= 0)
|
|
696
|
+
return;
|
|
697
|
+
const timer = this.timers.setTimeout(() => {
|
|
698
|
+
if (!query.settled)
|
|
699
|
+
connection.cancelAsync(query).catch(PostgreSQLPool.ignore);
|
|
700
|
+
}, seconds * 1000);
|
|
701
|
+
query.promise.then(() => this.timers.clearTimeout(timer), () => this.timers.clearTimeout(timer));
|
|
702
|
+
}
|
|
703
|
+
retype() {
|
|
704
|
+
const error = this.typesError;
|
|
705
|
+
if (Object.isNull(error))
|
|
706
|
+
return null;
|
|
707
|
+
if (this.timers.readTime() - this.typesFailed < PostgreSQLPool.retypeDelay)
|
|
708
|
+
return error;
|
|
709
|
+
this.typesError = null;
|
|
710
|
+
for (const connection of this.connections)
|
|
711
|
+
connection.loading = true;
|
|
712
|
+
const first = [...this.connections].find(connection => Object.isNull(connection.closed));
|
|
713
|
+
if (!Object.isUndefined(first))
|
|
714
|
+
this.loadTypes(first);
|
|
715
|
+
return null;
|
|
716
|
+
}
|
|
717
|
+
loadTypes(connection) {
|
|
718
|
+
this.hold(connection);
|
|
719
|
+
this.typesLoading = true;
|
|
720
|
+
connection.loading = this.types.named;
|
|
721
|
+
connection.typing = null;
|
|
722
|
+
this.types.loadAsync(this.createRunner(connection, !this.types.named)).then(() => {
|
|
723
|
+
this.typesLoaded = true;
|
|
724
|
+
}, (error) => {
|
|
725
|
+
if (error instanceof PostgreSQLException)
|
|
726
|
+
this.typesLoaded = true;
|
|
727
|
+
else if (error instanceof TypeNotFoundException) {
|
|
728
|
+
this.typesError = error;
|
|
729
|
+
this.typesFailed = this.timers.readTime();
|
|
730
|
+
}
|
|
731
|
+
}).then(() => this.handleTypesSettled(connection));
|
|
732
|
+
}
|
|
733
|
+
handleTypesSettled(connection) {
|
|
734
|
+
this.typesLoading = false;
|
|
735
|
+
this.isTyped = this.typesLoaded;
|
|
736
|
+
connection.typing = null;
|
|
737
|
+
const error = this.typesError;
|
|
738
|
+
for (const item of this.connections)
|
|
739
|
+
item.loading = !this.typesLoaded && Object.isNull(error);
|
|
740
|
+
if (this.typesLoaded || !Object.isNull(error))
|
|
741
|
+
this.markRecovered();
|
|
742
|
+
if (!Object.isNull(error))
|
|
743
|
+
for (const job of this.waiting.take())
|
|
744
|
+
this.reject(job, error);
|
|
745
|
+
else if (!this.typesLoaded) {
|
|
746
|
+
const next = [...this.connections].find(item => Object.isNull(item.closed) && Object.isNull(item.typing));
|
|
747
|
+
if (!Object.isUndefined(next))
|
|
748
|
+
this.loadTypes(next);
|
|
749
|
+
}
|
|
750
|
+
this.drain();
|
|
751
|
+
}
|
|
752
|
+
handleOpenFailure(error) {
|
|
753
|
+
this.opening -= 1;
|
|
754
|
+
if (this.aborted) {
|
|
755
|
+
this.finishEnding();
|
|
756
|
+
return;
|
|
757
|
+
}
|
|
758
|
+
this.recordFailedAttempt(error);
|
|
759
|
+
if (Object.isNull(this.ending))
|
|
760
|
+
this.grow();
|
|
761
|
+
else
|
|
762
|
+
this.finishEnding();
|
|
763
|
+
}
|
|
764
|
+
recordFailedAttempt(error) {
|
|
765
|
+
const now = this.timers.readTime();
|
|
766
|
+
const since = this.failingSince ?? now;
|
|
767
|
+
this.failingSince = since;
|
|
768
|
+
this.retries += 1;
|
|
769
|
+
this.nextAttempt = now + 1000 * this.computeBackoff(this.retries);
|
|
770
|
+
const transient = (error instanceof ConnectionClosedException && !error.destroyed)
|
|
771
|
+
|| (error instanceof PostgreSQLException && error.sqlState === Resources.tooManyConnectionsState);
|
|
772
|
+
const timeout = this.connection.connectTimeout;
|
|
773
|
+
const expired = timeout > 0 && now - since >= timeout * 1000;
|
|
774
|
+
this.failure = !transient || expired ? error : null;
|
|
775
|
+
if (this.isStuck() && this.opening === 0 && (!Object.isNull(this.failure) || !Object.isNull(this.ending)))
|
|
776
|
+
for (const job of this.waiting.take())
|
|
777
|
+
this.reject(job, error);
|
|
778
|
+
}
|
|
779
|
+
computeBackoff(retries) {
|
|
780
|
+
const backoff = this.clientOptions.backoff;
|
|
781
|
+
return Object.isFunction(backoff) ? backoff(retries) : backoff;
|
|
782
|
+
}
|
|
783
|
+
reject(job, error) {
|
|
784
|
+
this.markStarted(job);
|
|
785
|
+
job.fail(error);
|
|
786
|
+
}
|
|
787
|
+
scheduleLifetime(connection) {
|
|
788
|
+
const configured = this.clientOptions.maxLifetime;
|
|
789
|
+
const life = Object.isFunction(configured) ? configured() : configured;
|
|
790
|
+
if (!Object.isNull(life) && life > 0)
|
|
791
|
+
connection.lifeTimer = this.unref(this.timers.setTimeout(() => {
|
|
792
|
+
connection.retiring = true;
|
|
793
|
+
if (connection.inflight === 0 && Object.isNull(connection.lease))
|
|
794
|
+
connection.closeAsync();
|
|
795
|
+
}, life * 1000));
|
|
796
|
+
this.handleIdle(connection);
|
|
797
|
+
}
|
|
798
|
+
handleIdle(connection) {
|
|
799
|
+
if (connection.inflight > 0 || !Object.isNull(connection.lease))
|
|
800
|
+
return;
|
|
801
|
+
if (this.clientOptions.unrefSockets && !connection.loading && this.waiting.length === 0)
|
|
802
|
+
connection.unref();
|
|
803
|
+
const timeout = this.clientOptions.idleTimeout;
|
|
804
|
+
if (Object.isNull(timeout) || timeout <= 0)
|
|
805
|
+
return;
|
|
806
|
+
connection.idleSince = this.timers.readTime();
|
|
807
|
+
if (Object.isNull(connection.idleTimer))
|
|
808
|
+
connection.idleTimer = this.unref(this.timers.setTimeout(() => this.handleIdleTimeout(connection, timeout), timeout * 1000));
|
|
809
|
+
}
|
|
810
|
+
handleIdleTimeout(connection, timeout) {
|
|
811
|
+
connection.idleTimer = null;
|
|
812
|
+
if (connection.inflight > 0 || !Object.isNull(connection.lease) || !Object.isNull(connection.closed))
|
|
813
|
+
return;
|
|
814
|
+
if (this.connections.size <= this.clientOptions.min)
|
|
815
|
+
return;
|
|
816
|
+
const left = timeout * 1000 - (this.timers.readTime() - connection.idleSince);
|
|
817
|
+
if (left >= 1)
|
|
818
|
+
connection.idleTimer = this.unref(this.timers.setTimeout(() => this.handleIdleTimeout(connection, timeout), left));
|
|
819
|
+
else
|
|
820
|
+
connection.closeAsync();
|
|
821
|
+
}
|
|
822
|
+
hold(connection) {
|
|
823
|
+
if (this.clientOptions.unrefSockets)
|
|
824
|
+
connection.ref();
|
|
825
|
+
}
|
|
826
|
+
unref(timer) {
|
|
827
|
+
if (this.clientOptions.unrefSockets)
|
|
828
|
+
this.timers.unref(timer);
|
|
829
|
+
return timer;
|
|
830
|
+
}
|
|
831
|
+
grant(connection, kind) {
|
|
832
|
+
this.timers.clearTimeout(connection.idleTimer);
|
|
833
|
+
connection.idleTimer = null;
|
|
834
|
+
const lease = new PoolLease(this, connection, kind);
|
|
835
|
+
connection.lease = lease;
|
|
836
|
+
return lease;
|
|
837
|
+
}
|
|
838
|
+
finishEnding() {
|
|
839
|
+
const ended = this.ended;
|
|
840
|
+
if (Object.isNull(ended))
|
|
841
|
+
return;
|
|
842
|
+
if (this.waiting.length === 0)
|
|
843
|
+
for (const connection of [...this.connections])
|
|
844
|
+
if (connection.inflight === 0 && Object.isNull(connection.lease))
|
|
845
|
+
connection.closeAsync();
|
|
846
|
+
if (this.connections.size === 0 && this.opening === 0 && this.waiting.length > 0) {
|
|
847
|
+
const error = this.createEndedError();
|
|
848
|
+
for (const job of this.waiting.take())
|
|
849
|
+
this.reject(job, error);
|
|
850
|
+
}
|
|
851
|
+
if (this.connections.size === 0 && (this.opening === 0 || this.aborted) && this.waiting.length === 0 && this.cancelling === 0 && !this.separate) {
|
|
852
|
+
this.timers.clearTimeout(this.endTimer);
|
|
853
|
+
this.timers.clearTimeout(this.retryTimer);
|
|
854
|
+
this.timers.clearTimeout(this.retryTypes);
|
|
855
|
+
this.ended = null;
|
|
856
|
+
ended();
|
|
857
|
+
}
|
|
858
|
+
}
|
|
859
|
+
destroy() {
|
|
860
|
+
this.endTimer = null;
|
|
861
|
+
this.aborted = true;
|
|
862
|
+
for (const starter of this.starting)
|
|
863
|
+
starter.stop();
|
|
864
|
+
const error = this.createDestroyedError();
|
|
865
|
+
this.cancelAbort.abort(error);
|
|
866
|
+
for (const job of this.waiting.take())
|
|
867
|
+
this.reject(job, error);
|
|
868
|
+
for (const connection of [...this.connections])
|
|
869
|
+
connection.destroy();
|
|
870
|
+
this.loader?.destroy();
|
|
871
|
+
if (this.connections.size === 0)
|
|
872
|
+
this.finishEnding();
|
|
873
|
+
}
|
|
874
|
+
createEndedError() {
|
|
875
|
+
return new ConnectionEndedException(this.connection.host, this.connection.port);
|
|
876
|
+
}
|
|
877
|
+
createDestroyedError() {
|
|
878
|
+
return new ConnectionClosedException(true, this.connection.host, this.connection.port, null);
|
|
879
|
+
}
|
|
880
|
+
static isQuiet(connection) {
|
|
881
|
+
return connection.inflight === 0 || (!Object.isNull(connection.typing) && !connection.typing.settled && connection.inflight === 1);
|
|
882
|
+
}
|
|
883
|
+
static createLostError(connection, error) {
|
|
884
|
+
const cause = error.cause;
|
|
885
|
+
if (cause instanceof PostgreSQLException && cause.severity === Resources.fatalSeverity && !connection.uncertain)
|
|
886
|
+
return cause;
|
|
887
|
+
return error.destroyed ? new ConnectionClosedException(false, connection.host, connection.port, null) : error;
|
|
888
|
+
}
|
|
889
|
+
static ignore() {
|
|
890
|
+
return undefined;
|
|
891
|
+
}
|
|
892
|
+
static toError(error) {
|
|
893
|
+
return error instanceof Error ? error : new DataException(Resources.nonErrorThrown, new ExceptionOptions(error));
|
|
894
|
+
}
|
|
895
|
+
}
|
|
896
|
+
//# sourceMappingURL=postgresql-pool.js.map
|