feat: Add debounce for meta query (#11195)
This PR combines the approaches in https://github.com/chatwoot/chatwoot/pull/11190 and https://github.com/chatwoot/chatwoot/pull/11187 to debounce the meta request with a max wait time of 2.5 seconds With 500 concurrent users, the theoretical limit with this is 720K requests per minute, if all of them continuously receive websocket events. The max wait of 2.5 seconds is still very generous, and we can easily make it 2 seconds for smaller accounts and 5 seconds for larger accounts. ```js const debouncedFetchMetaData = debounce(fetchMetaData, 500, false, 200); const longDebouncedFetchMetaData = debounce(fetchMetaData, 500, false, 5000); export const actions = { get: async ({ commit, state: $state }, params) => { if ($state.allCount > 100) { longDebouncedFetchMetaData(commit, params); } else { debouncedFetchMetaData(commit, params); } }, set({ commit }, meta) { commit(types.SET_CONV_TAB_META, meta); }, }; ``` Related Utils PR: https://github.com/chatwoot/utils/pull/49 Here's the debounce in action <img width="934" alt="image" src="https://github.com/user-attachments/assets/5265a108-9c64-4488-9b4c-2e0d06aadc50" /> --------- Co-authored-by: Pranav <pranavrajs@gmail.com>
This commit is contained in:
@@ -1,34 +0,0 @@
|
||||
import ConversationMetaThrottleManager from '../ConversationMetaThrottleManager';
|
||||
|
||||
describe('ConversationMetaThrottleManager', () => {
|
||||
beforeEach(() => {
|
||||
// Reset the lastUpdatedTime before each test
|
||||
ConversationMetaThrottleManager.lastUpdatedTime = null;
|
||||
});
|
||||
|
||||
describe('shouldThrottle', () => {
|
||||
it('returns false when lastUpdatedTime is not set', () => {
|
||||
expect(ConversationMetaThrottleManager.shouldThrottle()).toBe(false);
|
||||
});
|
||||
|
||||
it('returns true when time difference is less than threshold', () => {
|
||||
ConversationMetaThrottleManager.markUpdate();
|
||||
expect(ConversationMetaThrottleManager.shouldThrottle()).toBe(true);
|
||||
});
|
||||
|
||||
it('returns false when time difference is more than threshold', () => {
|
||||
ConversationMetaThrottleManager.lastUpdatedTime = new Date(
|
||||
Date.now() - 11000
|
||||
);
|
||||
expect(ConversationMetaThrottleManager.shouldThrottle()).toBe(false);
|
||||
});
|
||||
|
||||
it('respects custom threshold value', () => {
|
||||
ConversationMetaThrottleManager.lastUpdatedTime = new Date(
|
||||
Date.now() - 5000
|
||||
);
|
||||
expect(ConversationMetaThrottleManager.shouldThrottle(3000)).toBe(false);
|
||||
expect(ConversationMetaThrottleManager.shouldThrottle(6000)).toBe(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user