Skip to content

Commit 129d285

Browse files
authored
fix: correct stale timeout, allow setting minimum retry interval (#290)
1 parent 0def6c7 commit 129d285

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,10 @@ const db = new DB("/path/to/file", { lockfile: { /* lockfile options */ } });
9292
| Option | Default | Description |
9393
|-----------------|---------|-------------|
9494
| `directory` | - | Change where the lockfile is created, e.g. to put the lockfile into a `tmpfs`. By default the lockfile is created in the same directory as the DB file. If the directory does not exist, it will be created when opening the DB. |
95-
| `staleMs` | `10000` | Duration after which the lock is considered stale. Minimum: `5000` |
95+
| `staleMs` | `10000` | Duration after which the lock is considered stale. Minimum: `2000` |
9696
| `updateMs` | `staleMs/2` | The interval in which the lockfile's `mtime` will be updated. Range: `1000 ... staleMs/2` |
9797
| `retries` | `0` | How often to retry acquiring a lock before giving up. The retries progressively wait longer with an exponential backoff strategy. |
98+
| `retryMinTimeoutMs` | `updateMs/2` or `1000` | The start interval used for retries. Minimum: `100` |
9899

99100

100101
### Copying and compressing the database
@@ -149,6 +150,10 @@ The file will be overwritten if it exists. The 2nd options argument can be used
149150
Placeholder for next release:
150151
### __WORK IN PROGRESS__
151152
-->
153+
### __WORK IN PROGRESS__
154+
* Allow setting minimum retry timeout
155+
* Correct minimum value for the lockfile's stale timeout
156+
152157
### 2.5.0 (2022-02-13)
153158
* Add the ability to control the lockfile's stale/update timeouts and retrying acquiring a lock
154159

src/lib/db.test.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,12 @@ describe("lib/db", () => {
134134
});
135135

136136
describe("validates lockfile options", () => {
137-
it("staleMs < 5000", () => {
137+
it("staleMs < 2000", () => {
138138
expect(
139139
() =>
140140
new JsonlDB("foo", {
141141
lockfile: {
142-
staleMs: 2500,
142+
staleMs: 1999,
143143
},
144144
}),
145145
).toThrowError("staleMs");
@@ -190,6 +190,17 @@ describe("lib/db", () => {
190190
).toThrowError("retries");
191191
});
192192

193+
it("retryMinTimeoutMs < 100", () => {
194+
expect(
195+
() =>
196+
new JsonlDB("foo", {
197+
lockfile: {
198+
retryMinTimeoutMs: 99,
199+
},
200+
}),
201+
).toThrowError("retryMinTimeoutMs");
202+
});
203+
193204
it("lockfileDirectory and lockfile.directory both present", () => {
194205
expect(
195206
() =>

src/lib/db.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ export interface JsonlDBOptions<V> {
8585
* Range: [0...10]. Default: 0
8686
*/
8787
retries?: number;
88+
/** The start interval used for retries. Default: updateMs/2 */
89+
retryMinTimeoutMs?: number;
8890
}>;
8991

9092
/**
@@ -227,9 +229,10 @@ export class JsonlDB<V extends unknown = unknown> {
227229
retries,
228230
staleMs = 10000,
229231
updateMs = staleMs / 2,
232+
retryMinTimeoutMs,
230233
} = options.lockfile;
231-
if (staleMs < 5000) {
232-
throw new Error("staleMs must be >= 5000");
234+
if (staleMs < 2000) {
235+
throw new Error("staleMs must be >= 2000");
233236
}
234237
if (updateMs < 1000) {
235238
throw new Error("updateMs must be >= 1000");
@@ -243,6 +246,9 @@ export class JsonlDB<V extends unknown = unknown> {
243246
if (retries != undefined && retries > 10) {
244247
throw new Error("retries must be <= 10");
245248
}
249+
if (retryMinTimeoutMs != undefined && retryMinTimeoutMs < 100) {
250+
throw new Error("retryMinTimeoutMs must be >= 100");
251+
}
246252
if (
247253
options.lockfileDirectory != undefined &&
248254
directory != undefined
@@ -309,6 +315,9 @@ export class JsonlDB<V extends unknown = unknown> {
309315
let retryOptions: lockfile.LockOptions["retries"];
310316
if (this.options.lockfile?.retries) {
311317
retryOptions = {
318+
minTimeout:
319+
this.options.lockfile.retryMinTimeoutMs ??
320+
(this.options.lockfile.updateMs ?? 2000) / 2,
312321
retries: this.options.lockfile.retries,
313322
factor: 1.25,
314323
};

0 commit comments

Comments
 (0)