fix: Update throttle for /meta endpoints, will call every 2s for small account, 10s for large accounts (#11190)
This update improves the throttling mechanism for conversation meta requests to optimize server load and enhance performance. The changes implement differentiated thresholds based on account size - a 2-second throttle for small accounts (≤100 conversations) and a 10-second throttle for large accounts (>100 conversations). Fixes #11178
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
class ConversationMetaThrottleManager {
|
||||
constructor() {
|
||||
this.lastUpdatedTime = null;
|
||||
}
|
||||
|
||||
shouldThrottle(threshold = 10000) {
|
||||
if (!this.lastUpdatedTime) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const currentTime = new Date().getTime();
|
||||
const lastUpdatedTime = new Date(this.lastUpdatedTime).getTime();
|
||||
|
||||
if (currentTime - lastUpdatedTime < threshold) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
markUpdate() {
|
||||
this.lastUpdatedTime = new Date();
|
||||
}
|
||||
|
||||
reset() {
|
||||
this.lastUpdatedTime = null;
|
||||
}
|
||||
}
|
||||
|
||||
export default new ConversationMetaThrottleManager();
|
||||
@@ -0,0 +1,34 @@
|
||||
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