@noego/sqlego 0.1.6-dev.0 → 0.1.6-dev.10
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/README.md +67 -10
- package/dist/client.d.ts +57 -9
- package/dist/index.js +488 -86
- package/dist/lease.d.ts +39 -0
- package/dist/legacy_lock.d.ts +20 -0
- package/dist/paths.d.ts +6 -0
- package/dist/protocol.d.ts +28 -2
- package/dist/version.d.ts +17 -3
- package/package.json +9 -8
- package/src/cli.ts +121 -2
- package/src/client.ts +188 -85
- package/src/lease.ts +265 -0
- package/src/legacy_lock.ts +123 -0
- package/src/paths.ts +4 -0
- package/src/protocol.ts +56 -6
- package/src/server.ts +355 -30
- package/src/version.ts +75 -7
package/README.md
CHANGED
|
@@ -85,25 +85,72 @@ installed; otherwise set `SQLEGO_BIN` to a binary you bundle.
|
|
|
85
85
|
client: connect(socket) ──ok──▶ hello ──▶ requests
|
|
86
86
|
│ refused/absent
|
|
87
87
|
▼
|
|
88
|
-
claim
|
|
89
|
-
│ won │ lost
|
|
88
|
+
claim the file's lease (taken if free or its holder is dead)
|
|
89
|
+
│ won │ lost (held by a live client or owner)
|
|
90
90
|
▼ ▼
|
|
91
91
|
an owner of this build running? ──yes──▶ `adopt <file>` wait for socket to appear
|
|
92
92
|
│ no (or fewer than maxOwners) │ │
|
|
93
93
|
▼ │ │
|
|
94
|
-
|
|
94
|
+
take the spawn lease ▶ recount owners ▶ spawn `sqlego serve <file>`│
|
|
95
95
|
└──────────────────────────▶ connect ◀─────────┴────────────────┘
|
|
96
96
|
|
|
97
|
-
owner:
|
|
98
|
-
|
|
97
|
+
owner: spawned: take the file's lease and the spawn lease over from its spawner
|
|
98
|
+
(refuse the file if either was taken by anyone else first)
|
|
99
|
+
adopting: take the file's lease over from the asking client (refuse likewise)
|
|
100
|
+
per file: open db ▶ write <key>.json ▶ listen ▶ serve, holding the file's lease
|
|
101
|
+
first file listening ▶ release the spawn lease
|
|
102
|
+
file's last client leaves ▶ idle timer (default 30 s) ▶ close db ▶ release lease ▶ rm socket+meta
|
|
99
103
|
file deleted or replaced at its path (checked every 5 s) ▶ same release, clients or not
|
|
100
104
|
last file released ▶ exit 0
|
|
101
105
|
```
|
|
102
106
|
|
|
103
|
-
|
|
107
|
+
A lease (`src/lease.ts`) is a chain of generation files `<name>.<g>`, each
|
|
108
|
+
created once with `link()`, which fails if that generation exists. Taking a
|
|
109
|
+
free lease, reclaiming a dead holder's, handing one to another process and
|
|
110
|
+
releasing one each create the next generation, so of all the processes acting
|
|
111
|
+
on one generation exactly one succeeds: an owner taking its dead spawner's
|
|
112
|
+
lease over and a client reclaiming that lease can never both win, and a late
|
|
113
|
+
release by a client the lease has moved on from changes nothing. Each
|
|
114
|
+
generation also carries a unique token that a release or handover must name,
|
|
115
|
+
so if the newest generation file is lost and numbering starts again, a holder
|
|
116
|
+
of the old chain cannot act on the new one.
|
|
117
|
+
|
|
118
|
+
Builds before leases claim a file with `<key>.lock` alone (created
|
|
119
|
+
exclusively, holding the claimer's pid). So that they see a file this build
|
|
120
|
+
is placing or serving as claimed, whoever holds its lease also holds that
|
|
121
|
+
lock in the same format, naming itself: a client takes it with the lease, an
|
|
122
|
+
owner moves it to itself when it takes the claim over, and it is released
|
|
123
|
+
with the lease. A client of this build that finds it held by a live process
|
|
124
|
+
treats the file as claimed; an owner that finds it held by a live process
|
|
125
|
+
other than the one that handed it the claim refuses the file.
|
|
126
|
+
|
|
127
|
+
**Known limitation of running mixed builds.** An older build's client
|
|
128
|
+
reclaims a `<key>.lock` naming a dead process by reading the pid and then
|
|
129
|
+
removing the file, with nothing in between that this build can see. If the
|
|
130
|
+
client that spawned an owner of this build dies before the owner takes its
|
|
131
|
+
claim over, and an older client is reclaiming that same lock at that
|
|
132
|
+
moment, the older client can remove the lock the new owner has just taken
|
|
133
|
+
and place the file with a second owner. It needs all three at once — a
|
|
134
|
+
pre-lease client on the same file, a spawner dying mid-spawn, and the older
|
|
135
|
+
client paused between its read and its removal — and it is the same race
|
|
136
|
+
older builds already have between two of their own clients reclaiming one
|
|
137
|
+
dead lock. Upgrading every process that opens a file removes it.
|
|
138
|
+
|
|
139
|
+
Socket, leases, and meta live in `$SQLEGO_RUNTIME_DIR` (default `<tmpdir>/sqlego/`)
|
|
104
140
|
keyed by a hash of the canonical file path — never beside the database, because
|
|
105
141
|
Unix socket paths cap at 104 bytes. On Windows the socket is the named pipe
|
|
106
|
-
`\\.\pipe\sqlego-<runtime dir hash>-<key>` (
|
|
142
|
+
`\\.\pipe\sqlego-<runtime dir hash>-<key>` (leases and meta stay in the directory).
|
|
143
|
+
|
|
144
|
+
**Do not clean the runtime directory while sqlego processes run.** Deleting
|
|
145
|
+
a live socket, meta or lease file from outside (a cleanup script, `rm -rf`
|
|
146
|
+
of the directory) is not supported: the exclusion between clients and
|
|
147
|
+
owners rests on those files, and a lease whose newest file is deleted can
|
|
148
|
+
admit a second holder. Age-based temp cleaners (macOS's, systemd-tmpfiles)
|
|
149
|
+
are covered: a running owner refreshes the access and modification times of
|
|
150
|
+
the files it holds every hour (`runtimeRefreshMs`), so they never look
|
|
151
|
+
stale. Point `SQLEGO_RUNTIME_DIR` outside the temp directory to keep even
|
|
152
|
+
idle leftovers (one small lease file per database opened) out of a cleaner's
|
|
153
|
+
reach. Clearing the directory with no sqlego process running is safe.
|
|
107
154
|
|
|
108
155
|
A client whose version differs from the owner's asks it to release the file
|
|
109
156
|
and places the file anew, so a rebuilt binary never keeps talking to an old
|
|
@@ -135,12 +182,20 @@ before owners could adopt. The limit counts every owner of the client's
|
|
|
135
182
|
build under the runtime directory, whichever app started it. Nothing is ever
|
|
136
183
|
refused for lack of a process.
|
|
137
184
|
|
|
185
|
+
The budget is per build, by design: a client never adopts into an owner of
|
|
186
|
+
another build (see above), so owners of another build are not counted
|
|
187
|
+
against it. Two builds sharing a runtime directory can therefore run up to
|
|
188
|
+
`maxOwners` owners each — with `maxOwners: 1`, one owner per build.
|
|
189
|
+
|
|
138
190
|
Each file keeps its own socket, database connection, leased transaction and
|
|
139
191
|
idle timer. `sqlego <file> stop` (refused while other clients use the file)
|
|
140
192
|
and the idle timeout release that one file; so does deleting the file or its
|
|
141
193
|
directory. The process exits when its last file is released. Spawning is serialized
|
|
142
|
-
through
|
|
143
|
-
share one process instead of starting 40.
|
|
194
|
+
through the spawn lease in the runtime directory so 40 files opened at once
|
|
195
|
+
share one process instead of starting 40. The spawned owner holds that lease
|
|
196
|
+
until it is counted: its meta is written before its socket is bound, and a
|
|
197
|
+
client counts owners again once it holds the lease, so neither an owner a
|
|
198
|
+
client can already reach nor one still starting is missed by the next spawner.
|
|
144
199
|
|
|
145
200
|
### Pragmas
|
|
146
201
|
|
|
@@ -366,7 +421,9 @@ bun scripts/release.ts --channel dev --pack ./out # build + stage + check all
|
|
|
366
421
|
|
|
367
422
|
`bun run build` runs the fresh binary with `--version` and fails unless it
|
|
368
423
|
reports `<package.json version>+<build id>`, so a stale binary cannot be
|
|
369
|
-
mistaken for the current one.
|
|
424
|
+
mistaken for the current one. The build id hashes `src/*.ts` and the Rust
|
|
425
|
+
owner's inputs (`rust/Cargo.toml`, `rust/Cargo.lock`, `rust/build.rs`, every
|
|
426
|
+
file under `rust/src/`), so a change on either side is a new build.
|
|
370
427
|
|
|
371
428
|
### Releasing
|
|
372
429
|
|
package/dist/client.d.ts
CHANGED
|
@@ -91,8 +91,10 @@ export declare class SqlegoClient {
|
|
|
91
91
|
private mismatches;
|
|
92
92
|
/** Why the server this client spawned did not come up, if it did not. */
|
|
93
93
|
private spawnFailure;
|
|
94
|
-
/**
|
|
95
|
-
private
|
|
94
|
+
/** The file's claim while this client holds it (see `claimLock`); an owner that takes the file on takes it over. */
|
|
95
|
+
private claim;
|
|
96
|
+
/** The spawn reservation while this client holds it (see `placeFile`); the owner it spawns takes it over. */
|
|
97
|
+
private spawnLease;
|
|
96
98
|
private readonly options;
|
|
97
99
|
private constructor();
|
|
98
100
|
static connect(file: string, options?: ClientOptions): Promise<SqlegoClient>;
|
|
@@ -201,18 +203,47 @@ export declare class SqlegoClient {
|
|
|
201
203
|
*/
|
|
202
204
|
private mismatchesOf;
|
|
203
205
|
private connectOrSpawn;
|
|
204
|
-
/**
|
|
206
|
+
/** How often a client waiting on another process's claim checks whether that claim was freed. */
|
|
207
|
+
private static readonly CLAIM_RECHECK_MS;
|
|
208
|
+
/**
|
|
209
|
+
* Waits for the file's socket until `deadline`. A client that found the
|
|
210
|
+
* claim held by another process (`watchClaim`) also watches that claim:
|
|
211
|
+
* an owner that is stopping removes its socket and meta while it still
|
|
212
|
+
* holds the claim and frees it last, so once the claim is free nothing
|
|
213
|
+
* will listen unless this client places the file itself (ok, null).
|
|
214
|
+
*/
|
|
215
|
+
private waitForSocket;
|
|
216
|
+
/**
|
|
217
|
+
* The file's claim: whoever holds it places the file with an owner, which
|
|
218
|
+
* then holds it while it serves the file. See `takeClaim`.
|
|
219
|
+
*/
|
|
205
220
|
private claimLock;
|
|
221
|
+
/**
|
|
222
|
+
* Takes the file's claim lease and, with it, the claim lock older builds
|
|
223
|
+
* use (`<key>.lock`, see legacy_lock.ts) so that they see the file claimed
|
|
224
|
+
* too. Null when either is held: the lease by a process of this build, the
|
|
225
|
+
* lock by a live client of an older build placing the file.
|
|
226
|
+
*/
|
|
227
|
+
private takeClaim;
|
|
228
|
+
/**
|
|
229
|
+
* Where this client's claim stands. `own` while this client still holds
|
|
230
|
+
* it (or holds none); `placed` once a live process holds it instead — the
|
|
231
|
+
* owner that took the file on, or another client placing it. An owner that
|
|
232
|
+
* took the claim over and then could not serve the file (its open or bind
|
|
233
|
+
* failed) released it again: this client takes it back and keeps placing.
|
|
234
|
+
*/
|
|
235
|
+
private followClaim;
|
|
206
236
|
private ownerBudget;
|
|
207
|
-
/** The runtime directory's spawn lock: at most one client spawns an owner process at a time. */
|
|
208
|
-
private spawnLockPath;
|
|
209
237
|
/**
|
|
210
|
-
* Gets this client's file served, holding the file's claim
|
|
238
|
+
* Gets this client's file served, holding the file's claim: by the
|
|
211
239
|
* running owner process (of this build) serving the fewest files, or by a
|
|
212
240
|
* new process while fewer than `maxOwners` are running. Spawning is
|
|
213
|
-
* serialized through the spawn
|
|
241
|
+
* serialized through the spawn lease so many files opened at once share
|
|
214
242
|
* one process instead of each starting their own; a client that loses
|
|
215
|
-
* that
|
|
243
|
+
* that lease waits for the new owner to appear and adopts into it. The
|
|
244
|
+
* owner a client spawns takes both leases over (see `serve` in cli.ts), so
|
|
245
|
+
* the reservation lasts until that owner is counted even if this client
|
|
246
|
+
* exits first.
|
|
216
247
|
*/
|
|
217
248
|
private placeFile;
|
|
218
249
|
/**
|
|
@@ -223,12 +254,29 @@ export declare class SqlegoClient {
|
|
|
223
254
|
* are ignored.
|
|
224
255
|
*/
|
|
225
256
|
private currentOwners;
|
|
257
|
+
/**
|
|
258
|
+
* Releases the spawn reservation unless it has moved on: once the spawned
|
|
259
|
+
* owner has taken it over, this is a no-op and never frees the owner's.
|
|
260
|
+
*/
|
|
226
261
|
private releaseSpawnLock;
|
|
262
|
+
/** Connected or given up: whatever this client still holds goes (a no-op for anything an owner took over). */
|
|
263
|
+
private settleLeases;
|
|
227
264
|
/** Waits until nothing answers on the file's socket (the old owner released it, its database closed first), or the deadline. */
|
|
228
265
|
private waitForRelease;
|
|
229
266
|
private spawnServer;
|
|
230
|
-
/** The spawn never started: record why, and release
|
|
267
|
+
/** The spawn never started: record why, and release both locks so another claimer can try. */
|
|
231
268
|
private onSpawnError;
|
|
269
|
+
/**
|
|
270
|
+
* Releases this client's claim unless an owner has taken it over (then this
|
|
271
|
+
* is a no-op), or it was released already. First the claim moves to a new
|
|
272
|
+
* generation this client holds — one step that fails if an owner took it
|
|
273
|
+
* over meanwhile, in which case the older builds' claim lock is the
|
|
274
|
+
* owner's and stays. Holding that generation, no owner can take the claim
|
|
275
|
+
* over any more, so the lock (only if it names this client) goes before
|
|
276
|
+
* the claim is freed: freed first, a client taking it would find the lock
|
|
277
|
+
* still naming this live process, back off, and wait for a socket nobody
|
|
278
|
+
* starts.
|
|
279
|
+
*/
|
|
232
280
|
private releaseLock;
|
|
233
281
|
private attach;
|
|
234
282
|
private onClose;
|