durability 5.2.0 → 5.3.0
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 +28 -13
- package/dist/alarms/index.js +18 -21
- package/dist/fanout/index.js +40 -53
- package/dist/log/index.d.ts +12 -17
- package/dist/log/index.js +59 -53
- package/dist/memoize/index.js +9 -10
- package/dist/migrations.js +48 -52
- package/dist/operations/index.js +22 -28
- package/dist/outbox/index.js +20 -31
- package/dist/policy.d.ts +19 -19
- package/dist/policy.js +13 -12
- package/dist/schedule/index.d.ts +13 -1
- package/dist/schedule/index.js +73 -60
- package/dist/scheduler/index.d.ts +19 -1
- package/dist/scheduler/index.js +53 -54
- package/dist/semaphore/client.js +6 -5
- package/dist/semaphore/protocol.d.ts +446 -234
- package/dist/semaphore/protocol.js +46 -61
- package/dist/semaphore/queue-store.d.ts +58 -52
- package/dist/semaphore/queue-store.js +55 -57
- package/dist/semaphore/queue.js +2 -15
- package/dist/semaphore/server.js +24 -14
- package/dist/semaphore/store.d.ts +30 -25
- package/dist/semaphore/store.js +31 -34
- package/dist/serialization.d.ts +5 -0
- package/dist/serialization.js +41 -16
- package/dist/shared-timer.d.ts +27 -0
- package/dist/shared-timer.js +191 -0
- package/dist/socket/client.js +25 -22
- package/dist/storage.js +9 -9
- package/dist/topology/client.js +29 -15
- package/dist/topology/index.js +18 -32
- package/dist/topology/snapshot.d.ts +20 -40
- package/dist/topology/snapshot.js +18 -28
- package/dist/tracker/index.js +15 -12
- package/package.json +4 -4
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as v from 'valibot';
|
|
2
2
|
/**
|
|
3
3
|
* Messages between a semaphore client and the Durable Object holding the
|
|
4
4
|
* limit. Default clients keep version 1. Named queues use version 2 so old
|
|
5
5
|
* holders reject them before allocating grants or leases under the wrong
|
|
6
6
|
* identity. Extra fields remain tolerated within each version.
|
|
7
7
|
*/
|
|
8
|
-
const nonNegative =
|
|
9
|
-
const nonEmpty =
|
|
10
|
-
const protocolVersion =
|
|
8
|
+
const nonNegative = v.pipe(v.number(), v.safeInteger(), v.minValue(0));
|
|
9
|
+
const nonEmpty = v.pipe(v.string(), v.minLength(1));
|
|
10
|
+
const protocolVersion = v.picklist([1, 2]);
|
|
11
11
|
const versionMatchesChannel = (message) => message.v === (message.clientChannel === undefined ? 1 : 2);
|
|
12
12
|
const channelVersionError = 'Client channel does not match semaphore protocol version';
|
|
13
13
|
/**
|
|
14
14
|
* Which queue of the client object sent the message. Version 1 omits it;
|
|
15
15
|
* version 2 requires it. Older clients therefore keep their identity.
|
|
16
16
|
*/
|
|
17
|
-
const clientChannel =
|
|
17
|
+
const clientChannel = v.optional(nonEmpty);
|
|
18
18
|
/** Bounds one message's work so a client cannot monopolize the holder. */
|
|
19
19
|
export const maxDemandEntries = 100;
|
|
20
20
|
export const maxGrantReports = 200;
|
|
21
21
|
/** Cumulative counts for one grant. Replaying a report can never free capacity twice. */
|
|
22
|
-
export const grantReportSchema =
|
|
22
|
+
export const grantReportSchema = v.object({
|
|
23
23
|
grantId: nonEmpty,
|
|
24
24
|
/** Permits of this grant the client has used, as a high-water mark. */
|
|
25
25
|
claimed: nonNegative,
|
|
@@ -27,103 +27,88 @@ export const grantReportSchema = z.object({
|
|
|
27
27
|
released: nonNegative,
|
|
28
28
|
});
|
|
29
29
|
/** Cumulative counts for one lease the client holds. */
|
|
30
|
-
export const leaseReportSchema =
|
|
30
|
+
export const leaseReportSchema = v.object({
|
|
31
31
|
leaseId: nonEmpty,
|
|
32
32
|
/** Permits drawn locally so far, as a high-water mark. */
|
|
33
33
|
drawn: nonNegative,
|
|
34
34
|
/** Drawn permits returned, as a high-water mark. */
|
|
35
35
|
released: nonNegative,
|
|
36
36
|
/** The client stopped drawing: the lease's undrawn permits are returned. */
|
|
37
|
-
retired:
|
|
37
|
+
retired: v.optional(v.boolean()),
|
|
38
38
|
});
|
|
39
|
-
export const leaseGrantSchema =
|
|
39
|
+
export const leaseGrantSchema = v.object({
|
|
40
40
|
leaseId: nonEmpty,
|
|
41
|
-
group:
|
|
41
|
+
group: v.string(),
|
|
42
42
|
permits: nonNegative,
|
|
43
43
|
/** `expiresAt <= now` retires the lease: stop drawing immediately. */
|
|
44
44
|
expiresAt: nonNegative,
|
|
45
45
|
});
|
|
46
|
-
export const permitRequestSchema =
|
|
47
|
-
.
|
|
48
|
-
type: z.literal('request'),
|
|
46
|
+
export const permitRequestSchema = v.pipe(v.object({
|
|
47
|
+
type: v.literal('request'),
|
|
49
48
|
v: protocolVersion,
|
|
50
49
|
clientId: nonEmpty,
|
|
51
50
|
clientChannel,
|
|
52
51
|
/** Sparse demand per group with the oldest queued time, for FIFO allocation. */
|
|
53
|
-
demand:
|
|
54
|
-
.
|
|
55
|
-
|
|
56
|
-
count: z.number().int().positive(),
|
|
52
|
+
demand: v.pipe(v.array(v.object({
|
|
53
|
+
group: v.string(),
|
|
54
|
+
count: v.pipe(v.number(), v.safeInteger(), v.gtValue(0)),
|
|
57
55
|
oldestAt: nonNegative,
|
|
58
|
-
})
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
.refine(versionMatchesChannel, channelVersionError);
|
|
65
|
-
export const permitSyncSchema = z
|
|
66
|
-
.object({
|
|
67
|
-
type: z.literal('sync'),
|
|
56
|
+
})), v.maxLength(maxDemandEntries)),
|
|
57
|
+
grants: v.pipe(v.array(grantReportSchema), v.maxLength(maxGrantReports)),
|
|
58
|
+
leases: v.optional(v.pipe(v.array(leaseReportSchema), v.maxLength(maxGrantReports))),
|
|
59
|
+
}), v.check(versionMatchesChannel, channelVersionError));
|
|
60
|
+
export const permitSyncSchema = v.pipe(v.object({
|
|
61
|
+
type: v.literal('sync'),
|
|
68
62
|
v: protocolVersion,
|
|
69
63
|
clientId: nonEmpty,
|
|
70
64
|
clientChannel,
|
|
71
|
-
grants:
|
|
72
|
-
leases:
|
|
73
|
-
})
|
|
74
|
-
|
|
75
|
-
export const semaphoreRequestSchema = z.discriminatedUnion('type', [
|
|
65
|
+
grants: v.pipe(v.array(grantReportSchema), v.maxLength(maxGrantReports)),
|
|
66
|
+
leases: v.optional(v.pipe(v.array(leaseReportSchema), v.maxLength(maxGrantReports))),
|
|
67
|
+
}), v.check(versionMatchesChannel, channelVersionError));
|
|
68
|
+
export const semaphoreRequestSchema = v.union([
|
|
76
69
|
permitRequestSchema,
|
|
77
70
|
permitSyncSchema,
|
|
78
71
|
]);
|
|
79
|
-
export const semaphoreBlockedReasonSchema =
|
|
72
|
+
export const semaphoreBlockedReasonSchema = v.picklist([
|
|
80
73
|
'group_limit',
|
|
81
74
|
'limit',
|
|
82
75
|
'limit_resolution_error',
|
|
83
76
|
'not_permitted',
|
|
84
77
|
]);
|
|
85
|
-
export const grantResultSchema =
|
|
78
|
+
export const grantResultSchema = v.object({
|
|
86
79
|
grantId: nonEmpty,
|
|
87
80
|
/** Server-clamped claim high-water mark; the client admits only up to this. */
|
|
88
81
|
claimed: nonNegative,
|
|
89
|
-
status:
|
|
82
|
+
status: v.picklist(['ok', 'not_found', 'expired']),
|
|
90
83
|
});
|
|
91
|
-
export const permitGrantSchema =
|
|
92
|
-
.
|
|
93
|
-
type: z.literal('grant'),
|
|
84
|
+
export const permitGrantSchema = v.pipe(v.object({
|
|
85
|
+
type: v.literal('grant'),
|
|
94
86
|
v: protocolVersion,
|
|
95
|
-
grants:
|
|
96
|
-
.object({
|
|
87
|
+
grants: v.array(v.object({
|
|
97
88
|
grantId: nonEmpty,
|
|
98
|
-
group:
|
|
99
|
-
permits:
|
|
100
|
-
})
|
|
101
|
-
|
|
102
|
-
blocked: z
|
|
103
|
-
.object({ group: z.string(), reason: semaphoreBlockedReasonSchema })
|
|
104
|
-
.array(),
|
|
89
|
+
group: v.string(),
|
|
90
|
+
permits: v.pipe(v.number(), v.safeInteger(), v.gtValue(0)),
|
|
91
|
+
})),
|
|
92
|
+
blocked: v.array(v.object({ group: v.string(), reason: semaphoreBlockedReasonSchema })),
|
|
105
93
|
/** The client must not send another request before this. */
|
|
106
94
|
nextRequestAt: nonNegative,
|
|
107
|
-
grantResults:
|
|
108
|
-
leases:
|
|
95
|
+
grantResults: v.array(grantResultSchema),
|
|
96
|
+
leases: v.optional(v.array(leaseGrantSchema)),
|
|
109
97
|
/** Demand went unserved: return undrawn lease permits in groups with nothing queued. */
|
|
110
|
-
contended:
|
|
98
|
+
contended: v.optional(v.boolean()),
|
|
111
99
|
/** Echoes the request's client channel in version 2. */
|
|
112
100
|
clientChannel,
|
|
113
|
-
})
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
.object({
|
|
117
|
-
type: z.literal('synced'),
|
|
101
|
+
}), v.check(versionMatchesChannel, channelVersionError));
|
|
102
|
+
export const permitSyncResultSchema = v.pipe(v.object({
|
|
103
|
+
type: v.literal('synced'),
|
|
118
104
|
v: protocolVersion,
|
|
119
|
-
grantResults:
|
|
120
|
-
leases:
|
|
121
|
-
contended:
|
|
105
|
+
grantResults: v.array(grantResultSchema),
|
|
106
|
+
leases: v.optional(v.array(leaseGrantSchema)),
|
|
107
|
+
contended: v.optional(v.boolean()),
|
|
122
108
|
/** Echoes the sync's `clientChannel`, as for a grant. */
|
|
123
109
|
clientChannel,
|
|
124
|
-
})
|
|
125
|
-
|
|
126
|
-
export const semaphoreResponseSchema = z.discriminatedUnion('type', [
|
|
110
|
+
}), v.check(versionMatchesChannel, channelVersionError));
|
|
111
|
+
export const semaphoreResponseSchema = v.union([
|
|
127
112
|
permitGrantSchema,
|
|
128
113
|
permitSyncResultSchema,
|
|
129
114
|
]);
|
|
@@ -1,55 +1,61 @@
|
|
|
1
|
-
import
|
|
2
|
-
export declare const queueItemRowSchema:
|
|
3
|
-
id:
|
|
4
|
-
key:
|
|
5
|
-
grp:
|
|
6
|
-
payload:
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
confirmed:
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
export
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
1
|
+
import * as v from 'valibot';
|
|
2
|
+
export declare const queueItemRowSchema: v.ObjectSchema<{
|
|
3
|
+
readonly id: v.StringSchema<undefined>;
|
|
4
|
+
readonly key: v.StringSchema<undefined>;
|
|
5
|
+
readonly grp: v.StringSchema<undefined>;
|
|
6
|
+
readonly payload: v.StringSchema<undefined>;
|
|
7
|
+
/** queued: waiting for permits; claiming: counted against a grant, awaiting confirmation; admitted: running. */
|
|
8
|
+
readonly status: v.PicklistSchema<["queued", "claiming", "admitted"], undefined>;
|
|
9
|
+
readonly grant_id: v.NullableSchema<v.StringSchema<undefined>, undefined>;
|
|
10
|
+
readonly lease_id: v.NullableSchema<v.StringSchema<undefined>, undefined>;
|
|
11
|
+
readonly created_at: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
|
|
12
|
+
readonly admitted_at: v.NullableSchema<v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>, undefined>;
|
|
13
|
+
/** 0 until `admit()` has returned for this item. */
|
|
14
|
+
readonly notified: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>, v.MaxValueAction<number, 1, undefined>]>;
|
|
15
|
+
readonly attempt: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
|
|
16
|
+
readonly next_attempt_at: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
|
|
17
|
+
}, undefined>;
|
|
18
|
+
export type QueueItemRow = v.InferOutput<typeof queueItemRowSchema>;
|
|
19
|
+
export declare const queueGrantRowSchema: v.ObjectSchema<{
|
|
20
|
+
readonly grant_id: v.StringSchema<undefined>;
|
|
21
|
+
readonly key: v.StringSchema<undefined>;
|
|
22
|
+
readonly grp: v.StringSchema<undefined>;
|
|
23
|
+
readonly permits: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
|
|
24
|
+
/** Permits counted against this grant locally, as a high-water mark. */
|
|
25
|
+
readonly claimed: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
|
|
26
|
+
/** Claims the holder has confirmed. */
|
|
27
|
+
readonly confirmed: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
|
|
28
|
+
/** Items handed to `admit()` from this grant; never derived from live rows. */
|
|
29
|
+
readonly admitted: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
|
|
30
|
+
readonly released: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
|
|
31
|
+
readonly created_at: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
|
|
32
|
+
}, undefined>;
|
|
33
|
+
export type QueueGrantRow = v.InferOutput<typeof queueGrantRowSchema>;
|
|
34
|
+
export declare const queueKeyRowSchema: v.ObjectSchema<{
|
|
35
|
+
readonly key: v.StringSchema<undefined>;
|
|
36
|
+
/** Earliest time the next permit request for this key may be sent. */
|
|
37
|
+
readonly next_request_at: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
|
|
38
|
+
readonly queued: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
|
|
39
|
+
}, undefined>;
|
|
40
|
+
export type QueueKeyRow = v.InferOutput<typeof queueKeyRowSchema>;
|
|
41
|
+
export declare const queueLeaseRowSchema: v.ObjectSchema<{
|
|
42
|
+
readonly lease_id: v.StringSchema<undefined>;
|
|
43
|
+
readonly key: v.StringSchema<undefined>;
|
|
44
|
+
readonly grp: v.StringSchema<undefined>;
|
|
45
|
+
readonly permits: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
|
|
46
|
+
/** Permits drawn so far; a drawn permit is never drawable again. */
|
|
47
|
+
readonly drawn: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
|
|
48
|
+
readonly released: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
|
|
49
|
+
readonly created_at: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
|
|
50
|
+
readonly expires_at: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
|
|
51
|
+
/** When to report the lease again while it has permits in use, so the holder keeps it. */
|
|
52
|
+
readonly renew_at: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
|
|
53
|
+
/** Half the lease's lifetime at the holder, from its last refresh. */
|
|
54
|
+
readonly renew_ms: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.GtValueAction<number, 0, undefined>]>;
|
|
55
|
+
/** 1 once the holder has settled the stopped lease; until then it is reported again. */
|
|
56
|
+
readonly confirmed: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>, v.MaxValueAction<number, 1, undefined>]>;
|
|
57
|
+
}, undefined>;
|
|
58
|
+
export type QueueLeaseRow = v.InferOutput<typeof queueLeaseRowSchema>;
|
|
53
59
|
/**
|
|
54
60
|
* One queue channel of a semaphore client: items, the grants it holds per
|
|
55
61
|
* key, and per-key request pacing. Every query is scoped to the channel, so
|
|
@@ -1,63 +1,63 @@
|
|
|
1
|
-
import
|
|
2
|
-
export const queueItemRowSchema =
|
|
3
|
-
id:
|
|
4
|
-
key:
|
|
5
|
-
grp:
|
|
6
|
-
payload:
|
|
1
|
+
import * as v from 'valibot';
|
|
2
|
+
export const queueItemRowSchema = v.object({
|
|
3
|
+
id: v.string(),
|
|
4
|
+
key: v.string(),
|
|
5
|
+
grp: v.string(),
|
|
6
|
+
payload: v.string(),
|
|
7
7
|
/** queued: waiting for permits; claiming: counted against a grant, awaiting confirmation; admitted: running. */
|
|
8
|
-
status:
|
|
9
|
-
grant_id:
|
|
10
|
-
lease_id:
|
|
11
|
-
created_at:
|
|
12
|
-
admitted_at:
|
|
8
|
+
status: v.picklist(['queued', 'claiming', 'admitted']),
|
|
9
|
+
grant_id: v.nullable(v.string()),
|
|
10
|
+
lease_id: v.nullable(v.string()),
|
|
11
|
+
created_at: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
12
|
+
admitted_at: v.nullable(v.pipe(v.number(), v.safeInteger(), v.minValue(0))),
|
|
13
13
|
/** 0 until `admit()` has returned for this item. */
|
|
14
|
-
notified:
|
|
15
|
-
attempt:
|
|
16
|
-
next_attempt_at:
|
|
14
|
+
notified: v.pipe(v.number(), v.safeInteger(), v.minValue(0), v.maxValue(1)),
|
|
15
|
+
attempt: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
16
|
+
next_attempt_at: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
17
17
|
});
|
|
18
|
-
export const queueGrantRowSchema =
|
|
19
|
-
grant_id:
|
|
20
|
-
key:
|
|
21
|
-
grp:
|
|
22
|
-
permits:
|
|
18
|
+
export const queueGrantRowSchema = v.object({
|
|
19
|
+
grant_id: v.string(),
|
|
20
|
+
key: v.string(),
|
|
21
|
+
grp: v.string(),
|
|
22
|
+
permits: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
23
23
|
/** Permits counted against this grant locally, as a high-water mark. */
|
|
24
|
-
claimed:
|
|
24
|
+
claimed: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
25
25
|
/** Claims the holder has confirmed. */
|
|
26
|
-
confirmed:
|
|
26
|
+
confirmed: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
27
27
|
/** Items handed to `admit()` from this grant; never derived from live rows. */
|
|
28
|
-
admitted:
|
|
29
|
-
released:
|
|
30
|
-
created_at:
|
|
28
|
+
admitted: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
29
|
+
released: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
30
|
+
created_at: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
31
31
|
});
|
|
32
|
-
export const queueKeyRowSchema =
|
|
33
|
-
key:
|
|
32
|
+
export const queueKeyRowSchema = v.object({
|
|
33
|
+
key: v.string(),
|
|
34
34
|
/** Earliest time the next permit request for this key may be sent. */
|
|
35
|
-
next_request_at:
|
|
36
|
-
queued:
|
|
35
|
+
next_request_at: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
36
|
+
queued: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
37
37
|
});
|
|
38
|
-
export const queueLeaseRowSchema =
|
|
39
|
-
lease_id:
|
|
40
|
-
key:
|
|
41
|
-
grp:
|
|
42
|
-
permits:
|
|
38
|
+
export const queueLeaseRowSchema = v.object({
|
|
39
|
+
lease_id: v.string(),
|
|
40
|
+
key: v.string(),
|
|
41
|
+
grp: v.string(),
|
|
42
|
+
permits: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
43
43
|
/** Permits drawn so far; a drawn permit is never drawable again. */
|
|
44
|
-
drawn:
|
|
45
|
-
released:
|
|
46
|
-
created_at:
|
|
47
|
-
expires_at:
|
|
44
|
+
drawn: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
45
|
+
released: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
46
|
+
created_at: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
47
|
+
expires_at: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
48
48
|
/** When to report the lease again while it has permits in use, so the holder keeps it. */
|
|
49
|
-
renew_at:
|
|
49
|
+
renew_at: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
50
50
|
/** Half the lease's lifetime at the holder, from its last refresh. */
|
|
51
|
-
renew_ms:
|
|
51
|
+
renew_ms: v.pipe(v.number(), v.safeInteger(), v.gtValue(0)),
|
|
52
52
|
/** 1 once the holder has settled the stopped lease; until then it is reported again. */
|
|
53
|
-
confirmed:
|
|
53
|
+
confirmed: v.pipe(v.number(), v.safeInteger(), v.minValue(0), v.maxValue(1)),
|
|
54
54
|
});
|
|
55
|
-
const queueItemColumns =
|
|
56
|
-
const queueGrantColumns =
|
|
57
|
-
const queueLeaseColumns =
|
|
58
|
-
const statusCountSchema =
|
|
59
|
-
status: queueItemRowSchema.
|
|
60
|
-
count:
|
|
55
|
+
const queueItemColumns = v.keyof(queueItemRowSchema).options;
|
|
56
|
+
const queueGrantColumns = v.keyof(queueGrantRowSchema).options;
|
|
57
|
+
const queueLeaseColumns = v.keyof(queueLeaseRowSchema).options;
|
|
58
|
+
const statusCountSchema = v.object({
|
|
59
|
+
status: queueItemRowSchema.entries.status,
|
|
60
|
+
count: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
61
61
|
});
|
|
62
62
|
/**
|
|
63
63
|
* One queue channel of a semaphore client: items, the grants it holds per
|
|
@@ -121,7 +121,7 @@ export class QueueStore {
|
|
|
121
121
|
}
|
|
122
122
|
countItems() {
|
|
123
123
|
const counts = { queued: 0, claiming: 0, admitted: 0 };
|
|
124
|
-
for (const row of
|
|
124
|
+
for (const row of v.parse(v.array(statusCountSchema), this.sql
|
|
125
125
|
.exec(`SELECT status, COUNT(*) AS count FROM durability_semaphore_queue_items
|
|
126
126
|
WHERE channel = ? GROUP BY status`, this.channel)
|
|
127
127
|
.toArray())) {
|
|
@@ -130,7 +130,7 @@ export class QueueStore {
|
|
|
130
130
|
return counts;
|
|
131
131
|
}
|
|
132
132
|
getGrant(grantId) {
|
|
133
|
-
return
|
|
133
|
+
return v.parse(v.optional(queueGrantRowSchema), this.sql
|
|
134
134
|
.exec(`SELECT * FROM durability_semaphore_queue_grants
|
|
135
135
|
WHERE channel = ? AND grant_id = ? LIMIT 1`, this.channel, grantId)
|
|
136
136
|
.toArray()[0]);
|
|
@@ -152,13 +152,13 @@ export class QueueStore {
|
|
|
152
152
|
this.sql.exec('DELETE FROM durability_semaphore_queue_grants WHERE channel = ? AND grant_id = ?', this.channel, grantId);
|
|
153
153
|
}
|
|
154
154
|
listGrants(key) {
|
|
155
|
-
return
|
|
155
|
+
return v.parse(v.array(queueGrantRowSchema), this.sql
|
|
156
156
|
.exec(`SELECT * FROM durability_semaphore_queue_grants
|
|
157
157
|
WHERE channel = ? AND key = ? ORDER BY created_at ASC, grant_id ASC`, this.channel, key)
|
|
158
158
|
.toArray());
|
|
159
159
|
}
|
|
160
160
|
getLease(leaseId) {
|
|
161
|
-
return
|
|
161
|
+
return v.parse(v.optional(queueLeaseRowSchema), this.sql
|
|
162
162
|
.exec(`SELECT * FROM durability_semaphore_queue_leases
|
|
163
163
|
WHERE channel = ? AND lease_id = ? LIMIT 1`, this.channel, leaseId)
|
|
164
164
|
.toArray()[0]);
|
|
@@ -183,7 +183,7 @@ export class QueueStore {
|
|
|
183
183
|
* reported, so a lost report must be repeated.
|
|
184
184
|
*/
|
|
185
185
|
listRenewalsDue(now, limit) {
|
|
186
|
-
return
|
|
186
|
+
return v.parse(v.array(queueLeaseRowSchema), this.sql
|
|
187
187
|
.exec(`SELECT * FROM durability_semaphore_queue_leases
|
|
188
188
|
WHERE channel = ? AND confirmed = 0 AND renew_at <= ?
|
|
189
189
|
AND (released < drawn OR expires_at <= ?)
|
|
@@ -197,13 +197,13 @@ export class QueueStore {
|
|
|
197
197
|
AND released >= drawn`, this.channel, before);
|
|
198
198
|
}
|
|
199
199
|
listLeases(key) {
|
|
200
|
-
return
|
|
200
|
+
return v.parse(v.array(queueLeaseRowSchema), this.sql
|
|
201
201
|
.exec(`SELECT * FROM durability_semaphore_queue_leases
|
|
202
202
|
WHERE channel = ? AND key = ? ORDER BY created_at ASC, lease_id ASC`, this.channel, key)
|
|
203
203
|
.toArray());
|
|
204
204
|
}
|
|
205
205
|
getKey(key) {
|
|
206
|
-
return
|
|
206
|
+
return v.parse(v.optional(queueKeyRowSchema), this.sql
|
|
207
207
|
.exec(`SELECT * FROM durability_semaphore_queue_keys
|
|
208
208
|
WHERE channel = ? AND key = ? LIMIT 1`, this.channel, key)
|
|
209
209
|
.toArray()[0]);
|
|
@@ -221,7 +221,7 @@ export class QueueStore {
|
|
|
221
221
|
this.sql.exec('DELETE FROM durability_semaphore_queue_keys WHERE channel = ? AND key = ?', this.channel, key);
|
|
222
222
|
}
|
|
223
223
|
listDueKeys(now) {
|
|
224
|
-
return
|
|
224
|
+
return v.parse(v.array(queueKeyRowSchema), this.sql
|
|
225
225
|
.exec(`SELECT * FROM durability_semaphore_queue_keys
|
|
226
226
|
WHERE channel = ? AND queued > 0 AND next_request_at <= ?
|
|
227
227
|
ORDER BY next_request_at ASC`, this.channel, now)
|
|
@@ -253,8 +253,6 @@ export class QueueStore {
|
|
|
253
253
|
return typeof at === 'number' ? at : undefined;
|
|
254
254
|
}
|
|
255
255
|
items(query, ...bindings) {
|
|
256
|
-
return queueItemRowSchema
|
|
257
|
-
.array()
|
|
258
|
-
.parse(this.sql.exec(query, ...bindings).toArray());
|
|
256
|
+
return v.parse(v.array(queueItemRowSchema), this.sql.exec(query, ...bindings).toArray());
|
|
259
257
|
}
|
|
260
258
|
}
|
package/dist/semaphore/queue.js
CHANGED
|
@@ -903,20 +903,7 @@ async function _DurabilitySemaphoreQueue_notify() {
|
|
|
903
903
|
reportFailure('durability.semaphore_queue.retry_policy_failed', {}, error);
|
|
904
904
|
}
|
|
905
905
|
return 60000;
|
|
906
|
-
}, _DurabilitySemaphoreQueue_deadline =
|
|
907
|
-
|
|
908
|
-
try {
|
|
909
|
-
return await Promise.race([
|
|
910
|
-
Promise.resolve().then(run),
|
|
911
|
-
new Promise((_resolve, reject) => {
|
|
912
|
-
timer = setTimeout(() => reject(new Error(`Semaphore ${what} timed out after ${__classPrivateFieldGet(this, _DurabilitySemaphoreQueue_attemptTimeoutMs, "f")}ms`)), __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_attemptTimeoutMs, "f"));
|
|
913
|
-
}),
|
|
914
|
-
]);
|
|
915
|
-
}
|
|
916
|
-
finally {
|
|
917
|
-
if (timer !== undefined) {
|
|
918
|
-
clearTimeout(timer);
|
|
919
|
-
}
|
|
920
|
-
}
|
|
906
|
+
}, _DurabilitySemaphoreQueue_deadline = function _DurabilitySemaphoreQueue_deadline(what, run) {
|
|
907
|
+
return __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_engine, "f").timer.withTimeout(Promise.resolve().then(run), __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_attemptTimeoutMs, "f"), () => new Error(`Semaphore ${what} timed out after ${__classPrivateFieldGet(this, _DurabilitySemaphoreQueue_attemptTimeoutMs, "f")}ms`));
|
|
921
908
|
};
|
|
922
909
|
traceMethods(DurabilitySemaphoreQueue.prototype, 'DurabilitySemaphoreQueue');
|
package/dist/semaphore/server.js
CHANGED
|
@@ -9,7 +9,8 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
9
9
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
10
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
11
|
};
|
|
12
|
-
var _DurabilitySemaphoreServer_instances, _DurabilitySemaphoreServer_engine, _DurabilitySemaphoreServer_store, _DurabilitySemaphoreServer_limit, _DurabilitySemaphoreServer_groupLimit, _DurabilitySemaphoreServer_canGrant, _DurabilitySemaphoreServer_roundWaitMs, _DurabilitySemaphoreServer_roundPeriodMs, _DurabilitySemaphoreServer_grantTimeoutMs, _DurabilitySemaphoreServer_leases, _DurabilitySemaphoreServer_emit, _DurabilitySemaphoreServer_pending,
|
|
12
|
+
var _DurabilitySemaphoreServer_instances, _DurabilitySemaphoreServer_engine, _DurabilitySemaphoreServer_store, _DurabilitySemaphoreServer_limit, _DurabilitySemaphoreServer_groupLimit, _DurabilitySemaphoreServer_canGrant, _DurabilitySemaphoreServer_roundWaitMs, _DurabilitySemaphoreServer_roundPeriodMs, _DurabilitySemaphoreServer_grantTimeoutMs, _DurabilitySemaphoreServer_leases, _DurabilitySemaphoreServer_emit, _DurabilitySemaphoreServer_pending, _DurabilitySemaphoreServer_roundScheduled, _DurabilitySemaphoreServer_round, _DurabilitySemaphoreServer_lastRoundAt, _DurabilitySemaphoreServer_contended, _DurabilitySemaphoreServer_grantRoom, _DurabilitySemaphoreServer_scheduleRound, _DurabilitySemaphoreServer_runRound, _DurabilitySemaphoreServer_allocate, _DurabilitySemaphoreServer_sync, _DurabilitySemaphoreServer_applyReports, _DurabilitySemaphoreServer_reclaim, _DurabilitySemaphoreServer_reclaimExpired, _DurabilitySemaphoreServer_resolveLimit, _DurabilitySemaphoreServer_groupPolicy;
|
|
13
|
+
import * as v from 'valibot';
|
|
13
14
|
import { traceLifecycle, traceMethods } from '../dev.js';
|
|
14
15
|
import { SemaphoreProtocolError, reportFailure } from '../errors.js';
|
|
15
16
|
import { durabilitySemaphoreMigrations } from './migrations.js';
|
|
@@ -85,7 +86,7 @@ export class DurabilitySemaphoreServer {
|
|
|
85
86
|
_DurabilitySemaphoreServer_leases.set(this, void 0);
|
|
86
87
|
_DurabilitySemaphoreServer_emit.set(this, void 0);
|
|
87
88
|
_DurabilitySemaphoreServer_pending.set(this, []);
|
|
88
|
-
|
|
89
|
+
_DurabilitySemaphoreServer_roundScheduled.set(this, false);
|
|
89
90
|
_DurabilitySemaphoreServer_round.set(this, void 0);
|
|
90
91
|
_DurabilitySemaphoreServer_lastRoundAt.set(this, -Infinity);
|
|
91
92
|
// From the last round; tells clients in later syncs to return idle lease headroom.
|
|
@@ -139,14 +140,14 @@ export class DurabilitySemaphoreServer {
|
|
|
139
140
|
* fails and the client asks again.
|
|
140
141
|
*/
|
|
141
142
|
handle(message) {
|
|
142
|
-
const parsed =
|
|
143
|
+
const parsed = v.safeParse(semaphoreRequestSchema, message);
|
|
143
144
|
if (!parsed.success) {
|
|
144
145
|
return Promise.reject(new SemaphoreProtocolError('Malformed semaphore message'));
|
|
145
146
|
}
|
|
146
|
-
if (parsed.
|
|
147
|
-
return __classPrivateFieldGet(this, _DurabilitySemaphoreServer_instances, "m", _DurabilitySemaphoreServer_sync).call(this, parsed.
|
|
147
|
+
if (parsed.output.type === 'sync') {
|
|
148
|
+
return __classPrivateFieldGet(this, _DurabilitySemaphoreServer_instances, "m", _DurabilitySemaphoreServer_sync).call(this, parsed.output);
|
|
148
149
|
}
|
|
149
|
-
const request = parsed.
|
|
150
|
+
const request = parsed.output;
|
|
150
151
|
return new Promise((resolve, reject) => {
|
|
151
152
|
__classPrivateFieldGet(this, _DurabilitySemaphoreServer_pending, "f").push({
|
|
152
153
|
request,
|
|
@@ -190,20 +191,21 @@ export class DurabilitySemaphoreServer {
|
|
|
190
191
|
};
|
|
191
192
|
}
|
|
192
193
|
}
|
|
193
|
-
_DurabilitySemaphoreServer_engine = new WeakMap(), _DurabilitySemaphoreServer_store = new WeakMap(), _DurabilitySemaphoreServer_limit = new WeakMap(), _DurabilitySemaphoreServer_groupLimit = new WeakMap(), _DurabilitySemaphoreServer_canGrant = new WeakMap(), _DurabilitySemaphoreServer_roundWaitMs = new WeakMap(), _DurabilitySemaphoreServer_roundPeriodMs = new WeakMap(), _DurabilitySemaphoreServer_grantTimeoutMs = new WeakMap(), _DurabilitySemaphoreServer_leases = new WeakMap(), _DurabilitySemaphoreServer_emit = new WeakMap(), _DurabilitySemaphoreServer_pending = new WeakMap(),
|
|
194
|
+
_DurabilitySemaphoreServer_engine = new WeakMap(), _DurabilitySemaphoreServer_store = new WeakMap(), _DurabilitySemaphoreServer_limit = new WeakMap(), _DurabilitySemaphoreServer_groupLimit = new WeakMap(), _DurabilitySemaphoreServer_canGrant = new WeakMap(), _DurabilitySemaphoreServer_roundWaitMs = new WeakMap(), _DurabilitySemaphoreServer_roundPeriodMs = new WeakMap(), _DurabilitySemaphoreServer_grantTimeoutMs = new WeakMap(), _DurabilitySemaphoreServer_leases = new WeakMap(), _DurabilitySemaphoreServer_emit = new WeakMap(), _DurabilitySemaphoreServer_pending = new WeakMap(), _DurabilitySemaphoreServer_roundScheduled = new WeakMap(), _DurabilitySemaphoreServer_round = new WeakMap(), _DurabilitySemaphoreServer_lastRoundAt = new WeakMap(), _DurabilitySemaphoreServer_contended = new WeakMap(), _DurabilitySemaphoreServer_instances = new WeakSet(), _DurabilitySemaphoreServer_grantRoom = function _DurabilitySemaphoreServer_grantRoom(limit, granted, leased) {
|
|
194
195
|
if (!__classPrivateFieldGet(this, _DurabilitySemaphoreServer_leases, "f")) {
|
|
195
196
|
return Math.max(0, limit - granted);
|
|
196
197
|
}
|
|
197
198
|
const budget = budgets(__classPrivateFieldGet(this, _DurabilitySemaphoreServer_leases, "f").provisioning, limit);
|
|
198
199
|
return Math.max(0, budget.grants - granted - (budget.shared ? leased : 0));
|
|
199
200
|
}, _DurabilitySemaphoreServer_scheduleRound = function _DurabilitySemaphoreServer_scheduleRound() {
|
|
200
|
-
if (__classPrivateFieldGet(this,
|
|
201
|
+
if (__classPrivateFieldGet(this, _DurabilitySemaphoreServer_roundScheduled, "f") || __classPrivateFieldGet(this, _DurabilitySemaphoreServer_round, "f") !== undefined) {
|
|
201
202
|
return;
|
|
202
203
|
}
|
|
203
204
|
const earliest = __classPrivateFieldGet(this, _DurabilitySemaphoreServer_lastRoundAt, "f") + __classPrivateFieldGet(this, _DurabilitySemaphoreServer_roundPeriodMs, "f");
|
|
204
205
|
const delay = Math.max(__classPrivateFieldGet(this, _DurabilitySemaphoreServer_roundWaitMs, "f"), earliest - Date.now());
|
|
205
|
-
__classPrivateFieldSet(this,
|
|
206
|
-
|
|
206
|
+
__classPrivateFieldSet(this, _DurabilitySemaphoreServer_roundScheduled, true, "f");
|
|
207
|
+
__classPrivateFieldGet(this, _DurabilitySemaphoreServer_engine, "f").timer.schedule(() => {
|
|
208
|
+
__classPrivateFieldSet(this, _DurabilitySemaphoreServer_roundScheduled, false, "f");
|
|
207
209
|
const onError = (error) => reportFailure('durability.semaphore.round_failed', {}, error);
|
|
208
210
|
__classPrivateFieldSet(this, _DurabilitySemaphoreServer_round, __classPrivateFieldGet(this, _DurabilitySemaphoreServer_instances, "m", _DurabilitySemaphoreServer_runRound).call(this)
|
|
209
211
|
.catch(onError)
|
|
@@ -214,7 +216,7 @@ _DurabilitySemaphoreServer_engine = new WeakMap(), _DurabilitySemaphoreServer_st
|
|
|
214
216
|
}
|
|
215
217
|
}), "f");
|
|
216
218
|
__classPrivateFieldGet(this, _DurabilitySemaphoreServer_engine, "f").waitUntil(__classPrivateFieldGet(this, _DurabilitySemaphoreServer_round, "f"), onError);
|
|
217
|
-
}, delay)
|
|
219
|
+
}, delay);
|
|
218
220
|
}, _DurabilitySemaphoreServer_runRound = async function _DurabilitySemaphoreServer_runRound() {
|
|
219
221
|
const pending = __classPrivateFieldGet(this, _DurabilitySemaphoreServer_pending, "f");
|
|
220
222
|
__classPrivateFieldSet(this, _DurabilitySemaphoreServer_pending, [], "f");
|
|
@@ -307,9 +309,15 @@ _DurabilitySemaphoreServer_engine = new WeakMap(), _DurabilitySemaphoreServer_st
|
|
|
307
309
|
? budgets(__classPrivateFieldGet(this, _DurabilitySemaphoreServer_leases, "f").provisioning, limit)
|
|
308
310
|
: { grants: limit, leases: 0, shared: false };
|
|
309
311
|
let leased = 0;
|
|
310
|
-
|
|
312
|
+
// A group's limit counts its live lease footprint in every mode, retired
|
|
313
|
+
// leases included until their clients confirm. Kept apart from `used`,
|
|
314
|
+
// which the lease pass reads as grants only.
|
|
315
|
+
const leasedByGroup = new Map();
|
|
316
|
+
if (__classPrivateFieldGet(this, _DurabilitySemaphoreServer_leases, "f")) {
|
|
311
317
|
for (const lease of store.listLeases()) {
|
|
312
|
-
|
|
318
|
+
const footprint = leaseFootprint(lease);
|
|
319
|
+
leased += footprint;
|
|
320
|
+
leasedByGroup.set(lease.grp, (leasedByGroup.get(lease.grp) ?? 0) + footprint);
|
|
313
321
|
}
|
|
314
322
|
}
|
|
315
323
|
let remaining = budget.grants - total - (budget.shared ? leased : 0);
|
|
@@ -332,7 +340,9 @@ _DurabilitySemaphoreServer_engine = new WeakMap(), _DurabilitySemaphoreServer_st
|
|
|
332
340
|
continue;
|
|
333
341
|
}
|
|
334
342
|
const groupUsed = used.get(demand.group) ?? 0;
|
|
335
|
-
const groupRemaining = policy.limit === undefined
|
|
343
|
+
const groupRemaining = policy.limit === undefined
|
|
344
|
+
? Infinity
|
|
345
|
+
: policy.limit - groupUsed - (leasedByGroup.get(demand.group) ?? 0);
|
|
336
346
|
const give = Math.max(0, Math.min(demand.count, groupRemaining, remaining));
|
|
337
347
|
if (give > 0) {
|
|
338
348
|
const grant = {
|