@zukhruf/election 0.8.41 → 0.8.42

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.
Files changed (2) hide show
  1. package/README.md +83 -0
  2. package/package.json +6 -6
package/README.md CHANGED
@@ -215,6 +215,89 @@ The socket lock store of `@zukhruf/mutex` uses `SqliteElection`, with the claim
215
215
  | `LeaseLostError` | The reason of `term.signal` when the backend took the claim away. Its `subject` is `'leadership'`, and its `cause` is the backend's reason. It is the class of `@zukhruf/lease`, and this package exports it too. |
216
216
  | `signal.reason` | A campaign rejects with it when its signal aborts. |
217
217
 
218
+ ## API
219
+
220
+ ```ts
221
+ interface CampaignOptions {
222
+ timeout?: number | undefined; // default: one try; Infinity: until it wins or signal aborts
223
+ signal?: AbortSignal | undefined;
224
+ }
225
+
226
+ abstract class LeaderElection<Claim> {
227
+ protected readonly clock: Clock;
228
+ protected constructor(clock: Clock);
229
+ campaign(options?: CampaignOptions): Promise<Term | undefined>;
230
+ protected abstract open(signal: AbortSignal | undefined): Promise<Claim>;
231
+ protected abstract tryClaim(
232
+ claim: Claim,
233
+ signal: AbortSignal | undefined,
234
+ ): Promise<Pick<Term, 'epoch' | 'afterCleanShutdown'> | undefined>;
235
+ protected abstract waitForChange(
236
+ claim: Claim,
237
+ signal: AbortSignal,
238
+ ): Promise<void>;
239
+ protected abstract watch(
240
+ claim: Claim,
241
+ lose: (reason: Error) => void,
242
+ ): Disposable;
243
+ protected abstract recordCleanShutdown(
244
+ claim: Claim,
245
+ epoch: bigint,
246
+ ): Promise<void>;
247
+ protected abstract release(claim: Claim): Promise<void>;
248
+ protected abstract close(claim: Claim): Promise<void>;
249
+ }
250
+
251
+ interface ResignOptions {
252
+ clean: boolean;
253
+ }
254
+
255
+ // The steps of the backend for the claim that a term won.
256
+ interface TermSteps {
257
+ watch(lose: (reason: Error) => void): Disposable;
258
+ recordCleanShutdown(): Promise<void>; // only for a term that was not lost, before release
259
+ release(): Promise<void>; // only for a term that was not lost
260
+ close(): Promise<void>; // at each end
261
+ }
262
+
263
+ class Term implements Lease, AsyncDisposable {
264
+ constructor(
265
+ { epoch, afterCleanShutdown }: Pick<Term, 'epoch' | 'afterCleanShutdown'>,
266
+ steps: TermSteps,
267
+ );
268
+ readonly epoch: bigint;
269
+ readonly afterCleanShutdown: boolean;
270
+ get signal(): AbortSignal; // aborts with LeaseLostError when the term is lost
271
+ resign(options?: ResignOptions): Promise<void>;
272
+ [Symbol.asyncDispose](): Promise<void>; // resign()
273
+ }
274
+
275
+ interface SqliteElectionOptions {
276
+ directory: string;
277
+ claimFile: string;
278
+ epochFile: string;
279
+ leaseDuration?: number | undefined; // default: 15000
280
+ heartbeatInterval?: number | undefined; // default: leaseDuration / 3
281
+ clock: Clock;
282
+ }
283
+
284
+ class SqliteElection extends LeaderElection<SqliteAttempt> {
285
+ constructor(options: SqliteElectionOptions); // RangeError: unless 0 < heartbeatInterval < leaseDuration
286
+ lastEpoch(): Promise<bigint>;
287
+ observe(): LeaderObserver;
288
+ }
289
+
290
+ class LeaderObserver implements Disposable {
291
+ get left(): Promise<'closed' | 'ended' | 'absent'>;
292
+ [Symbol.dispose](): void;
293
+ }
294
+
295
+ export { NetworkDirectoryError } from '@zukhruf/fs';
296
+ export { LeaseLostError } from '@zukhruf/lease';
297
+ ```
298
+
299
+ `campaign` builds the `Term` of a claim that it won, and gives it the backend's steps for that claim as `TermSteps` ([src/leader-election.ts](./src/leader-election.ts)). The term calls `watch` when it begins. When the leader resigns, it calls `recordCleanShutdown` (only with `clean: true`), `release` and `close`. After a loss, it calls only `close` ([src/term.ts](./src/term.ts)). `LeaderObserver` is exported as a type: `observe()` makes one.
300
+
218
301
  ## Documentation
219
302
 
220
303
  - [How SqliteElection elects a leader](./docs/concepts/leader-election.md)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zukhruf/election",
3
- "version": "0.8.41",
3
+ "version": "0.8.42",
4
4
  "description": "Elects a leader among candidates. Each term has a higher epoch and ends when the leader resigns or dies. Backends subclass one campaign. `SqliteElection` elects among processes of one host; candidates and observers end a leader whose pipe heartbeats stop.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -40,13 +40,13 @@
40
40
  "access": "public"
41
41
  },
42
42
  "dependencies": {
43
- "@zukhruf/async": "^0.8.41",
44
- "@zukhruf/fs": "^0.8.41",
45
- "@zukhruf/lease": "^0.8.41",
46
- "@zukhruf/pipes": "^0.8.41"
43
+ "@zukhruf/async": "^0.8.42",
44
+ "@zukhruf/fs": "^0.8.42",
45
+ "@zukhruf/lease": "^0.8.42",
46
+ "@zukhruf/pipes": "^0.8.42"
47
47
  },
48
48
  "devDependencies": {
49
49
  "@sinonjs/fake-timers": "15.4.0",
50
- "@zukhruf/testing": "^0.8.41"
50
+ "@zukhruf/testing": "^0.8.42"
51
51
  }
52
52
  }