Skip to content

Commit b32e884

Browse files
committed
fix(traffic): normalize adaptive limiter state
1 parent 05cbc37 commit b32e884

1 file changed

Lines changed: 19 additions & 10 deletions

File tree

packages/core/src/traffic/traffic-adaptive-limiter.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,14 @@ export class TrafficAdaptiveLimiter {
8181
this.applyPenaltyDecay(state, now);
8282

8383
// If still in cooldown, instruct scheduler to wait
84-
if (state.cooldownUntil !== undefined && now < state.cooldownUntil) {
85-
return {
86-
kind: "wait",
87-
wakeUpAt: state.cooldownUntil,
88-
};
84+
if (state.cooldownUntil !== undefined) {
85+
if (now < state.cooldownUntil) {
86+
return {
87+
kind: "wait",
88+
wakeUpAt: state.cooldownUntil,
89+
};
90+
}
91+
state.cooldownUntil = undefined;
8992
}
9093

9194
return null;
@@ -245,13 +248,19 @@ export class TrafficAdaptiveLimiter {
245248
): string {
246249
const baseKey = rateLimitKeyOverride ?? this.buildRateLimitKey(metadata);
247250

248-
// If tenant is already encoded, do not duplicate
249-
if (baseKey.includes("tenant=")) {
250-
return baseKey;
251-
}
252-
253251
const tenant = metadata?.tenantId ?? tenantId ?? "default";
254252

253+
const baseParts = baseKey.split("::");
254+
let updated = false;
255+
const normalizedParts = baseParts.map((part) => {
256+
if (!part.startsWith("tenant=")) return part;
257+
updated = true;
258+
return `tenant=${encodeURIComponent(tenant)}`;
259+
});
260+
if (updated) {
261+
return normalizedParts.join("::");
262+
}
263+
255264
return `${baseKey}::tenant=${encodeURIComponent(tenant)}`;
256265
}
257266
}

0 commit comments

Comments
 (0)