-
Notifications
You must be signed in to change notification settings - Fork 13k
fix(presence): batch presence updates to prevent broadcast storm #21182 #38238
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
Open
shash-hq
wants to merge
5
commits into
RocketChat:develop
Choose a base branch
from
shash-hq:fix/presence-broadcast-storm
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f3d1ac6
fix(presence): batch presence updates to prevent broadcast storm #21182
shash-hq a2d5c4b
Merge branch 'develop' into fix/presence-broadcast-storm
shash-hq 8392a4d
chore: add changeset for presence fix
shash-hq db69ed4
Update ee/packages/presence/src/Presence.ts
shash-hq 71a77ff
Address PR feedback and improve presence batching resilience
shash-hq 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
Some comments aren't visible on the classic Files Changed page.
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,6 @@ | ||
| --- | ||
| "@rocket.chat/core-services": patch | ||
| "@rocket.chat/meteor": patch | ||
| --- | ||
|
|
||
| Fix: Implement batching for presence status updates to prevent broadcast storms during mass user reconnections. |
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
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,112 @@ | ||
| import { Presence } from './Presence'; | ||
|
|
||
| jest.mock('@rocket.chat/core-services', () => ({ | ||
| ServiceClass: class { | ||
| api = { | ||
| broadcast: jest.fn(), | ||
| }; | ||
| onEvent() { } | ||
| }, | ||
| License: { | ||
| hasModule: jest.fn().mockResolvedValue(true), | ||
| }, | ||
| })); | ||
|
|
||
| jest.mock('@rocket.chat/models', () => ({ | ||
| Settings: { updateValueById: jest.fn() }, | ||
| Users: {}, | ||
| UsersSessions: {}, | ||
| registerServiceModels: jest.fn(), | ||
| })); | ||
|
|
||
| jest.mock('./lib/PresenceReaper', () => ({ | ||
| PresenceReaper: class { | ||
| start() { } | ||
| stop() { } | ||
| }, | ||
| })); | ||
|
|
||
| describe('Presence Batching', () => { | ||
| let presence: Presence; | ||
|
|
||
| beforeEach(() => { | ||
| jest.useFakeTimers(); | ||
| presence = new Presence(); | ||
| (presence as any).broadcastEnabled = true; | ||
| (presence as any).hasLicense = true; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| jest.useRealTimers(); | ||
| }); | ||
|
|
||
| it('should buffer broadcast events', () => { | ||
| const user = { _id: 'u1', username: 'user1', status: 'online' } as any; | ||
| (presence as any).broadcast(user, 'offline'); | ||
|
|
||
| // Check buffer | ||
| expect((presence as any).presenceBatch.size).toBe(1); | ||
|
|
||
| // Assert API not called yet | ||
| expect((presence as any).api.broadcast).not.toHaveBeenCalled(); | ||
|
|
||
| // Advance timers | ||
| jest.advanceTimersByTime(500); | ||
|
|
||
| // Assert API called | ||
| expect((presence as any).api.broadcast).toHaveBeenCalledWith('presence.status.batch', expect.any(Array)); | ||
| expect((presence as any).api.broadcast).toHaveBeenCalledTimes(1); | ||
|
|
||
| const batch = (presence as any).api.broadcast.mock.calls[0][1]; | ||
| expect(batch).toHaveLength(1); | ||
| expect(batch[0].user._id).toBe('u1'); | ||
| }); | ||
|
|
||
| it('should batch multiple updates', () => { | ||
| const u1 = { _id: 'u1', username: 'user1', status: 'online' } as any; | ||
| const u2 = { _id: 'u2', username: 'user2', status: 'busy' } as any; | ||
|
|
||
| (presence as any).broadcast(u1, 'offline'); | ||
| (presence as any).broadcast(u2, 'online'); | ||
|
|
||
| expect((presence as any).presenceBatch.size).toBe(2); | ||
|
|
||
| jest.advanceTimersByTime(500); | ||
|
|
||
| expect((presence as any).api.broadcast).toHaveBeenCalledWith('presence.status.batch', expect.any(Array)); | ||
| const batch = (presence as any).api.broadcast.mock.calls[0][1]; | ||
| expect(batch).toHaveLength(2); | ||
| const ids = batch.map((item: any) => item.user._id).sort(); | ||
| expect(ids).toEqual(['u1', 'u2']); | ||
| }); | ||
|
|
||
| it('should debounce updates for same user', () => { | ||
| const u1_v1 = { _id: 'u1', username: 'user1', status: 'online' } as any; | ||
| const u1_v2 = { _id: 'u1', username: 'user1', status: 'offline' } as any; | ||
|
|
||
| (presence as any).broadcast(u1_v1, 'busy'); | ||
| (presence as any).broadcast(u1_v2, 'online'); | ||
|
|
||
| // Should update the existing entry in map | ||
| expect((presence as any).presenceBatch.size).toBe(1); | ||
| const stored = (presence as any).presenceBatch.get('u1'); | ||
| expect(stored.user.status).toBe('offline'); | ||
|
|
||
| jest.advanceTimersByTime(500); | ||
| expect((presence as any).api.broadcast).toHaveBeenCalledTimes(1); | ||
| const batch = (presence as any).api.broadcast.mock.calls[0][1]; | ||
| expect(batch).toHaveLength(1); | ||
| expect(batch[0].user.status).toBe('offline'); | ||
| }); | ||
|
|
||
| it('should not broadcast if broadcastEnabled is false', () => { | ||
| (presence as any).broadcastEnabled = false; | ||
| const user = { _id: 'u1', username: 'user1', status: 'online' } as any; | ||
|
|
||
| (presence as any).broadcast(user, 'offline'); | ||
|
|
||
| expect((presence as any).presenceBatch.size).toBe(0); | ||
| jest.advanceTimersByTime(500); | ||
| expect((presence as any).api.broadcast).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
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
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.