@ultimat3/db 31.22.1 → 31.23.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/src/pool-listeners.ts +54 -8
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ultimat3/db",
|
|
3
|
-
"version": "31.
|
|
3
|
+
"version": "31.23.0",
|
|
4
4
|
"description": "Postgres access, transactions, migrations and drift detection",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ultimate",
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
"test": "bun test"
|
|
46
46
|
},
|
|
47
47
|
"dependencies": {
|
|
48
|
-
"@ultimat3/core": "31.
|
|
48
|
+
"@ultimat3/core": "31.23.0"
|
|
49
49
|
},
|
|
50
50
|
"peerDependencies": {
|
|
51
51
|
"@electric-sql/pglite": ">=0.5.0"
|
package/src/pool-listeners.ts
CHANGED
|
@@ -2,7 +2,13 @@
|
|
|
2
2
|
// them across a retired pool. `Bun.SQL` re-dials a session whose backend died, but `close()` ends it
|
|
3
3
|
// for good, so a retirement would leave `startQueueWake` reporting live on a session that is gone.
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import {
|
|
6
|
+
backoffDelay,
|
|
7
|
+
isUltimateError,
|
|
8
|
+
logger,
|
|
9
|
+
renderThrowable,
|
|
10
|
+
type Scheduler,
|
|
11
|
+
} from '@ultimat3/core';
|
|
6
12
|
import type { BunSqlDriver } from './bun-sql';
|
|
7
13
|
import { DbError, driverError } from './errors';
|
|
8
14
|
import { assertListenChannel, type DbSubscription, listenUnsupported } from './listen';
|
|
@@ -34,6 +40,26 @@ interface Held {
|
|
|
34
40
|
dial: number;
|
|
35
41
|
cancelRetry: () => void;
|
|
36
42
|
ended: boolean;
|
|
43
|
+
/**
|
|
44
|
+
* Refused dials since this session last held, across retirements: a restart is ONE streak — one
|
|
45
|
+
* line when it opens, one when a dial lands (`db.listen.redialed`, with the count), never a line
|
|
46
|
+
* per attempt; a refusal with a different cause (`causeOf`) logs again, naming the one it ends.
|
|
47
|
+
* Local until core ships a limiter for it.
|
|
48
|
+
*/
|
|
49
|
+
failures: number;
|
|
50
|
+
cause: FailureCause | undefined;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
interface FailureCause {
|
|
54
|
+
readonly key: string;
|
|
55
|
+
readonly label: Readonly<Record<string, string>>;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** What makes two refusals the same one: the code, or, uncoded, the rendered message. */
|
|
59
|
+
function causeOf(error: unknown): FailureCause {
|
|
60
|
+
if (isUltimateError(error)) return { key: `code:${error.code}`, label: { code: error.code } };
|
|
61
|
+
const message = renderThrowable(error);
|
|
62
|
+
return { key: `error:${message}`, label: { error: message } };
|
|
37
63
|
}
|
|
38
64
|
|
|
39
65
|
export function poolListeners(
|
|
@@ -67,16 +93,34 @@ export function poolListeners(
|
|
|
67
93
|
// `async` so a pool that cannot be built (`currentPool` throws) lands in the retry too.
|
|
68
94
|
void (async () => dialOn(entry))().then(
|
|
69
95
|
(session) => {
|
|
70
|
-
if (overtaken())
|
|
71
|
-
|
|
96
|
+
if (overtaken()) {
|
|
97
|
+
void end(session);
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
entry.session = session;
|
|
101
|
+
if (entry.failures > 0) {
|
|
102
|
+
logger.info('db.listen.redialed', { channel: entry.channel, failures: entry.failures });
|
|
103
|
+
}
|
|
104
|
+
entry.failures = 0;
|
|
105
|
+
entry.cause = undefined;
|
|
72
106
|
},
|
|
73
107
|
(error: unknown) => {
|
|
74
108
|
if (overtaken()) return;
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
109
|
+
const cause = causeOf(error);
|
|
110
|
+
if (entry.cause?.key === cause.key) entry.failures += 1;
|
|
111
|
+
else {
|
|
112
|
+
const ended = entry.cause;
|
|
113
|
+
logger.warn('db.listen.redial_failed', {
|
|
114
|
+
channel: entry.channel,
|
|
115
|
+
attempt,
|
|
116
|
+
error: renderThrowable(error),
|
|
117
|
+
...(ended === undefined
|
|
118
|
+
? {}
|
|
119
|
+
: { previous: { ...ended.label, failures: entry.failures } }),
|
|
120
|
+
});
|
|
121
|
+
entry.failures = 1;
|
|
122
|
+
entry.cause = cause;
|
|
123
|
+
}
|
|
80
124
|
// Equal jitter: a fleet that lost its database together must not re-dial it together.
|
|
81
125
|
const wait = backoffDelay({
|
|
82
126
|
attempt,
|
|
@@ -100,6 +144,8 @@ export function poolListeners(
|
|
|
100
144
|
dial: 0,
|
|
101
145
|
cancelRetry: () => undefined,
|
|
102
146
|
ended: false,
|
|
147
|
+
failures: 0,
|
|
148
|
+
cause: undefined,
|
|
103
149
|
};
|
|
104
150
|
held.add(entry);
|
|
105
151
|
try {
|