@noego/sqlego 0.1.6-dev.6 → 0.1.6-dev.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/README.md CHANGED
@@ -85,72 +85,25 @@ 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 the file's lease (taken if free or its holder is dead)
89
- │ won │ lost (held by a live client or owner)
88
+ claim <key>.lock (O_EXCL; stale lock of a dead pid is reclaimed)
89
+ │ won │ lost
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
- take the spawn lease ▶ recount owners ▶ spawn `sqlego serve <file>`│
94
+ claim spawn.lock ▶ spawn `sqlego serve <file>` │ │
95
95
  └──────────────────────────▶ connect ◀─────────┴────────────────┘
96
96
 
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
97
+ owner: per file: open db ▶ listen ▶ write <key>.json ▶ release <key>.lock ▶ serve
98
+ file's last client leaves ▶ idle timer (default 30 s) ▶ close db ▶ rm lock+socket+meta
103
99
  file deleted or replaced at its path (checked every 5 s) ▶ same release, clients or not
104
100
  last file released ▶ exit 0
105
101
  ```
106
102
 
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/`)
103
+ Socket, lock, and meta live in `$SQLEGO_RUNTIME_DIR` (default `<tmpdir>/sqlego/`)
140
104
  keyed by a hash of the canonical file path — never beside the database, because
141
105
  Unix socket paths cap at 104 bytes. On Windows the socket is the named pipe
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.
106
+ `\\.\pipe\sqlego-<runtime dir hash>-<key>` (lock and meta stay in the directory).
154
107
 
155
108
  A client whose version differs from the owner's asks it to release the file
156
109
  and places the file anew, so a rebuilt binary never keeps talking to an old
@@ -182,20 +135,12 @@ before owners could adopt. The limit counts every owner of the client's
182
135
  build under the runtime directory, whichever app started it. Nothing is ever
183
136
  refused for lack of a process.
184
137
 
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
-
190
138
  Each file keeps its own socket, database connection, leased transaction and
191
139
  idle timer. `sqlego <file> stop` (refused while other clients use the file)
192
140
  and the idle timeout release that one file; so does deleting the file or its
193
141
  directory. The process exits when its last file is released. Spawning is serialized
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.
142
+ through `spawn.lock` in the runtime directory so 40 files opened at once
143
+ share one process instead of starting 40.
199
144
 
200
145
  ### Pragmas
201
146
 
package/dist/client.d.ts CHANGED
@@ -91,10 +91,8 @@ 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
- /** 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;
94
+ /** True while this client is the one spawning an owner process (see `placeFile`). */
95
+ private holdsSpawnLock;
98
96
  private readonly options;
99
97
  private constructor();
100
98
  static connect(file: string, options?: ClientOptions): Promise<SqlegoClient>;
@@ -203,37 +201,18 @@ export declare class SqlegoClient {
203
201
  */
204
202
  private mismatchesOf;
205
203
  private connectOrSpawn;
206
- /**
207
- * The file's claim: whoever holds it places the file with an owner, which
208
- * then holds it while it serves the file. See `takeClaim`.
209
- */
204
+ /** The file's own claim lock: whoever holds it places the file with an owner. */
210
205
  private claimLock;
211
- /**
212
- * Takes the file's claim lease and, with it, the claim lock older builds
213
- * use (`<key>.lock`, see legacy_lock.ts) so that they see the file claimed
214
- * too. Null when either is held: the lease by a process of this build, the
215
- * lock by a live client of an older build placing the file.
216
- */
217
- private takeClaim;
218
- /**
219
- * Where this client's claim stands. `own` while this client still holds
220
- * it (or holds none); `placed` once a live process holds it instead — the
221
- * owner that took the file on, or another client placing it. An owner that
222
- * took the claim over and then could not serve the file (its open or bind
223
- * failed) released it again: this client takes it back and keeps placing.
224
- */
225
- private followClaim;
226
206
  private ownerBudget;
207
+ /** The runtime directory's spawn lock: at most one client spawns an owner process at a time. */
208
+ private spawnLockPath;
227
209
  /**
228
- * Gets this client's file served, holding the file's claim: by the
210
+ * Gets this client's file served, holding the file's claim lock: by the
229
211
  * running owner process (of this build) serving the fewest files, or by a
230
212
  * new process while fewer than `maxOwners` are running. Spawning is
231
- * serialized through the spawn lease so many files opened at once share
213
+ * serialized through the spawn lock so many files opened at once share
232
214
  * one process instead of each starting their own; a client that loses
233
- * that lease waits for the new owner to appear and adopts into it. The
234
- * owner a client spawns takes both leases over (see `serve` in cli.ts), so
235
- * the reservation lasts until that owner is counted even if this client
236
- * exits first.
215
+ * that lock waits for the new owner to appear and adopts into it.
237
216
  */
238
217
  private placeFile;
239
218
  /**
@@ -244,29 +223,12 @@ export declare class SqlegoClient {
244
223
  * are ignored.
245
224
  */
246
225
  private currentOwners;
247
- /**
248
- * Releases the spawn reservation unless it has moved on: once the spawned
249
- * owner has taken it over, this is a no-op and never frees the owner's.
250
- */
251
226
  private releaseSpawnLock;
252
- /** Connected or given up: whatever this client still holds goes (a no-op for anything an owner took over). */
253
- private settleLeases;
254
227
  /** Waits until nothing answers on the file's socket (the old owner released it, its database closed first), or the deadline. */
255
228
  private waitForRelease;
256
229
  private spawnServer;
257
- /** The spawn never started: record why, and release both locks so another claimer can try. */
230
+ /** The spawn never started: record why, and release the lock so another claimer can try. */
258
231
  private onSpawnError;
259
- /**
260
- * Releases this client's claim unless an owner has taken it over (then this
261
- * is a no-op), or it was released already. First the claim moves to a new
262
- * generation this client holds — one step that fails if an owner took it
263
- * over meanwhile, in which case the older builds' claim lock is the
264
- * owner's and stays. Holding that generation, no owner can take the claim
265
- * over any more, so the lock (only if it names this client) goes before
266
- * the claim is freed: freed first, a client taking it would find the lock
267
- * still naming this live process, back off, and wait for a socket nobody
268
- * starts.
269
- */
270
232
  private releaseLock;
271
233
  private attach;
272
234
  private onClose;