async-bulkhead-llm 3.14.0 → 3.15.1
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/CHANGELOG.md +900 -0
- package/README.md +138 -28
- package/dist/cjs/bulkhead.cjs +228 -26
- package/dist/cjs/bulkhead.cjs.map +1 -1
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/esm/bulkhead.js +228 -26
- package/dist/esm/bulkhead.js.map +1 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/types/bulkhead.d.ts.map +1 -1
- package/dist/types/index.d.ts +1 -1
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/types.d.ts +63 -11
- package/dist/types/types.d.ts.map +1 -1
- package/package.json +5 -2
package/README.md
CHANGED
|
@@ -9,7 +9,8 @@ Designed for services that need to enforce **cost ceilings, concurrency limits,
|
|
|
9
9
|
## Features
|
|
10
10
|
|
|
11
11
|
- ✅ Hard **max in-flight** concurrency (`maxConcurrent`)
|
|
12
|
-
- ✅ **Atomic versioned limit updates** — apply concurrency, queue, token budget, priority reserve, and admission-class ceilings as one revisioned snapshot
|
|
12
|
+
- ✅ **Atomic versioned limit updates** — apply concurrency, queue, token budget, priority reserve, and admission-class floors/ceilings as one revisioned snapshot
|
|
13
|
+
- ✅ **Protected admission-class floors** — reserve per-class concurrency and token capacity before requests borrow from the shared remainder
|
|
13
14
|
- ✅ **Bounded admission classes** — isolate policy classes with hard per-class concurrency and in-flight token ceilings
|
|
14
15
|
- ✅ **Admission-linearized revisions** — every admitted or bypassed lifecycle retains the exact limit revision that made the decision
|
|
15
16
|
- ✅ **Token-aware admission** — reserves against estimated input + max output tokens
|
|
@@ -136,6 +137,57 @@ const result = await bulkhead.run(request, async () => {
|
|
|
136
137
|
|
|
137
138
|
---
|
|
138
139
|
|
|
140
|
+
## What's New in v3.15 — Protected admission-class floors
|
|
141
|
+
|
|
142
|
+
v3.15 lets each statically configured admission class reserve a strict local
|
|
143
|
+
concurrency floor and, when token admission is enabled, a strict in-flight
|
|
144
|
+
token floor. Capacity above all floors forms a shared remainder that any class
|
|
145
|
+
may borrow, subject to its hard class ceilings and the enclosing global limits.
|
|
146
|
+
|
|
147
|
+
```ts
|
|
148
|
+
const bulkhead = createLLMBulkhead({
|
|
149
|
+
model: "gpt-4o",
|
|
150
|
+
maxConcurrent: 12,
|
|
151
|
+
tokenBudget: { budget: 24_000 },
|
|
152
|
+
admissionClasses: {
|
|
153
|
+
defaultClass: "standard",
|
|
154
|
+
classes: {
|
|
155
|
+
premium: {
|
|
156
|
+
protectedConcurrent: 6,
|
|
157
|
+
maxConcurrent: 10,
|
|
158
|
+
protectedInFlightTokens: 12_000,
|
|
159
|
+
maxInFlightTokens: 20_000,
|
|
160
|
+
},
|
|
161
|
+
standard: {
|
|
162
|
+
protectedConcurrent: 2,
|
|
163
|
+
maxConcurrent: 6,
|
|
164
|
+
protectedInFlightTokens: 4_000,
|
|
165
|
+
maxInFlightTokens: 10_000,
|
|
166
|
+
},
|
|
167
|
+
},
|
|
168
|
+
},
|
|
169
|
+
});
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
In this example, eight concurrency slots and 16,000 tokens are protected. The
|
|
173
|
+
remaining four slots and 8,000 tokens are shared. Premium may exceed its floor
|
|
174
|
+
by borrowing from that shared remainder, but it cannot consume the two slots
|
|
175
|
+
or 4,000 tokens protected for standard.
|
|
176
|
+
|
|
177
|
+
Floors are strict reservations, not idle-capacity lending. Unused protected
|
|
178
|
+
capacity remains available only to its owning class. Cross-class lending of an
|
|
179
|
+
idle floor requires a gateway or control plane to install a higher-revision
|
|
180
|
+
snapshot. Increasing a floor never revokes active work: new borrowing pauses
|
|
181
|
+
until completions restore the guarantee by attrition.
|
|
182
|
+
|
|
183
|
+
`stats().admissionClasses` now reports the shared remainder plus each class's
|
|
184
|
+
protected and borrowed usage. Rejections caused by preserving another class's
|
|
185
|
+
floor use `constraint: "admission_class_protection"` and include the shared
|
|
186
|
+
capacity snapshot. Omitting both floor fields preserves v3.14 admission
|
|
187
|
+
behavior.
|
|
188
|
+
|
|
189
|
+
---
|
|
190
|
+
|
|
139
191
|
## What's New in v3.14 — Bounded admission classes
|
|
140
192
|
|
|
141
193
|
v3.14 adds a bounded hierarchical admission layer inside one bulkhead. A
|
|
@@ -873,11 +925,12 @@ const bulkhead = createLLMBulkhead({
|
|
|
873
925
|
|
|
874
926
|
---
|
|
875
927
|
|
|
876
|
-
## Bounded Admission Classes
|
|
928
|
+
## Protected and Bounded Admission Classes
|
|
877
929
|
|
|
878
|
-
Admission classes add a
|
|
879
|
-
bulkhead.
|
|
880
|
-
|
|
930
|
+
Admission classes add a statically bounded policy layer beneath the global
|
|
931
|
+
bulkhead. Each class may define strict protected floors, hard ceilings, or both.
|
|
932
|
+
A request must satisfy its class policy as well as the enclosing global
|
|
933
|
+
concurrency and token limits.
|
|
881
934
|
|
|
882
935
|
```ts
|
|
883
936
|
const bulkhead = createLLMBulkhead({
|
|
@@ -888,16 +941,20 @@ const bulkhead = createLLMBulkhead({
|
|
|
888
941
|
defaultClass: 'standard',
|
|
889
942
|
classes: {
|
|
890
943
|
latencySensitive: {
|
|
891
|
-
|
|
892
|
-
|
|
944
|
+
protectedConcurrent: 8,
|
|
945
|
+
maxConcurrent: 14,
|
|
946
|
+
protectedInFlightTokens: 20_000,
|
|
947
|
+
maxInFlightTokens: 36_000,
|
|
893
948
|
},
|
|
894
949
|
standard: {
|
|
895
|
-
|
|
896
|
-
|
|
950
|
+
protectedConcurrent: 3,
|
|
951
|
+
maxConcurrent: 8,
|
|
952
|
+
protectedInFlightTokens: 8_000,
|
|
953
|
+
maxInFlightTokens: 20_000,
|
|
897
954
|
},
|
|
898
955
|
background: {
|
|
899
|
-
maxConcurrent:
|
|
900
|
-
maxInFlightTokens:
|
|
956
|
+
maxConcurrent: 4,
|
|
957
|
+
maxInFlightTokens: 10_000,
|
|
901
958
|
},
|
|
902
959
|
},
|
|
903
960
|
},
|
|
@@ -908,22 +965,46 @@ await bulkhead.run(request, callProvider, {
|
|
|
908
965
|
});
|
|
909
966
|
```
|
|
910
967
|
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
968
|
+
`protectedConcurrent` and `protectedInFlightTokens` reserve capacity for the
|
|
969
|
+
owning class. The sum of concurrency floors must not exceed
|
|
970
|
+
`maxConcurrent`; the sum of token floors must not exceed
|
|
971
|
+
`tokenBudget.budget`. A floor may not exceed the same class's hard ceiling.
|
|
972
|
+
Invalid construction or reconfiguration snapshots throw before any limit is
|
|
973
|
+
installed.
|
|
974
|
+
|
|
975
|
+
After all floors are subtracted, the remaining capacity is shared. A class
|
|
976
|
+
uses its protected capacity first, then borrows from the shared remainder.
|
|
977
|
+
Borrowing remains subject to `maxConcurrent`, `maxInFlightTokens`, the global
|
|
978
|
+
limits, and the priority-adjusted token budget. A rejection required to keep
|
|
979
|
+
another class's floor available reports
|
|
980
|
+
`constraint: 'admission_class_protection'`.
|
|
981
|
+
|
|
982
|
+
Floors are deliberately strict: an idle class's protected capacity is not
|
|
983
|
+
automatically lent to another class. A gateway or control plane may implement
|
|
984
|
+
demand-aware lending by atomically applying a newer snapshot with adjusted
|
|
985
|
+
floors. Raising a floor does not cancel work that is already active. Existing
|
|
986
|
+
borrowers drain normally, while new shared-capacity admissions pause until the
|
|
987
|
+
new floor is restored by attrition.
|
|
988
|
+
|
|
989
|
+
Omitting a floor is equivalent to zero. Omitting a hard ceiling means that
|
|
990
|
+
dimension is governed only by the global bulkhead. Zero is valid for either a
|
|
991
|
+
floor or ceiling. Class checks remain fail-fast; `maxQueue` is still one global
|
|
992
|
+
queue and does not create per-class waiter queues.
|
|
993
|
+
|
|
994
|
+
The class table is fixed at construction, which bounds runtime state and metric
|
|
995
|
+
cardinality. Map authenticated tenants or applications to these trusted policy
|
|
996
|
+
buckets instead of using arbitrary identity values as class IDs. Identity
|
|
997
|
+
verification and distributed coordination remain outside this single-process
|
|
998
|
+
library. Admission classes partition deduplication automatically, but use
|
|
999
|
+
`dedupScope` when tenants inside the same class must not share results.
|
|
1000
|
+
|
|
1001
|
+
Telemetry includes:
|
|
1002
|
+
|
|
1003
|
+
- `stats().admissionClasses.shared` for the shared concurrency/token remainder.
|
|
1004
|
+
- Per-class protected and borrowed in-flight usage.
|
|
1005
|
+
- Cumulative admissions whose concurrency came from shared capacity.
|
|
1006
|
+
- Cumulative reservation tokens placed in shared capacity at admission.
|
|
1007
|
+
- Rejection details showing protected, borrowed, and available shared capacity.
|
|
927
1008
|
|
|
928
1009
|
---
|
|
929
1010
|
|
|
@@ -1336,6 +1417,16 @@ type LLMStats = {
|
|
|
1336
1417
|
};
|
|
1337
1418
|
admissionClasses?: {
|
|
1338
1419
|
defaultClass: string;
|
|
1420
|
+
shared: {
|
|
1421
|
+
maxConcurrent: number;
|
|
1422
|
+
inFlight: number;
|
|
1423
|
+
availableConcurrent: number;
|
|
1424
|
+
tokenBudget?: {
|
|
1425
|
+
budget: number;
|
|
1426
|
+
inFlightTokens: number;
|
|
1427
|
+
available: number;
|
|
1428
|
+
};
|
|
1429
|
+
};
|
|
1339
1430
|
classes: Record<string, LLMAdmissionClassStats>;
|
|
1340
1431
|
};
|
|
1341
1432
|
deduplication?: {
|
|
@@ -1409,8 +1500,10 @@ type LLMBulkheadOptions = {
|
|
|
1409
1500
|
admissionClasses?: {
|
|
1410
1501
|
defaultClass: string;
|
|
1411
1502
|
classes: Record<string, {
|
|
1503
|
+
protectedConcurrent?: number;
|
|
1412
1504
|
maxConcurrent?: number;
|
|
1413
|
-
|
|
1505
|
+
protectedInFlightTokens?: number; // requires tokenBudget
|
|
1506
|
+
maxInFlightTokens?: number; // requires tokenBudget
|
|
1414
1507
|
}>;
|
|
1415
1508
|
};
|
|
1416
1509
|
deduplication?: boolean | DeduplicationOptions;
|
|
@@ -1537,7 +1630,9 @@ type LLMAdmissionLimits = {
|
|
|
1537
1630
|
readonly highPriorityReserve: number;
|
|
1538
1631
|
};
|
|
1539
1632
|
readonly admissionClasses?: Readonly<Record<string, {
|
|
1633
|
+
readonly protectedConcurrent?: number;
|
|
1540
1634
|
readonly maxConcurrent?: number;
|
|
1635
|
+
readonly protectedInFlightTokens?: number;
|
|
1541
1636
|
readonly maxInFlightTokens?: number;
|
|
1542
1637
|
}>>;
|
|
1543
1638
|
};
|
|
@@ -1626,6 +1721,21 @@ type LLMRejectReason =
|
|
|
1626
1721
|
|
|
1627
1722
|
## Migration
|
|
1628
1723
|
|
|
1724
|
+
### v3.14 → v3.15
|
|
1725
|
+
|
|
1726
|
+
No admission call-site changes are required. Configure
|
|
1727
|
+
`protectedConcurrent` and/or `protectedInFlightTokens` only when a class needs
|
|
1728
|
+
a strict local guarantee. The sum of floors must fit inside the corresponding
|
|
1729
|
+
global limit. Floors are not automatically lent while idle; a control plane
|
|
1730
|
+
must adjust them with `applyLimits()` to implement demand-aware lending.
|
|
1731
|
+
|
|
1732
|
+
`stats().admissionClasses` gains `shared`, protected/borrowed usage fields, and
|
|
1733
|
+
cumulative borrowing counters. Rejection capacity details may now use
|
|
1734
|
+
`constraint: 'admission_class_protection'` and include `sharedCapacity`. Code
|
|
1735
|
+
that exhaustively asserts these additive object or union shapes must be
|
|
1736
|
+
updated. Omitting both floor fields preserves v3.14 admission behavior.
|
|
1737
|
+
|
|
1738
|
+
|
|
1629
1739
|
### v3.13 → v3.14
|
|
1630
1740
|
|
|
1631
1741
|
No existing call-site changes are required. `admissionClasses` is opt-in and
|
package/dist/cjs/bulkhead.cjs
CHANGED
|
@@ -36,18 +36,53 @@ function readAdmissionClassLimits(value, label, tokenBudgetEnabled) {
|
|
|
36
36
|
throw new Error(`${label} must be an object`);
|
|
37
37
|
}
|
|
38
38
|
const source = value;
|
|
39
|
+
const protectedConcurrent = source.protectedConcurrent;
|
|
39
40
|
const maxConcurrent = source.maxConcurrent;
|
|
41
|
+
const protectedInFlightTokens = source.protectedInFlightTokens;
|
|
40
42
|
const maxInFlightTokens = source.maxInFlightTokens;
|
|
43
|
+
(0, validation_js_1.assertOptionalNonNegativeInteger)(protectedConcurrent, `${label}.protectedConcurrent`);
|
|
41
44
|
(0, validation_js_1.assertOptionalNonNegativeInteger)(maxConcurrent, `${label}.maxConcurrent`);
|
|
45
|
+
(0, validation_js_1.assertOptionalNonNegativeInteger)(protectedInFlightTokens, `${label}.protectedInFlightTokens`);
|
|
42
46
|
(0, validation_js_1.assertOptionalNonNegativeInteger)(maxInFlightTokens, `${label}.maxInFlightTokens`);
|
|
43
|
-
if (
|
|
44
|
-
|
|
47
|
+
if (maxConcurrent !== undefined &&
|
|
48
|
+
(protectedConcurrent ?? 0) > maxConcurrent) {
|
|
49
|
+
throw new Error(`${label}.protectedConcurrent must be <= ${label}.maxConcurrent`);
|
|
50
|
+
}
|
|
51
|
+
if (maxInFlightTokens !== undefined &&
|
|
52
|
+
(protectedInFlightTokens ?? 0) > maxInFlightTokens) {
|
|
53
|
+
throw new Error(`${label}.protectedInFlightTokens must be <= ${label}.maxInFlightTokens`);
|
|
54
|
+
}
|
|
55
|
+
if (!tokenBudgetEnabled) {
|
|
56
|
+
if (protectedInFlightTokens !== undefined) {
|
|
57
|
+
throw new Error(`${label}.protectedInFlightTokens requires tokenBudget to be configured`);
|
|
58
|
+
}
|
|
59
|
+
if (maxInFlightTokens !== undefined) {
|
|
60
|
+
throw new Error(`${label}.maxInFlightTokens requires tokenBudget to be configured`);
|
|
61
|
+
}
|
|
45
62
|
}
|
|
46
63
|
return Object.freeze({
|
|
64
|
+
...(protectedConcurrent !== undefined && { protectedConcurrent }),
|
|
47
65
|
...(maxConcurrent !== undefined && { maxConcurrent }),
|
|
66
|
+
...(protectedInFlightTokens !== undefined && {
|
|
67
|
+
protectedInFlightTokens,
|
|
68
|
+
}),
|
|
48
69
|
...(maxInFlightTokens !== undefined && { maxInFlightTokens }),
|
|
49
70
|
});
|
|
50
71
|
}
|
|
72
|
+
function validateProtectedCapacity(limits, maxConcurrent, tokenBudget, label) {
|
|
73
|
+
let protectedConcurrent = 0;
|
|
74
|
+
let protectedTokens = 0;
|
|
75
|
+
for (const classLimits of limits) {
|
|
76
|
+
protectedConcurrent += classLimits.protectedConcurrent ?? 0;
|
|
77
|
+
protectedTokens += classLimits.protectedInFlightTokens ?? 0;
|
|
78
|
+
}
|
|
79
|
+
if (protectedConcurrent > maxConcurrent) {
|
|
80
|
+
throw new Error(`${label} protectedConcurrent sum must be <= maxConcurrent`);
|
|
81
|
+
}
|
|
82
|
+
if (tokenBudget !== undefined && protectedTokens > tokenBudget) {
|
|
83
|
+
throw new Error(`${label} protectedInFlightTokens sum must be <= tokenBudget.budget`);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
51
86
|
// ────────────────────────────────────────────
|
|
52
87
|
// createLLMBulkhead
|
|
53
88
|
// ────────────────────────────────────────────
|
|
@@ -87,15 +122,19 @@ function createLLMBulkhead(opts) {
|
|
|
87
122
|
throw new Error("admissionClasses.classes must be an object");
|
|
88
123
|
}
|
|
89
124
|
const entries = Object.entries(classes);
|
|
125
|
+
const validatedClassLimits = [];
|
|
90
126
|
if (entries.length === 0) {
|
|
91
127
|
throw new Error("admissionClasses.classes must contain at least one class");
|
|
92
128
|
}
|
|
93
129
|
for (const [id, rawLimits] of entries) {
|
|
94
130
|
validateAdmissionClassId(id, "admissionClasses class id");
|
|
95
131
|
const limits = readAdmissionClassLimits(rawLimits, `admissionClasses.classes[${JSON.stringify(id)}]`, opts.tokenBudget !== undefined);
|
|
132
|
+
validatedClassLimits.push(limits);
|
|
96
133
|
admissionClassStates.set(id, {
|
|
97
134
|
id,
|
|
135
|
+
protectedConcurrent: limits.protectedConcurrent ?? 0,
|
|
98
136
|
maxConcurrent: limits.maxConcurrent,
|
|
137
|
+
protectedInFlightTokens: limits.protectedInFlightTokens ?? 0,
|
|
99
138
|
maxInFlightTokens: limits.maxInFlightTokens,
|
|
100
139
|
inFlight: 0,
|
|
101
140
|
inFlightTokens: 0,
|
|
@@ -107,12 +146,15 @@ function createLLMBulkhead(opts) {
|
|
|
107
146
|
totalConsumed: 0,
|
|
108
147
|
totalRefunded: 0,
|
|
109
148
|
totalOverrun: 0,
|
|
149
|
+
totalBorrowedAdmissions: 0,
|
|
150
|
+
totalBorrowedTokensReserved: 0,
|
|
110
151
|
});
|
|
111
152
|
}
|
|
112
153
|
if (!admissionClassStates.has(defaultAdmissionClass)) {
|
|
113
154
|
throw new Error(`admissionClasses.defaultClass ${JSON.stringify(defaultAdmissionClass)} ` +
|
|
114
155
|
`must name a configured class`);
|
|
115
156
|
}
|
|
157
|
+
validateProtectedCapacity(validatedClassLimits, opts.maxConcurrent, opts.tokenBudget?.budget, "admissionClasses");
|
|
116
158
|
}
|
|
117
159
|
// ---- Internal reconfigurable concurrency bulkhead ----
|
|
118
160
|
const bulkhead = (0, reconfigurable_bulkhead_js_1.createReconfigurableBulkhead)({
|
|
@@ -214,9 +256,15 @@ function createLLMBulkhead(opts) {
|
|
|
214
256
|
entries.push([
|
|
215
257
|
id,
|
|
216
258
|
Object.freeze({
|
|
259
|
+
...(state.protectedConcurrent > 0 && {
|
|
260
|
+
protectedConcurrent: state.protectedConcurrent,
|
|
261
|
+
}),
|
|
217
262
|
...(state.maxConcurrent !== undefined && {
|
|
218
263
|
maxConcurrent: state.maxConcurrent,
|
|
219
264
|
}),
|
|
265
|
+
...(state.protectedInFlightTokens > 0 && {
|
|
266
|
+
protectedInFlightTokens: state.protectedInFlightTokens,
|
|
267
|
+
}),
|
|
220
268
|
...(state.maxInFlightTokens !== undefined && {
|
|
221
269
|
maxInFlightTokens: state.maxInFlightTokens,
|
|
222
270
|
}),
|
|
@@ -366,7 +414,52 @@ function createLLMBulkhead(opts) {
|
|
|
366
414
|
? currentBudget
|
|
367
415
|
: Math.max(0, currentBudget - currentHighPriorityReserve);
|
|
368
416
|
}
|
|
369
|
-
function
|
|
417
|
+
function protectedConcurrentTotal() {
|
|
418
|
+
let total = 0;
|
|
419
|
+
for (const state of admissionClassStates.values()) {
|
|
420
|
+
total += state.protectedConcurrent;
|
|
421
|
+
}
|
|
422
|
+
return total;
|
|
423
|
+
}
|
|
424
|
+
function protectedTokensTotal() {
|
|
425
|
+
let total = 0;
|
|
426
|
+
for (const state of admissionClassStates.values()) {
|
|
427
|
+
total += state.protectedInFlightTokens;
|
|
428
|
+
}
|
|
429
|
+
return total;
|
|
430
|
+
}
|
|
431
|
+
function protectedConcurrentInUse(state) {
|
|
432
|
+
return Math.min(state.inFlight, state.protectedConcurrent);
|
|
433
|
+
}
|
|
434
|
+
function protectedTokensInUse(state) {
|
|
435
|
+
return Math.min(state.inFlightTokens, state.protectedInFlightTokens);
|
|
436
|
+
}
|
|
437
|
+
function sharedConcurrentInUse() {
|
|
438
|
+
let protectedInUse = 0;
|
|
439
|
+
for (const state of admissionClassStates.values()) {
|
|
440
|
+
protectedInUse += protectedConcurrentInUse(state);
|
|
441
|
+
}
|
|
442
|
+
return Math.max(0, bulkhead.stats().inFlight - protectedInUse);
|
|
443
|
+
}
|
|
444
|
+
function sharedTokensInUse() {
|
|
445
|
+
let protectedInUse = 0;
|
|
446
|
+
for (const state of admissionClassStates.values()) {
|
|
447
|
+
protectedInUse += protectedTokensInUse(state);
|
|
448
|
+
}
|
|
449
|
+
return Math.max(0, inFlightTokens - protectedInUse);
|
|
450
|
+
}
|
|
451
|
+
function requestedBorrowedConcurrent(admissionClass) {
|
|
452
|
+
if (admissionClass === undefined)
|
|
453
|
+
return 0;
|
|
454
|
+
return admissionClass.inFlight < admissionClass.protectedConcurrent ? 0 : 1;
|
|
455
|
+
}
|
|
456
|
+
function requestedBorrowedTokens(admissionClass, reserved) {
|
|
457
|
+
if (admissionClass === undefined || reserved === 0)
|
|
458
|
+
return 0;
|
|
459
|
+
const protectedAvailable = Math.max(0, admissionClass.protectedInFlightTokens - admissionClass.inFlightTokens);
|
|
460
|
+
return Math.max(0, reserved - protectedAvailable);
|
|
461
|
+
}
|
|
462
|
+
function checkAdmissionCapacity(reserved, priority, admissionClass, globalSlotHeld = false) {
|
|
370
463
|
if (budget && inFlightTokens + reserved > effectiveBudget(priority)) {
|
|
371
464
|
return { reason: "budget_limit", constraint: "global" };
|
|
372
465
|
}
|
|
@@ -379,6 +472,40 @@ function createLLMBulkhead(opts) {
|
|
|
379
472
|
admissionClass.inFlight >= admissionClass.maxConcurrent) {
|
|
380
473
|
return { reason: "concurrency_limit", constraint: "admission_class" };
|
|
381
474
|
}
|
|
475
|
+
if (admissionClass !== undefined) {
|
|
476
|
+
const totalProtectedConcurrent = protectedConcurrentTotal();
|
|
477
|
+
if (totalProtectedConcurrent > 0) {
|
|
478
|
+
const sharedConcurrentCapacity = Math.max(0, bulkhead.stats().maxConcurrent - totalProtectedConcurrent);
|
|
479
|
+
const globalInFlightBefore = Math.max(0, bulkhead.stats().inFlight - (globalSlotHeld ? 1 : 0));
|
|
480
|
+
let protectedConcurrentUsed = 0;
|
|
481
|
+
for (const state of admissionClassStates.values()) {
|
|
482
|
+
protectedConcurrentUsed += protectedConcurrentInUse(state);
|
|
483
|
+
}
|
|
484
|
+
const sharedConcurrentBefore = Math.max(0, globalInFlightBefore - protectedConcurrentUsed);
|
|
485
|
+
const borrowedConcurrentRequested = requestedBorrowedConcurrent(admissionClass);
|
|
486
|
+
if (borrowedConcurrentRequested > 0 &&
|
|
487
|
+
sharedConcurrentBefore + borrowedConcurrentRequested >
|
|
488
|
+
sharedConcurrentCapacity) {
|
|
489
|
+
return {
|
|
490
|
+
reason: "concurrency_limit",
|
|
491
|
+
constraint: "admission_class_protection",
|
|
492
|
+
};
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
const totalProtectedTokens = protectedTokensTotal();
|
|
496
|
+
if (budget && totalProtectedTokens > 0) {
|
|
497
|
+
const sharedTokenCapacity = Math.max(0, effectiveBudget(priority) - totalProtectedTokens);
|
|
498
|
+
const borrowedTokensRequested = requestedBorrowedTokens(admissionClass, reserved);
|
|
499
|
+
if (borrowedTokensRequested > 0 &&
|
|
500
|
+
sharedTokensInUse() + borrowedTokensRequested >
|
|
501
|
+
sharedTokenCapacity) {
|
|
502
|
+
return {
|
|
503
|
+
reason: "budget_limit",
|
|
504
|
+
constraint: "admission_class_protection",
|
|
505
|
+
};
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
}
|
|
382
509
|
return undefined;
|
|
383
510
|
}
|
|
384
511
|
/**
|
|
@@ -386,7 +513,7 @@ function createLLMBulkhead(opts) {
|
|
|
386
513
|
* concurrency slot is held. No counters are changed unless all checks pass.
|
|
387
514
|
*/
|
|
388
515
|
function tryReserveCapacity(reserved, priority, admissionClass) {
|
|
389
|
-
const failure = checkAdmissionCapacity(reserved, priority, admissionClass);
|
|
516
|
+
const failure = checkAdmissionCapacity(reserved, priority, admissionClass, true);
|
|
390
517
|
if (failure !== undefined)
|
|
391
518
|
return failure;
|
|
392
519
|
if (budget) {
|
|
@@ -394,8 +521,12 @@ function createLLMBulkhead(opts) {
|
|
|
394
521
|
totalReserved += reserved;
|
|
395
522
|
}
|
|
396
523
|
if (admissionClass !== undefined) {
|
|
524
|
+
const borrowedConcurrent = requestedBorrowedConcurrent(admissionClass);
|
|
525
|
+
const borrowedTokens = requestedBorrowedTokens(admissionClass, reserved);
|
|
397
526
|
admissionClass.inFlight++;
|
|
398
527
|
admissionClass.admitted++;
|
|
528
|
+
admissionClass.totalBorrowedAdmissions += borrowedConcurrent;
|
|
529
|
+
admissionClass.totalBorrowedTokensReserved += borrowedTokens;
|
|
399
530
|
if (budget) {
|
|
400
531
|
admissionClass.inFlightTokens += reserved;
|
|
401
532
|
admissionClass.totalReserved += reserved;
|
|
@@ -451,9 +582,35 @@ function createLLMBulkhead(opts) {
|
|
|
451
582
|
};
|
|
452
583
|
}
|
|
453
584
|
if (admissionClass !== undefined) {
|
|
585
|
+
const classProtectedConcurrentInUse = protectedConcurrentInUse(admissionClass);
|
|
586
|
+
const classProtectedTokensInUse = protectedTokensInUse(admissionClass);
|
|
587
|
+
const sharedConcurrentCapacity = Math.max(0, base.maxConcurrent - protectedConcurrentTotal());
|
|
588
|
+
const sharedConcurrentUsed = sharedConcurrentInUse();
|
|
589
|
+
detail.sharedCapacity = {
|
|
590
|
+
concurrency: {
|
|
591
|
+
capacity: sharedConcurrentCapacity,
|
|
592
|
+
inUse: sharedConcurrentUsed,
|
|
593
|
+
available: Math.max(0, sharedConcurrentCapacity - sharedConcurrentUsed),
|
|
594
|
+
requestedBorrowed: requestedBorrowedConcurrent(admissionClass),
|
|
595
|
+
},
|
|
596
|
+
...(budget
|
|
597
|
+
? {
|
|
598
|
+
tokenBudget: {
|
|
599
|
+
capacity: Math.max(0, effectiveBudget(priority) - protectedTokensTotal()),
|
|
600
|
+
inUse: sharedTokensInUse(),
|
|
601
|
+
available: Math.max(0, Math.max(0, effectiveBudget(priority) - protectedTokensTotal()) - sharedTokensInUse()),
|
|
602
|
+
requestedBorrowed: requestedBorrowedTokens(admissionClass, requested),
|
|
603
|
+
},
|
|
604
|
+
}
|
|
605
|
+
: {}),
|
|
606
|
+
};
|
|
454
607
|
detail.admissionClass = {
|
|
455
608
|
id: admissionClass.id,
|
|
456
609
|
inFlight: admissionClass.inFlight,
|
|
610
|
+
protectedConcurrent: admissionClass.protectedConcurrent,
|
|
611
|
+
protectedConcurrentInUse: classProtectedConcurrentInUse,
|
|
612
|
+
borrowedConcurrent: Math.max(0, admissionClass.inFlight - classProtectedConcurrentInUse),
|
|
613
|
+
availableProtectedConcurrent: Math.max(0, admissionClass.protectedConcurrent - admissionClass.inFlight),
|
|
457
614
|
maxConcurrent: admissionClass.maxConcurrent ?? null,
|
|
458
615
|
availableConcurrent: admissionClass.maxConcurrent === undefined
|
|
459
616
|
? null
|
|
@@ -462,6 +619,11 @@ function createLLMBulkhead(opts) {
|
|
|
462
619
|
? {
|
|
463
620
|
tokenBudget: {
|
|
464
621
|
inFlightTokens: admissionClass.inFlightTokens,
|
|
622
|
+
protectedInFlightTokens: admissionClass.protectedInFlightTokens,
|
|
623
|
+
protectedTokensInUse: classProtectedTokensInUse,
|
|
624
|
+
borrowedInFlightTokens: Math.max(0, admissionClass.inFlightTokens - classProtectedTokensInUse),
|
|
625
|
+
availableProtectedTokens: Math.max(0, admissionClass.protectedInFlightTokens -
|
|
626
|
+
admissionClass.inFlightTokens),
|
|
465
627
|
maxInFlightTokens: admissionClass.maxInFlightTokens ?? null,
|
|
466
628
|
available: admissionClass.maxInFlightTokens === undefined
|
|
467
629
|
? null
|
|
@@ -604,6 +766,9 @@ function createLLMBulkhead(opts) {
|
|
|
604
766
|
else if (nextAdmissionClasses !== undefined) {
|
|
605
767
|
throw new Error("limits.admissionClasses must be omitted because admissionClasses was not configured at construction");
|
|
606
768
|
}
|
|
769
|
+
if (preparedAdmissionClassLimits !== undefined) {
|
|
770
|
+
validateProtectedCapacity(preparedAdmissionClassLimits.values(), maxConcurrent, nextBudget, "limits.admissionClasses");
|
|
771
|
+
}
|
|
607
772
|
const previous = limits();
|
|
608
773
|
// Install every LLM-layer value before pumping the concurrency queue.
|
|
609
774
|
// Promise continuations from admitted waiters run only after this
|
|
@@ -616,7 +781,10 @@ function createLLMBulkhead(opts) {
|
|
|
616
781
|
if (preparedAdmissionClassLimits !== undefined) {
|
|
617
782
|
for (const [id, nextLimits] of preparedAdmissionClassLimits) {
|
|
618
783
|
const state = admissionClassStates.get(id);
|
|
784
|
+
state.protectedConcurrent = nextLimits.protectedConcurrent ?? 0;
|
|
619
785
|
state.maxConcurrent = nextLimits.maxConcurrent;
|
|
786
|
+
state.protectedInFlightTokens =
|
|
787
|
+
nextLimits.protectedInFlightTokens ?? 0;
|
|
620
788
|
state.maxInFlightTokens = nextLimits.maxInFlightTokens;
|
|
621
789
|
}
|
|
622
790
|
}
|
|
@@ -1443,31 +1611,65 @@ function createLLMBulkhead(opts) {
|
|
|
1443
1611
|
};
|
|
1444
1612
|
}
|
|
1445
1613
|
if (defaultAdmissionClass !== undefined) {
|
|
1614
|
+
const sharedConcurrentCapacity = Math.max(0, base.maxConcurrent - protectedConcurrentTotal());
|
|
1615
|
+
const sharedConcurrentUsed = sharedConcurrentInUse();
|
|
1616
|
+
const shared = {
|
|
1617
|
+
maxConcurrent: sharedConcurrentCapacity,
|
|
1618
|
+
inFlight: sharedConcurrentUsed,
|
|
1619
|
+
availableConcurrent: Math.max(0, sharedConcurrentCapacity - sharedConcurrentUsed),
|
|
1620
|
+
...(budget
|
|
1621
|
+
? {
|
|
1622
|
+
tokenBudget: {
|
|
1623
|
+
budget: Math.max(0, currentBudget - protectedTokensTotal()),
|
|
1624
|
+
inFlightTokens: sharedTokensInUse(),
|
|
1625
|
+
available: Math.max(0, Math.max(0, currentBudget - protectedTokensTotal()) -
|
|
1626
|
+
sharedTokensInUse()),
|
|
1627
|
+
},
|
|
1628
|
+
}
|
|
1629
|
+
: {}),
|
|
1630
|
+
};
|
|
1446
1631
|
result.admissionClasses = {
|
|
1447
1632
|
defaultClass: defaultAdmissionClass,
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1633
|
+
shared,
|
|
1634
|
+
classes: Object.fromEntries(Array.from(admissionClassStates, ([id, state]) => {
|
|
1635
|
+
const protectedConcurrency = protectedConcurrentInUse(state);
|
|
1636
|
+
const protectedTokens = protectedTokensInUse(state);
|
|
1637
|
+
return [
|
|
1638
|
+
id,
|
|
1639
|
+
{
|
|
1640
|
+
limits: {
|
|
1641
|
+
...(state.protectedConcurrent > 0 && {
|
|
1642
|
+
protectedConcurrent: state.protectedConcurrent,
|
|
1643
|
+
}),
|
|
1644
|
+
...(state.maxConcurrent !== undefined && {
|
|
1645
|
+
maxConcurrent: state.maxConcurrent,
|
|
1646
|
+
}),
|
|
1647
|
+
...(state.protectedInFlightTokens > 0 && {
|
|
1648
|
+
protectedInFlightTokens: state.protectedInFlightTokens,
|
|
1649
|
+
}),
|
|
1650
|
+
...(state.maxInFlightTokens !== undefined && {
|
|
1651
|
+
maxInFlightTokens: state.maxInFlightTokens,
|
|
1652
|
+
}),
|
|
1653
|
+
},
|
|
1654
|
+
inFlight: state.inFlight,
|
|
1655
|
+
protectedConcurrentInUse: protectedConcurrency,
|
|
1656
|
+
borrowedConcurrent: Math.max(0, state.inFlight - protectedConcurrency),
|
|
1657
|
+
inFlightTokens: state.inFlightTokens,
|
|
1658
|
+
protectedTokensInUse: protectedTokens,
|
|
1659
|
+
borrowedInFlightTokens: Math.max(0, state.inFlightTokens - protectedTokens),
|
|
1660
|
+
admitted: state.admitted,
|
|
1661
|
+
released: state.released,
|
|
1662
|
+
rejected: state.rejected,
|
|
1663
|
+
rejectedByReason: { ...state.rejectedByReason },
|
|
1664
|
+
totalReserved: state.totalReserved,
|
|
1665
|
+
totalConsumed: state.totalConsumed,
|
|
1666
|
+
totalRefunded: state.totalRefunded,
|
|
1667
|
+
totalOverrun: state.totalOverrun,
|
|
1668
|
+
totalBorrowedAdmissions: state.totalBorrowedAdmissions,
|
|
1669
|
+
totalBorrowedTokensReserved: state.totalBorrowedTokensReserved,
|
|
1458
1670
|
},
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
admitted: state.admitted,
|
|
1462
|
-
released: state.released,
|
|
1463
|
-
rejected: state.rejected,
|
|
1464
|
-
rejectedByReason: { ...state.rejectedByReason },
|
|
1465
|
-
totalReserved: state.totalReserved,
|
|
1466
|
-
totalConsumed: state.totalConsumed,
|
|
1467
|
-
totalRefunded: state.totalRefunded,
|
|
1468
|
-
totalOverrun: state.totalOverrun,
|
|
1469
|
-
},
|
|
1470
|
-
])),
|
|
1671
|
+
];
|
|
1672
|
+
})),
|
|
1471
1673
|
};
|
|
1472
1674
|
}
|
|
1473
1675
|
return result;
|