-
-
Notifications
You must be signed in to change notification settings - Fork 3k
feat(scaling): fan-out debounce (#7756 lever 3 prototype) #7766
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,43 @@ | ||
| // Per-pad fan-out scheduler — debounce wrapper for #7756 lever 3. | ||
| // | ||
| // When `settings.fanoutDebounceMs <= 0` (default), fan-out fires immediately | ||
| // — legacy behaviour. When > 0, rapid scheduleFanout(pad) calls coalesce | ||
| // into a single fan-out per pad per debounce window. | ||
| // | ||
| // Lives in its own module rather than inside PadMessageHandler so the | ||
| // scheduling logic can be unit-tested without pulling in the full pad / DB | ||
| // / socket.io stack. | ||
|
|
||
| import settings from '../utils/Settings'; | ||
|
|
||
| export type FanoutCallback = (padId: string) => Promise<void>; | ||
|
|
||
| const pendingFanouts = new Map<string, NodeJS.Timeout>(); | ||
| let onError: (padId: string, err: unknown) => void = (padId, err) => { | ||
| // Default error sink: re-throw on the next tick so it shows up in the log. | ||
| setImmediate(() => { throw err; }); | ||
| }; | ||
|
|
||
| /** Override the error handler. PadMessageHandler installs one that uses messageLogger. */ | ||
| export const setErrorHandler = (fn: (padId: string, err: unknown) => void): void => { | ||
| onError = fn; | ||
| }; | ||
|
|
||
| /** Schedule a fan-out for the given pad. */ | ||
| export const scheduleFanout = (padId: string, fanout: FanoutCallback): void => { | ||
| const debounceMs = settings.fanoutDebounceMs ?? 0; | ||
| if (debounceMs <= 0) { | ||
| void fanout(padId).catch((err) => onError(padId, err)); | ||
| return; | ||
| } | ||
| if (pendingFanouts.has(padId)) return; | ||
| const t = setTimeout(() => { | ||
| pendingFanouts.delete(padId); | ||
| void fanout(padId).catch((err) => onError(padId, err)); | ||
| }, debounceMs); | ||
| if (typeof (t as {unref?: () => void}).unref === 'function') (t as {unref: () => void}).unref(); | ||
| pendingFanouts.set(padId, t); | ||
| }; | ||
|
|
||
| /** Test helper. */ | ||
| export const _state = {pendingFanouts}; | ||
This file contains hidden or 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 hidden or 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 hidden or 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,78 @@ | ||
| // Unit coverage for the per-pad fan-out debounce (#7756 lever 3). | ||
| // With debounce > 0, rapid scheduleFanout calls coalesce into a single | ||
| // fanout invocation per pad per debounce window. | ||
|
|
||
| import {describe, it, expect, beforeEach, afterEach, vi} from 'vitest'; | ||
| import settings from '../../../node/utils/Settings'; | ||
| import {scheduleFanout, _state, setErrorHandler, type FanoutCallback} from '../../../node/handler/FanoutScheduler'; | ||
|
|
||
| describe('fanout debounce', () => { | ||
| const originalDebounce = settings.fanoutDebounceMs; | ||
| let calls: string[]; | ||
| let fanout: FanoutCallback; | ||
|
|
||
| beforeEach(() => { | ||
| vi.useFakeTimers(); | ||
| _state.pendingFanouts.clear(); | ||
| calls = []; | ||
| fanout = async (padId) => { calls.push(padId); }; | ||
| setErrorHandler(() => {/* swallow in tests */}); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.useRealTimers(); | ||
| settings.fanoutDebounceMs = originalDebounce; | ||
| }); | ||
|
|
||
| it('with debounce=0 fires the fanout synchronously per call', async () => { | ||
| settings.fanoutDebounceMs = 0; | ||
| scheduleFanout('pad-a', fanout); | ||
| scheduleFanout('pad-a', fanout); | ||
| scheduleFanout('pad-a', fanout); | ||
| await vi.runAllTimersAsync(); | ||
| expect(calls).toEqual(['pad-a', 'pad-a', 'pad-a']); | ||
| expect(_state.pendingFanouts.size).toBe(0); | ||
| }); | ||
|
|
||
| it('with debounce>0 coalesces N rapid calls into a single fanout call', async () => { | ||
| settings.fanoutDebounceMs = 50; | ||
| for (let i = 0; i < 10; i++) scheduleFanout('pad-a', fanout); | ||
| expect(calls).toEqual([]); | ||
| expect(_state.pendingFanouts.size).toBe(1); | ||
| await vi.advanceTimersByTimeAsync(60); | ||
| expect(calls).toEqual(['pad-a']); | ||
| expect(_state.pendingFanouts.size).toBe(0); | ||
| }); | ||
|
|
||
| it('debounces independently per pad', async () => { | ||
| settings.fanoutDebounceMs = 50; | ||
| scheduleFanout('pad-a', fanout); | ||
| scheduleFanout('pad-b', fanout); | ||
| scheduleFanout('pad-a', fanout); | ||
| expect(_state.pendingFanouts.size).toBe(2); | ||
| await vi.advanceTimersByTimeAsync(60); | ||
| expect(calls.sort()).toEqual(['pad-a', 'pad-b']); | ||
| }); | ||
|
|
||
| it('after the window fires, a new schedule starts a fresh window', async () => { | ||
| settings.fanoutDebounceMs = 50; | ||
| scheduleFanout('pad-a', fanout); | ||
| await vi.advanceTimersByTimeAsync(60); | ||
| expect(calls).toEqual(['pad-a']); | ||
| scheduleFanout('pad-a', fanout); | ||
| expect(_state.pendingFanouts.size).toBe(1); | ||
| await vi.advanceTimersByTimeAsync(60); | ||
| expect(calls).toEqual(['pad-a', 'pad-a']); | ||
| }); | ||
|
|
||
| it('routes fanout errors through setErrorHandler', async () => { | ||
| settings.fanoutDebounceMs = 0; | ||
| const errors: Array<{padId: string; err: unknown}> = []; | ||
| setErrorHandler((padId, err) => { errors.push({padId, err}); }); | ||
| scheduleFanout('pad-x', async () => { throw new Error('boom'); }); | ||
| await vi.runAllTimersAsync(); | ||
| expect(errors).toHaveLength(1); | ||
| expect(errors[0]!.padId).toBe('pad-x'); | ||
| expect((errors[0]!.err as Error).message).toBe('boom'); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.