The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
fix(server): bound TimelineActivityRoutingPlanService workspace cache (#25659)
Etiennebd601f0c2a5b
diff --git a/packages/twenty-server/src/modules/timeline/services/timeline-activity-routing-plan.service.ts b/packages/twenty-server/src/modules/timeline/services/timeline-activity-routing-plan.service.tsindex 61a0cff95a8..343be80580a 100644--- a/packages/twenty-server/src/modules/timeline/services/timeline-activity-routing-plan.service.ts+++ b/packages/twenty-server/src/modules/timeline/services/timeline-activity-routing-plan.service.ts@@ -14,6 +14,7 @@ import { buildJunctionTargetShape } from 'src/modules/timeline/utils/build-junctimport { buildNonAuditLoggedFieldNamesByObjectMetadataId } from 'src/modules/timeline/utils/build-non-audit-logged-field-names-by-object-metadata-id.util';import { buildTimelineActivitySelfRule } from 'src/modules/timeline/utils/build-timeline-activity-self-rule.util';import { resolveTimelineActivityTypeRouting } from 'src/modules/timeline/utils/resolve-timeline-activity-type-routing.util';+import { readLruEntry, writeLruEntry } from 'src/utils/lru-map.util';import {buildTimelineActivityTypeResolution,toResolvedTimelineActivityType,@@ -40,6 +41,10 @@ type TimelineActivityRoutingPlan = {resolveTimelineActivityType: TimelineActivityTypeResolver;};+// A routing plan holds that workspace's whole flat field metadata maps, so an+// unbounded map grows with the number of workspaces a worker ever routes for.+const MAX_CACHED_WORKSPACES = 128;+@Injectable()export class TimelineActivityRoutingPlanService {private readonly routingPlanByWorkspaceId = new Map<@@ -55,7 +60,7 @@ export class TimelineActivityRoutingPlanService {flatObjectMetadata,workspaceId,}: {- flatObjectMetadata: FlatObjectMetadata;+ flatObjectMetadata: Pick<FlatObjectMetadata, 'id' | 'isAuditLogged'>;workspaceId: string;}): Promise<boolean> {if (flatObjectMetadata.isAuditLogged) {@@ -122,7 +127,10 @@ export class TimelineActivityRoutingPlanService {hashes.flatFieldMetadataMapsOrm,hashes.flatTimelineActivityTypeMaps,].join('|');- const cachedRoutingPlan = this.routingPlanByWorkspaceId.get(workspaceId);+ const cachedRoutingPlan = readLruEntry({+ map: this.routingPlanByWorkspaceId,+ key: workspaceId,+ });if (cachedRoutingPlan?.cacheKey === cacheKey) {return cachedRoutingPlan.routingPlan;@@ -130,9 +138,11 @@ export class TimelineActivityRoutingPlanService {const routingPlan = this.buildRoutingPlan(data);- this.routingPlanByWorkspaceId.set(workspaceId, {- cacheKey,- routingPlan,+ writeLruEntry({+ map: this.routingPlanByWorkspaceId,+ key: workspaceId,+ value: { cacheKey, routingPlan },+ maxEntries: MAX_CACHED_WORKSPACES,});return routingPlan;diff --git a/packages/twenty-server/src/utils/__tests__/lru-map.util.spec.ts b/packages/twenty-server/src/utils/__tests__/lru-map.util.spec.tsnew file mode 100644index 00000000000..fd824752291--- /dev/null+++ b/packages/twenty-server/src/utils/__tests__/lru-map.util.spec.ts@@ -0,0 +1,54 @@+import { readLruEntry, writeLruEntry } from 'src/utils/lru-map.util';++describe('readLruEntry', () => {+ it('returns undefined for a missing key', () => {+ expect(+ readLruEntry({ map: new Map<string, number>(), key: 'a' }),+ ).toBeUndefined();+ });++ it('moves the read key to the most recently used position', () => {+ const map = new Map([+ ['a', 1],+ ['b', 2],+ ['c', 3],+ ]);++ readLruEntry({ map, key: 'a' });++ expect([...map.keys()]).toEqual(['b', 'c', 'a']);+ });+});++describe('writeLruEntry', () => {+ it('keeps the map within maxEntries', () => {+ const map = new Map<string, number>();++ for (let index = 0; index < 10; index += 1) {+ writeLruEntry({ map, key: `k-${index}`, value: index, maxEntries: 3 });+ }++ expect(map.size).toBe(3);+ expect([...map.keys()]).toEqual(['k-7', 'k-8', 'k-9']);+ });++ it('evicts the least recently used entry, not the oldest inserted', () => {+ const map = new Map<string, number>();++ writeLruEntry({ map, key: 'a', value: 1, maxEntries: 2 });+ writeLruEntry({ map, key: 'b', value: 2, maxEntries: 2 });+ readLruEntry({ map, key: 'a' });+ writeLruEntry({ map, key: 'c', value: 3, maxEntries: 2 });++ expect([...map.keys()]).toEqual(['a', 'c']);+ });++ it('overwrites an existing key without growing the map', () => {+ const map = new Map([['a', 1]]);++ writeLruEntry({ map, key: 'a', value: 2, maxEntries: 5 });++ expect(map.size).toBe(1);+ expect(map.get('a')).toBe(2);+ });+});diff --git a/packages/twenty-server/src/utils/lru-map.util.ts b/packages/twenty-server/src/utils/lru-map.util.tsnew file mode 100644index 00000000000..2827e6260f0--- /dev/null+++ b/packages/twenty-server/src/utils/lru-map.util.ts@@ -0,0 +1,43 @@+export const readLruEntry = <Key, Value>({+ map,+ key,+}: {+ map: Map<Key, Value>;+ key: Key;+}): Value | undefined => {+ const value = map.get(key);++ if (value === undefined) {+ return undefined;+ }++ map.delete(key);+ map.set(key, value);++ return value;+};++export const writeLruEntry = <Key, Value>({+ map,+ key,+ value,+ maxEntries,+}: {+ map: Map<Key, Value>;+ key: Key;+ value: Value;+ maxEntries: number;+}): void => {+ map.delete(key);+ map.set(key, value);++ while (map.size > maxEntries) {+ const leastRecentlyUsed = map.keys().next();++ if (leastRecentlyUsed.done) {+ return;+ }++ map.delete(leastRecentlyUsed.value);+ }+};