File tree Expand file tree Collapse file tree
extensions/discord/src/internal Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import { parseFiniteNumber } from "openclaw/plugin-sdk/number-runtime" ;
2+
13const 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+
310class 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 ;
Original file line number Diff line number Diff 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 ) => { } ) ;
You can’t perform that action at this time.
0 commit comments