The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
perf(server): rate-limit the active event stream count scan (#21951)
Charles Bochet4dd9253d0105
diff --git a/packages/twenty-server/src/engine/subscriptions/event-stream.service.ts b/packages/twenty-server/src/engine/subscriptions/event-stream.service.tsindex f3292c64815..5dbe44d9f0b 100644--- a/packages/twenty-server/src/engine/subscriptions/event-stream.service.ts+++ b/packages/twenty-server/src/engine/subscriptions/event-stream.service.ts@@ -19,9 +19,13 @@ import {type RecordOrMetadataGqlOperationSignature,} from 'src/engine/subscriptions/types/event-stream-data.type';+const ACTIVE_STREAM_COUNT_REFRESH_MS = 5 * 60 * 1_000;+@Injectable()export class EventStreamService implements OnModuleInit {private readonly logger = new Logger(EventStreamService.name);+ private activeStreamCount = 0;+ private activeStreamCountRefreshedAt = 0;constructor(@InjectCacheStorage(CacheStorageNamespace.EngineSubscriptions)@@ -42,9 +46,19 @@ export class EventStreamService implements OnModuleInit {}async getTotalActiveStreamCount(): Promise<number> {- return this.cacheStorageService.scanAndCountSetMembers(- 'workspace:*:activeStreams',- );+ const now = Date.now();+ const isStale =+ now - this.activeStreamCountRefreshedAt >= ACTIVE_STREAM_COUNT_REFRESH_MS;++ if (isStale) {+ this.activeStreamCount =+ await this.cacheStorageService.scanAndCountSetMembers(+ 'workspace:*:activeStreams',+ );+ this.activeStreamCountRefreshedAt = now;+ }++ return this.activeStreamCount;}async createEventStream({