rate-budget 0.1.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/LICENSE +21 -0
- package/README.md +238 -0
- package/dist/cjs/index.js +21 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/limits.js +185 -0
- package/dist/cjs/limits.js.map +1 -0
- package/dist/cjs/memory-cooldown.js +81 -0
- package/dist/cjs/memory-cooldown.js.map +1 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/retry.js +82 -0
- package/dist/cjs/retry.js.map +1 -0
- package/dist/cjs/token-budget.js +46 -0
- package/dist/cjs/token-budget.js.map +1 -0
- package/dist/esm/index.d.ts +5 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +5 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/limits.d.ts +91 -0
- package/dist/esm/limits.d.ts.map +1 -0
- package/dist/esm/limits.js +170 -0
- package/dist/esm/limits.js.map +1 -0
- package/dist/esm/memory-cooldown.d.ts +38 -0
- package/dist/esm/memory-cooldown.d.ts.map +1 -0
- package/dist/esm/memory-cooldown.js +76 -0
- package/dist/esm/memory-cooldown.js.map +1 -0
- package/dist/esm/retry.d.ts +29 -0
- package/dist/esm/retry.d.ts.map +1 -0
- package/dist/esm/retry.js +75 -0
- package/dist/esm/retry.js.map +1 -0
- package/dist/esm/token-budget.d.ts +19 -0
- package/dist/esm/token-budget.d.ts.map +1 -0
- package/dist/esm/token-budget.js +41 -0
- package/dist/esm/token-budget.js.map +1 -0
- package/package.json +48 -0
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { DEFAULT_LIMIT_WINDOW_MS, toNonNegativeInteger, toPositiveInteger, } from "./limits.js";
|
|
2
|
+
function normalizeRatio(value) {
|
|
3
|
+
if (!Number.isFinite(value)) {
|
|
4
|
+
return 0;
|
|
5
|
+
}
|
|
6
|
+
return Math.min(1, Math.max(0, value));
|
|
7
|
+
}
|
|
8
|
+
export function calculatePerRequestTokenBudget(input) {
|
|
9
|
+
const tokensPerWindow = toPositiveInteger(input.tokensPerWindow);
|
|
10
|
+
const requestSpacingMs = toPositiveInteger(input.requestSpacingMs);
|
|
11
|
+
const windowMs = toPositiveInteger(input.windowMs ?? DEFAULT_LIMIT_WINDOW_MS, DEFAULT_LIMIT_WINDOW_MS);
|
|
12
|
+
const requestsPerWindow = windowMs / requestSpacingMs;
|
|
13
|
+
const minimumTokens = toPositiveInteger(input.minimumTokens ?? 1);
|
|
14
|
+
return Math.max(minimumTokens, Math.floor(tokensPerWindow / Math.max(1, requestsPerWindow)));
|
|
15
|
+
}
|
|
16
|
+
export function calculateTokenBudgetPortion(totalBudget, ratio) {
|
|
17
|
+
return Math.max(1, Math.floor(toPositiveInteger(totalBudget) * normalizeRatio(ratio)));
|
|
18
|
+
}
|
|
19
|
+
export function distributeFlexibleTokenBudget(input) {
|
|
20
|
+
const parentBudget = toPositiveInteger(input.parentBudget);
|
|
21
|
+
let remainingTokens = toNonNegativeInteger(input.availableTokens);
|
|
22
|
+
const allocations = Object.create(null);
|
|
23
|
+
for (const partition of input.partitions) {
|
|
24
|
+
const targetBudget = Math.floor(parentBudget * normalizeRatio(partition.targetRatio));
|
|
25
|
+
const allocated = Math.min(targetBudget, remainingTokens);
|
|
26
|
+
allocations[partition.name] = allocated;
|
|
27
|
+
remainingTokens -= allocated;
|
|
28
|
+
}
|
|
29
|
+
for (const partition of input.partitions) {
|
|
30
|
+
if (remainingTokens <= 0) {
|
|
31
|
+
break;
|
|
32
|
+
}
|
|
33
|
+
const currentAllocation = allocations[partition.name] ?? 0;
|
|
34
|
+
const maxBudget = Math.max(currentAllocation, Math.floor(parentBudget * normalizeRatio(partition.maxRatio)));
|
|
35
|
+
const additionalTokens = Math.min(maxBudget - currentAllocation, remainingTokens);
|
|
36
|
+
allocations[partition.name] = currentAllocation + additionalTokens;
|
|
37
|
+
remainingTokens -= additionalTokens;
|
|
38
|
+
}
|
|
39
|
+
return allocations;
|
|
40
|
+
}
|
|
41
|
+
//# sourceMappingURL=token-budget.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"token-budget.js","sourceRoot":"","sources":["../../src/token-budget.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,uBAAuB,EACvB,oBAAoB,EACpB,iBAAiB,GAClB,MAAM,aAAa,CAAC;AAerB,SAAS,cAAc,CAAC,KAAa;IACnC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC;QAC5B,OAAO,CAAC,CAAC;IACX,CAAC;IAED,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC,CAAC;AACzC,CAAC;AAED,MAAM,UAAU,8BAA8B,CAC5C,KAAiC;IAEjC,MAAM,eAAe,GAAG,iBAAiB,CAAC,KAAK,CAAC,eAAe,CAAC,CAAC;IACjE,MAAM,gBAAgB,GAAG,iBAAiB,CAAC,KAAK,CAAC,gBAAgB,CAAC,CAAC;IACnE,MAAM,QAAQ,GAAG,iBAAiB,CAChC,KAAK,CAAC,QAAQ,IAAI,uBAAuB,EACzC,uBAAuB,CACxB,CAAC;IACF,MAAM,iBAAiB,GAAG,QAAQ,GAAG,gBAAgB,CAAC;IACtD,MAAM,aAAa,GAAG,iBAAiB,CAAC,KAAK,CAAC,aAAa,IAAI,CAAC,CAAC,CAAC;IAElE,OAAO,IAAI,CAAC,GAAG,CACb,aAAa,EACb,IAAI,CAAC,KAAK,CAAC,eAAe,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,iBAAiB,CAAC,CAAC,CAC7D,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,2BAA2B,CACzC,WAAmB,EACnB,KAAa;IAEb,OAAO,IAAI,CAAC,GAAG,CACb,CAAC,EACD,IAAI,CAAC,KAAK,CAAC,iBAAiB,CAAC,WAAW,CAAC,GAAG,cAAc,CAAC,KAAK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C;AACrB,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "rate-budget",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Provider-agnostic, storage-agnostic budget and rate limiting primitives for requests, tokens, quotas, cooldowns, and retries.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"main": "./dist/cjs/index.js",
|
|
9
|
+
"module": "./dist/esm/index.js",
|
|
10
|
+
"types": "./dist/esm/index.d.ts",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./dist/esm/index.d.ts",
|
|
14
|
+
"import": "./dist/esm/index.js",
|
|
15
|
+
"require": "./dist/cjs/index.js"
|
|
16
|
+
},
|
|
17
|
+
"./package.json": "./package.json"
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist",
|
|
21
|
+
"README.md",
|
|
22
|
+
"LICENSE"
|
|
23
|
+
],
|
|
24
|
+
"scripts": {
|
|
25
|
+
"clean": "node scripts/clean.cjs",
|
|
26
|
+
"build": "node scripts/clean.cjs && tsc -p tsconfig.esm.json && tsc -p tsconfig.cjs.json && node scripts/write-cjs-package.cjs",
|
|
27
|
+
"test": "node scripts/clean.cjs && tsc -p tsconfig.esm.json && tsc -p tsconfig.cjs.json && node scripts/write-cjs-package.cjs && node --test test/*.test.mjs",
|
|
28
|
+
"prepack": "npm run build"
|
|
29
|
+
},
|
|
30
|
+
"keywords": [
|
|
31
|
+
"rate-limit",
|
|
32
|
+
"rate-limiter",
|
|
33
|
+
"rate-budget",
|
|
34
|
+
"quota",
|
|
35
|
+
"throttle",
|
|
36
|
+
"retry",
|
|
37
|
+
"token-budget",
|
|
38
|
+
"rpm",
|
|
39
|
+
"tpm",
|
|
40
|
+
"rpd"
|
|
41
|
+
],
|
|
42
|
+
"engines": {
|
|
43
|
+
"node": ">=18"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"typescript": "^5.0.0"
|
|
47
|
+
}
|
|
48
|
+
}
|