Skip to content
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

Change cursors batch time formula on presence update #333

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Cursors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ describe('Cursors', () => {
vi.spyOn(channel.presence, 'get').mockImplementation(createPresenceCount(2));
await cursors['onPresenceUpdate']();
expect(batching.shouldSend).toBeTruthy();
expect(batching.batchTime).toEqual(50);
expect(batching.batchTime).toEqual(25);
});

it<CursorsTestContext>('batchTime is updated when multiple people are present', async ({
Expand All @@ -126,7 +126,7 @@ describe('Cursors', () => {
}) => {
vi.spyOn(channel.presence, 'get').mockImplementation(createPresenceCount(2));
await cursors['onPresenceUpdate']();
expect(batching.batchTime).toEqual(50);
expect(batching.batchTime).toEqual(25);
});

describe('pushCursorPosition', () => {
Expand Down
6 changes: 5 additions & 1 deletion src/Cursors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,11 @@ export default class Cursors extends EventEmitter<CursorsEventMap> {
const channel = this.getChannel();
const cursorsMembers = await channel.presence.get();
this.cursorBatching.setShouldSend(cursorsMembers.length > 1);
this.cursorBatching.setBatchTime(cursorsMembers.length * this.options.outboundBatchInterval);
/**
* Since server-side batching is automically enabled for cursors channels, we can now adjust the client-side batching interval more granularly.
* E.g. multiply the configured outboundBatchInterval by groups of 100 members instead of the total number of members.
*/
this.cursorBatching.setBatchTime(Math.ceil(cursorsMembers.length / 100) * this.options.outboundBatchInterval);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's hard to understand why this is divided by 100 now?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before we were adjusting with cursorsMembers.length * this.options.outboundBatchInterval, which made the client-side latency climb linearly with the presence set. Since we have server-side batching now we don't need to make it so steep client-side. Therefore, the ticket suggestion was that it could climb with groups of 100s, which makes client-side batching latency grow much slower.

Example assuming outboundBatchInterval=25ms:

  • if #presenceMembers <= 100: batchTime=25ms
  • if #presenceMembers <= 200: batchTime=50ms
  • if #presenceMembers <= 300: batchTime=75ms

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok thanks, makes sense, perhaps put that as a comment in the code?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added as a comment in the code

}

private isUnsubscribed() {
Expand Down
Loading