@zukhruf/election 0.8.232 → 0.8.234
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 +14 -13
- package/dist/clean-up-after.d.ts +9 -0
- package/dist/clean-up-after.d.ts.map +1 -0
- package/dist/clean-up-after.js +26 -0
- package/dist/clean-up-after.js.map +1 -0
- package/dist/leader-selector.d.ts.map +1 -1
- package/dist/leader-selector.js +2 -14
- package/dist/leader-selector.js.map +1 -1
- package/dist/term.d.ts +2 -1
- package/dist/term.d.ts.map +1 -1
- package/dist/term.js +9 -14
- package/dist/term.js.map +1 -1
- package/package.json +8 -8
package/README.md
CHANGED
|
@@ -96,7 +96,7 @@ await using term = await election.campaign({ timeout: Infinity, signal });
|
|
|
96
96
|
A term is a lease on leadership. `Term` implements the `Lease` interface of [`@zukhruf/lease`](../lease/README.md), so a function that takes a `Lease` also takes a term. See the glossary of [leases](../lease/CONTEXT.md).
|
|
97
97
|
|
|
98
98
|
- `term.epoch` is the number of the term. It is higher than the epoch of each earlier term of the election. The epoch of a `SqliteElection` is below 2^31.
|
|
99
|
-
- `term.resign()` ends the term and gives the claim up. A second call waits for the first, and its options do nothing. `await using` resigns at the end of its scope. When the release fails, the claim is still freed, and `resign` rejects with the release's error. When the release or the record of `clean` fails and freeing the claim fails too, `resign` rejects with a `SuppressedError`: its `suppressed` is that step's error, and its `error` is the failure to free. After a loss, `resign` never rejects, and the term still ends: a failure to stop watching the claim or to free it has no caller to go to, so it is a `TermWarning` (see [Errors](#errors)). When both fail, the warning's `cause` is a `SuppressedError`: its `suppressed` is the failure to stop watching, and its `error` is the failure to free.
|
|
99
|
+
- `term.resign()` ends the term and gives the claim up. A second call waits for the first, and its options do nothing. `await using` resigns at the end of its scope. When the release fails, the claim is still freed, and `resign` rejects with the release's error. When the release or the record of `clean` fails and freeing the claim fails too, `resign` rejects with a `SuppressedError`: its `suppressed` is that step's error, and its `error` is the failure to free. When both fail with one error, `resign` rejects with that error ([ADR 0013](./docs/adr/0013-a-term-whose-two-steps-fail-with-one-error-ends-with-that-error.md)). After a loss, `resign` never rejects, and the term still ends: a failure to stop watching the claim or to free it has no caller to go to, so it is a `TermWarning` (see [Errors](#errors)). When both fail, the warning's `cause` is a `SuppressedError`: its `suppressed` is the failure to stop watching, and its `error` is the failure to free. When both fail with one error, the `cause` is that error.
|
|
100
100
|
- `term.resign({ clean: true })` records a clean shutdown for the next term, and then ends the term. Use it only when the leader leaves nothing for the next term to recover, for example no work in progress. A lost term records nothing, also with `clean`. When the record fails, the claim is freed, and `resign` rejects with that error. The socket lock store of `@zukhruf/mutex` resigns clean when no process holds a key ([its ADR 0018](../mutex/docs/adr/0018-the-next-leader-skips-the-grace-window-after-a-clean-shutdown.md)). `@zukhruf/single-flight` no longer records a clean shutdown, because its coordinator has no grace window to skip ([its ADR 0010](../single-flight/docs/adr/0010-a-leader-holds-a-kernel-lock-on-its-key-until-its-outcome-is-handed-over.md)).
|
|
101
101
|
- `term.afterCleanShutdown` is `true` only when the term just before this one resigned clean. Then the new leader knows that it has nothing to recover. It is `false` after a crash, after a lost term, after a resign without `clean`, and for the first term.
|
|
102
102
|
- `term.signal` aborts with `LeaseLostError` when the term is lost while its leader still runs. The `subject` of the error is `'leadership'`, and its `cause` is the reason of the backend. The signal does not abort when the leader resigns.
|
|
@@ -299,21 +299,21 @@ The socket lock store of `@zukhruf/mutex` uses `SqliteElection`, with the claim
|
|
|
299
299
|
|
|
300
300
|
## Errors
|
|
301
301
|
|
|
302
|
-
| Error | When
|
|
303
|
-
| ------------------------------ |
|
|
304
|
-
| `NetworkDirectoryError` | The directory of a `SqliteElection` is on a network file system. It is the class of `@zukhruf/fs`.
|
|
305
|
-
| `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.
|
|
306
|
-
| `signal.reason` | A campaign rejects with it when its signal aborts.
|
|
307
|
-
| `SuppressedError` | A campaign or a resign rejects with it when a step failed and giving the claim up or freeing it failed too. Its `suppressed` is the step's error, and its `error` is the failure to free. A `LeaderSelector` ends with it when its task failed and the resign failed with another error. |
|
|
308
|
-
| `RangeError` | A campaign of a `SqliteElection` rejects with it when the next epoch would be 2^31 or more. The message names `epochFile`.
|
|
309
|
-
| The failure of a leader's task | `ended` and the dispose of a `LeaderSelector` reject with the error of a task that failed, of a resign that failed, and, without `autoRequeue`, of a failed campaign. `requeue()` throws an `Error` after the dispose, and after a failure ended the selector.
|
|
302
|
+
| Error | When |
|
|
303
|
+
| ------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
304
|
+
| `NetworkDirectoryError` | The directory of a `SqliteElection` is on a network file system. It is the class of `@zukhruf/fs`. |
|
|
305
|
+
| `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. |
|
|
306
|
+
| `signal.reason` | A campaign rejects with it when its signal aborts. |
|
|
307
|
+
| `SuppressedError` | A campaign or a resign rejects with it when a step failed and giving the claim up or freeing it failed too. Its `suppressed` is the step's error, and its `error` is the failure to free. A resign whose two steps fail with one error rejects with that error instead. A `LeaderSelector` ends with it when its task failed and the resign failed with another error. |
|
|
308
|
+
| `RangeError` | A campaign of a `SqliteElection` rejects with it when the next epoch would be 2^31 or more. The message names `epochFile`. |
|
|
309
|
+
| The failure of a leader's task | `ended` and the dispose of a `LeaderSelector` reject with the error of a task that failed, of a resign that failed, and, without `autoRequeue`, of a failed campaign. `requeue()` throws an `Error` after the dispose, and after a failure ended the selector. |
|
|
310
310
|
|
|
311
311
|
A failure that no caller waits for is a process warning, in the shape of [root ADR 0010](../../docs/adr/0010-a-package-warning-names-its-type-code-and-subject-and-keeps-the-failure-as-its-cause.md). A `'warning'` listener reads its type as `name`, its `code`, and its subject in `detail`, one `<term>: <value>` line each. Its `cause` is the failure, and its message ends with the failure's message. `--disable-warning=<code or type>` stops the print. `TermWarning` and `LeaderSelectorWarning` are classes that the package exports, so a listener can test `warning instanceof` the class ([root ADR 0011](../../docs/adr/0011-a-package-warning-is-an-instance-of-its-own-class-which-the-package-exports.md)). With two installed copies of the package, `instanceof` fails across the copies, so compare `code`.
|
|
312
312
|
|
|
313
|
-
| Warning | Code | Detail | When
|
|
314
|
-
| ----------------------- | -------------------------------------------------- | --------------------------------------- |
|
|
315
|
-
| `TermWarning` | `ZUKHRUF_ELECTION_TERM_FREE_FAILED` | `election: <name>` and `epoch: <epoch>` | A lost term fails to stop watching its claim or to free it. The term still ends. When both fail, the `cause` is a `SuppressedError`: its `suppressed` is the failure to stop watching, and its `error` is the failure to free. |
|
|
316
|
-
| `LeaderSelectorWarning` | `ZUKHRUF_ELECTION_LEADER_SELECTOR_CAMPAIGN_FAILED` | `election: <name>` | A campaign of a `LeaderSelector` with `autoRequeue` fails. The selector waits `backoff.duration(retries)`, and campaigns again.
|
|
313
|
+
| Warning | Code | Detail | When |
|
|
314
|
+
| ----------------------- | -------------------------------------------------- | --------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
315
|
+
| `TermWarning` | `ZUKHRUF_ELECTION_TERM_FREE_FAILED` | `election: <name>` and `epoch: <epoch>` | A lost term fails to stop watching its claim or to free it. The term still ends. When both fail, the `cause` is a `SuppressedError`: its `suppressed` is the failure to stop watching, and its `error` is the failure to free; when both fail with one error, the `cause` is that error. |
|
|
316
|
+
| `LeaderSelectorWarning` | `ZUKHRUF_ELECTION_LEADER_SELECTOR_CAMPAIGN_FAILED` | `election: <name>` | A campaign of a `LeaderSelector` with `autoRequeue` fails. The selector waits `backoff.duration(retries)`, and campaigns again. |
|
|
317
317
|
|
|
318
318
|
## API
|
|
319
319
|
|
|
@@ -454,6 +454,7 @@ export { LeaseLostError } from '@zukhruf/lease';
|
|
|
454
454
|
- [ADR 0010: An election has a name, and the name of a SQLite election is the path of its claim file](./docs/adr/0010-an-election-has-a-name-and-the-name-of-a-sqlite-election-is-the-path-of-its-claim-file.md)
|
|
455
455
|
- [ADR 0011: A leader selector runs a task each time it wins a term of a leader election, over a `LeaderElection` only](./docs/adr/0011-a-leader-selector-runs-a-task-each-time-it-wins-a-term-of-a-leader-election.md)
|
|
456
456
|
- [ADR 0012: A task failure and a resign failure that are one error end the leader selector with that error](./docs/adr/0012-a-task-failure-and-a-resign-failure-that-are-one-error-end-the-leader-selector-with-that-error.md)
|
|
457
|
+
- [ADR 0013: A term whose two steps fail with one error ends with that error](./docs/adr/0013-a-term-whose-two-steps-fail-with-one-error-ends-with-that-error.md)
|
|
457
458
|
- [Code copied into this package](./docs/copied-code.md)
|
|
458
459
|
|
|
459
460
|
## Development
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runs `body`, then `cleanup`, as `await using` runs a scope and then its
|
|
3
|
+
* disposal. When both fail with two errors, it rejects with a
|
|
4
|
+
* `SuppressedError` of the failure of `cleanup` over the failure of `body`,
|
|
5
|
+
* with `message`. When both fail with one error, it rejects with that error:
|
|
6
|
+
* `await using` would suppress it under itself (ADR 0012).
|
|
7
|
+
*/
|
|
8
|
+
export declare function cleanUpAfter(body: () => Promise<void> | void, cleanup: () => Promise<void>, message: string): Promise<void>;
|
|
9
|
+
//# sourceMappingURL=clean-up-after.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"clean-up-after.d.ts","sourceRoot":"","sources":["../src/clean-up-after.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,wBAAsB,YAAY,CAChC,IAAI,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,EAChC,OAAO,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,EAC5B,OAAO,EAAE,MAAM,GACd,OAAO,CAAC,IAAI,CAAC,CAef"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runs `body`, then `cleanup`, as `await using` runs a scope and then its
|
|
3
|
+
* disposal. When both fail with two errors, it rejects with a
|
|
4
|
+
* `SuppressedError` of the failure of `cleanup` over the failure of `body`,
|
|
5
|
+
* with `message`. When both fail with one error, it rejects with that error:
|
|
6
|
+
* `await using` would suppress it under itself (ADR 0012).
|
|
7
|
+
*/
|
|
8
|
+
export async function cleanUpAfter(body, cleanup, message) {
|
|
9
|
+
try {
|
|
10
|
+
await body();
|
|
11
|
+
}
|
|
12
|
+
catch (failure) {
|
|
13
|
+
try {
|
|
14
|
+
await cleanup();
|
|
15
|
+
}
|
|
16
|
+
catch (cleanupFailure) {
|
|
17
|
+
if (cleanupFailure === failure) {
|
|
18
|
+
throw failure;
|
|
19
|
+
}
|
|
20
|
+
throw new SuppressedError(cleanupFailure, failure, message);
|
|
21
|
+
}
|
|
22
|
+
throw failure;
|
|
23
|
+
}
|
|
24
|
+
await cleanup();
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=clean-up-after.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"clean-up-after.js","sourceRoot":"","sources":["../src/clean-up-after.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,YAAY,CAChC,IAAgC,EAChC,OAA4B,EAC5B,OAAe;IAEf,IAAI,CAAC;QACH,MAAM,IAAI,EAAE,CAAC;IACf,CAAC;IAAC,OAAO,OAAO,EAAE,CAAC;QACjB,IAAI,CAAC;YACH,MAAM,OAAO,EAAE,CAAC;QAClB,CAAC;QAAC,OAAO,cAAc,EAAE,CAAC;YACxB,IAAI,cAAc,KAAK,OAAO,EAAE,CAAC;gBAC/B,MAAM,OAAO,CAAC;YAChB,CAAC;YACD,MAAM,IAAI,eAAe,CAAC,cAAc,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC;QAC9D,CAAC;QACD,MAAM,OAAO,CAAC;IAChB,CAAC;IACD,MAAM,OAAO,EAAE,CAAC;AAClB,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"leader-selector.d.ts","sourceRoot":"","sources":["../src/leader-selector.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,OAAO,EACZ,KAAK,KAAK,EAIX,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"leader-selector.d.ts","sourceRoot":"","sources":["../src/leader-selector.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,OAAO,EACZ,KAAK,KAAK,EAIX,MAAM,gBAAgB,CAAC;AAGxB,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAE3D,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEtC,MAAM,WAAW,sBAAsB;IACrC,yEAAyE;IACzE,cAAc,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAChE;AAED,MAAM,WAAW,qBAAqB;IACpC,0EAA0E;IAC1E,WAAW,CAAC,EAAE;QAAE,OAAO,EAAE,OAAO,CAAC;QAAC,KAAK,EAAE,KAAK,CAAA;KAAE,GAAG,SAAS,CAAC;CAC9D;AAED;;;;;;GAMG;AACH,qBAAa,cAAe,YAAW,eAAe;;IAQpD,OAAO,eASN;IAED,yFAAyF;IACzF,MAAM,CAAC,KAAK,CACV,QAAQ,EAAE,cAAc,CAAC,OAAO,CAAC,EACjC,QAAQ,EAAE,sBAAsB,EAChC,EAAE,WAAW,EAAE,GAAE,qBAA0B,GAC1C,cAAc,CAQhB;IAED,wCAAwC;IACxC,IAAI,aAAa,IAAI,OAAO,CAE3B;IAED,qFAAqF;IACrF,IAAI,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAEzB;IAED,qIAAqI;IACrI,OAAO,IAAI,OAAO,CASjB;IAED,4FAA4F;IAC5F,mBAAmB,IAAI,IAAI,CAE1B;IAED,qFAAqF;IACrF,CAAC,MAAM,CAAC,YAAY,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC,CAGrC;CAoEF"}
|
package/dist/leader-selector.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { BackgroundService, Latch, delay, untilAborted, } from '@zukhruf/async';
|
|
2
|
+
import { cleanUpAfter } from './clean-up-after.js';
|
|
2
3
|
import { LeaderSelectorWarning } from './leader-selector-warning.js';
|
|
3
4
|
/**
|
|
4
5
|
* Runs a task each time this process wins a term of an election, and
|
|
@@ -95,20 +96,7 @@ export class LeaderSelector {
|
|
|
95
96
|
* resign fails with the task's own failure, it rejects with that failure.
|
|
96
97
|
*/
|
|
97
98
|
async #lead(term, stop) {
|
|
98
|
-
|
|
99
|
-
await this.#task(term, stop);
|
|
100
|
-
}
|
|
101
|
-
catch (failure) {
|
|
102
|
-
await term.resign().catch((resignFailure) => {
|
|
103
|
-
// `await using` would suppress the one failure under itself.
|
|
104
|
-
if (resignFailure === failure) {
|
|
105
|
-
throw failure;
|
|
106
|
-
}
|
|
107
|
-
throw new SuppressedError(resignFailure, failure, 'The task failed, and its term failed to resign.');
|
|
108
|
-
});
|
|
109
|
-
throw failure;
|
|
110
|
-
}
|
|
111
|
-
await term.resign();
|
|
99
|
+
await cleanUpAfter(() => this.#task(term, stop), () => term.resign(), 'The task failed, and its term failed to resign.');
|
|
112
100
|
}
|
|
113
101
|
/**
|
|
114
102
|
* Runs the task. Its signal aborts at the dispose, at
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"leader-selector.js","sourceRoot":"","sources":["../src/leader-selector.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iBAAiB,EAGjB,KAAK,EACL,KAAK,EACL,YAAY,GACb,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"leader-selector.js","sourceRoot":"","sources":["../src/leader-selector.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iBAAiB,EAGjB,KAAK,E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AM;CAAG;AAEf,MAAM,MAAM,GAAG,IAAI,MAAM,EAAE,CAAC;AAE5B,wDAAwD;AACxD,MAAM,OAAO;IACF,SAAS,CAAkB;IAEpC,YAAY,SAA0B;QACpC,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;IAC7B,CAAC;CACF;AAED,qGAAqG;AACrG,MAAM,IAAI;IACC,QAAQ,GAAG,IAAI,KAAK,EAAE,CAAC;CACjC;AAED,oFAAoF;AACpF,MAAM,KAAK;CAAG;AAEd,MAAM,KAAK,GAAG,IAAI,KAAK,EAAE,CAAC"}
|
package/dist/term.d.ts
CHANGED
|
@@ -47,7 +47,8 @@ export declare class Term implements Lease, AsyncDisposable {
|
|
|
47
47
|
* clean shutdown for the next term. A second call waits for the first, and
|
|
48
48
|
* its options do nothing. When a step fails and freeing the claim fails
|
|
49
49
|
* too, it rejects with a `SuppressedError`: its `suppressed` is the step's
|
|
50
|
-
* error, and its `error` is the failure to free.
|
|
50
|
+
* error, and its `error` is the failure to free. When both fail with one
|
|
51
|
+
* error, it rejects with that error. After a loss, it only waits
|
|
51
52
|
* until the claim's resources are free, records nothing, and never rejects.
|
|
52
53
|
*/
|
|
53
54
|
resign(options?: ResignOptions): Promise<void>;
|
package/dist/term.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"term.d.ts","sourceRoot":"","sources":["../src/term.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,KAAK,EAAmB,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"term.d.ts","sourceRoot":"","sources":["../src/term.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,KAAK,EAAmB,MAAM,gBAAgB,CAAC;AAK7D,4FAA4F;AAC5F,MAAM,WAAW,SAAS;IACxB,+EAA+E;IAC/E,KAAK,CAAC,IAAI,EAAE,CAAC,MAAM,EAAE,KAAK,KAAK,IAAI,GAAG,UAAU,CAAC;IACjD;;;OAGG;IACH,mBAAmB,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IACrC,oEAAoE;IACpE,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IACzB,uDAAuD;IACvD,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACxB;AAED,MAAM,WAAW,aAAa;IAC5B;;;;OAIG;IACH,KAAK,EAAE,OAAO,CAAC;CAChB;AAED;;;;;GAKG;AACH,qBAAa,IAAK,YAAW,KAAK,EAAE,eAAe;;IACjD,qGAAqG;IACrG,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB;;;;OAIG;IACH,QAAQ,CAAC,kBAAkB,EAAE,OAAO,CAAC;IAUrC,YACE,EACE,KAAK,EACL,kBAAkB,EAClB,QAAQ,GACT,EAAE,IAAI,CAAC,IAAI,EAAE,OAAO,GAAG,oBAAoB,CAAC,GAAG;QAAE,QAAQ,EAAE,MAAM,CAAA;KAAE,EACpE,KAAK,EAAE,SAAS,EAOjB;IAED,mFAAmF;IACnF,IAAI,MAAM,IAAI,WAAW,CAExB;IAED;;;;;;;;OAQG;IACG,MAAM,CAAC,OAAO,CAAC,EAAE,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC,CAYnD;IAED,CAAC,MAAM,CAAC,YAAY,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC,CAErC;CAmDF"}
|
package/dist/term.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Future } from '@zukhruf/async';
|
|
2
2
|
import { LeaseController } from '@zukhruf/lease';
|
|
3
|
+
import { cleanUpAfter } from './clean-up-after.js';
|
|
3
4
|
import { TermWarning } from './term-warning.js';
|
|
4
5
|
/**
|
|
5
6
|
* One term of the elected leader: a lease on leadership. It ends when the
|
|
@@ -40,7 +41,8 @@ export class Term {
|
|
|
40
41
|
* clean shutdown for the next term. A second call waits for the first, and
|
|
41
42
|
* its options do nothing. When a step fails and freeing the claim fails
|
|
42
43
|
* too, it rejects with a `SuppressedError`: its `suppressed` is the step's
|
|
43
|
-
* error, and its `error` is the failure to free.
|
|
44
|
+
* error, and its `error` is the failure to free. When both fail with one
|
|
45
|
+
* error, it rejects with that error. After a loss, it only waits
|
|
44
46
|
* until the claim's resources are free, records nothing, and never rejects.
|
|
45
47
|
*/
|
|
46
48
|
async resign(options) {
|
|
@@ -75,33 +77,26 @@ export class Term {
|
|
|
75
77
|
.then(() => this.#ended.resolve());
|
|
76
78
|
}
|
|
77
79
|
async #giveUp(clean) {
|
|
78
|
-
|
|
80
|
+
await cleanUpAfter(async () => {
|
|
79
81
|
this.#watching.dispose();
|
|
80
82
|
// Before the release: until then no other candidate can hold the claim.
|
|
81
83
|
if (clean)
|
|
82
84
|
await this.#steps.recordCleanShutdown();
|
|
83
85
|
await this.#steps.release();
|
|
84
|
-
}
|
|
85
|
-
catch (error) {
|
|
86
|
-
await using closing = new AsyncDisposableStack();
|
|
87
|
-
closing.defer(() => this.#steps.close());
|
|
88
|
-
throw error;
|
|
89
|
-
}
|
|
90
|
-
await this.#steps.close();
|
|
86
|
+
}, () => this.#steps.close(), 'The term failed to give its claim up, and failed to free it.');
|
|
91
87
|
}
|
|
92
88
|
/**
|
|
93
89
|
* A lost claim is no longer this term's to give up: a release could undo
|
|
94
90
|
* the next leader's claim. It only stops watching and frees the claim's
|
|
95
91
|
* resources. A loss has no caller to report a failure to, so a failure of
|
|
96
92
|
* either step is a process warning, and the term still ends. When both
|
|
97
|
-
* fail, the warning is a `SuppressedError` of the failure to free
|
|
98
|
-
* failure to stop watching
|
|
93
|
+
* fail, the warning's cause is a `SuppressedError` of the failure to free
|
|
94
|
+
* over the failure to stop watching; when both fail with one error, the
|
|
95
|
+
* cause is that error.
|
|
99
96
|
*/
|
|
100
97
|
async #freeLost() {
|
|
101
98
|
try {
|
|
102
|
-
await
|
|
103
|
-
freeing.defer(() => this.#steps.close());
|
|
104
|
-
this.#watching.dispose();
|
|
99
|
+
await cleanUpAfter(() => this.#watching.dispose(), () => this.#steps.close(), 'The lost term failed to stop watching its claim, and failed to free it.');
|
|
105
100
|
}
|
|
106
101
|
catch (error) {
|
|
107
102
|
process.emitWarning(new TermWarning(error, { election: this.#election, epoch: this.epoch }));
|
package/dist/term.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"term.js","sourceRoot":"","sources":["../src/term.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AACxC,OAAO,EAAc,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAE7D,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AA0BhD;;;;;GAKG;AACH,MAAM,OAAO,IAAI;IACf,qGAAqG;IAC5F,KAAK,CAAS;IACvB;;;;OAIG;IACM,kBAAkB,CAAU;IACrC,oFAAoF;IAC3E,SAAS,CAAS;IAClB,MAAM,CAAY;IAClB,MAAM,GAAG,IAAI,eAAe,CAAC,YAAY,CAAC,CAAC;IACpD,gEAAgE;IACvD,OAAO,GAAG,IAAI,eAAe,EAAE,CAAC;IAChC,MAAM,GAAG,IAAI,MAAM,EAAQ,CAAC;IAC5B,SAAS,GAAG,IAAI,eAAe,EAAE,CAAC;IAE3C,YACE,EACE,KAAK,EACL,kBAAkB,EAClB,QAAQ,GAC0D,EACpE,KAAgB;QAEhB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,kBAAkB,GAAG,kBAAkB,CAAC;QAC7C,IAAI,CAAC,SAAS,GAAG,QAAQ,CAAC;QAC1B,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;QACpB,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;IAClE,CAAC;IAED,mFAAmF;IACnF,IAAI,MAAM;QACR,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC;IAC5B,CA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mB,EAAE,CAAC;YACnD,MAAM,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;QAC9B,CAAC,EACD,GAAG,EAAE,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,EACzB,8DAA8D,CAC/D,CAAC;IACJ,CAAC;IAED;;;;;;;;OAQG;IACH,KAAK,CAAC,SAAS;QACb,IAAI,CAAC;YACH,MAAM,YAAY,CAChB,GAAG,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,OAAO,EAAE,EAC9B,GAAG,EAAE,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,EACzB,yEAAyE,CAC1E,CAAC;QACJ,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,OAAO,CAAC,WAAW,CACjB,IAAI,WAAW,CAAC,KAAK,EAAE,EAAE,QAAQ,EAAE,IAAI,CAAC,SAAS,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,EAAE,CAAC,CACxE,CAAC;QACJ,CAAC;IACH,CAAC;CACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zukhruf/election",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.234",
|
|
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,15 +40,15 @@
|
|
|
40
40
|
"access": "public"
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"@zukhruf/async": "^0.8.
|
|
44
|
-
"@zukhruf/fs": "^0.8.
|
|
45
|
-
"@zukhruf/lease": "^0.8.
|
|
46
|
-
"@zukhruf/pipes": "^0.8.
|
|
43
|
+
"@zukhruf/async": "^0.8.234",
|
|
44
|
+
"@zukhruf/fs": "^0.8.234",
|
|
45
|
+
"@zukhruf/lease": "^0.8.234",
|
|
46
|
+
"@zukhruf/pipes": "^0.8.234"
|
|
47
47
|
},
|
|
48
48
|
"devDependencies": {
|
|
49
49
|
"@sinonjs/fake-timers": "15.5.0",
|
|
50
|
-
"@zukhruf/parameterized-test": "^0.8.
|
|
51
|
-
"@zukhruf/process": "^0.8.
|
|
52
|
-
"@zukhruf/testing": "^0.8.
|
|
50
|
+
"@zukhruf/parameterized-test": "^0.8.234",
|
|
51
|
+
"@zukhruf/process": "^0.8.234",
|
|
52
|
+
"@zukhruf/testing": "^0.8.234"
|
|
53
53
|
}
|
|
54
54
|
}
|