Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: abstract identify throttling and correct max_concurrency ha…
…ndling (#9375) * refactor: properly support max_concurrency ratelimit keys * fix: properly block for same key * chore: export session state * chore: throttler no longer requires manager * refactor: abstract throttlers * chore: proper member order * chore: remove leftover debug log * chore: use @link tag in doc comment Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com> * chore: suggested changes * fix(WebSocketShard): cancel identify if the shard closed in the meantime * refactor(throttlers): support abort signals * fix: memory leak * chore: remove leftover --------- Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com> Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
- Loading branch information
1 parent
cac3c07
commit 02dfaf1
Showing
16 changed files
with
279 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
32 changes: 32 additions & 0 deletions
32
packages/ws/__tests__/util/SimpleIdentifyThrottler.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { setTimeout as sleep } from 'node:timers/promises'; | ||
import { expect, test, vi, type Mock } from 'vitest'; | ||
import { SimpleIdentifyThrottler } from '../../src/index.js'; | ||
|
||
vi.mock('node:timers/promises', () => ({ | ||
setTimeout: vi.fn(), | ||
})); | ||
|
||
const throttler = new SimpleIdentifyThrottler(2); | ||
|
||
vi.useFakeTimers(); | ||
|
||
const NOW = vi.fn().mockReturnValue(Date.now()); | ||
global.Date.now = NOW; | ||
|
||
test('basic case', async () => { | ||
// Those shouldn't wait since they're in different keys | ||
|
||
await throttler.waitForIdentify(0); | ||
expect(sleep).not.toHaveBeenCalled(); | ||
|
||
await throttler.waitForIdentify(1); | ||
expect(sleep).not.toHaveBeenCalled(); | ||
|
||
// Those should wait | ||
|
||
await throttler.waitForIdentify(2); | ||
expect(sleep).toHaveBeenCalledTimes(1); | ||
|
||
await throttler.waitForIdentify(3); | ||
expect(sleep).toHaveBeenCalledTimes(2); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.