Skip to content

Commit e7fb8ca

Browse files
committed
fix(discord): default non-finite identify concurrency
1 parent 6f9d5e1 commit e7fb8ca

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

extensions/discord/src/internal/gateway-identify-limiter.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1+
import { parseFiniteNumber } from "openclaw/plugin-sdk/number-runtime";
2+
13
const IDENTIFY_WINDOW_MS = 5_000;
24

5+
function normalizeMaxConcurrency(value: number | undefined): number {
6+
const parsed = parseFiniteNumber(value);
7+
return parsed === undefined ? 1 : Math.max(1, Math.floor(parsed));
8+
}
9+
310
class GatewayIdentifyLimiter {
411
private nextAllowedAtByKey = new Map<number, number>();
512

613
async wait(params: { shardId?: number; maxConcurrency?: number }): Promise<void> {
7-
const maxConcurrency = Math.max(1, Math.floor(params.maxConcurrency ?? 1));
14+
const maxConcurrency = normalizeMaxConcurrency(params.maxConcurrency);
815
const rateKey = (params.shardId ?? 0) % maxConcurrency;
916
const now = Date.now();
1017
const nextAllowedAt = this.nextAllowedAtByKey.get(rateKey) ?? now;

extensions/discord/src/internal/gateway.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,29 @@ describe("GatewayPlugin", () => {
234234
);
235235
});
236236

237+
it("uses the safe single identify bucket for non-finite max concurrency", async () => {
238+
vi.useFakeTimers();
239+
vi.setSystemTime(0);
240+
241+
await sharedGatewayIdentifyLimiter.wait({
242+
shardId: 0,
243+
maxConcurrency: Number.POSITIVE_INFINITY,
244+
});
245+
let secondResolved = false;
246+
const second = sharedGatewayIdentifyLimiter
247+
.wait({ shardId: 1, maxConcurrency: Number.POSITIVE_INFINITY })
248+
.then(() => {
249+
secondResolved = true;
250+
});
251+
252+
await vi.advanceTimersByTimeAsync(4_999);
253+
expect(secondResolved).toBe(false);
254+
255+
await vi.advanceTimersByTimeAsync(1);
256+
await second;
257+
expect(secondResolved).toBe(true);
258+
});
259+
237260
it("preserves MESSAGE_CREATE author payloads for inbound dispatch", async () => {
238261
const gateway = new GatewayPlugin({ autoInteractions: false });
239262
const dispatchGatewayEvent = vi.fn(async (eventValue: string, dataValue: unknown) => {});

0 commit comments

Comments
 (0)