From 3f3af4e53e0936a815a8e00b403d3fa5fe0a5044 Mon Sep 17 00:00:00 2001 From: Dotan Simha Date: Thu, 29 Feb 2024 15:56:55 +0200 Subject: [PATCH] Reduce the amount of calls to check retention (#4113) --- .../providers/rate-limit.provider.ts | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/packages/services/api/src/modules/rate-limit/providers/rate-limit.provider.ts b/packages/services/api/src/modules/rate-limit/providers/rate-limit.provider.ts index b6a17aa04..825819a87 100644 --- a/packages/services/api/src/modules/rate-limit/providers/rate-limit.provider.ts +++ b/packages/services/api/src/modules/rate-limit/providers/rate-limit.provider.ts @@ -1,4 +1,5 @@ import { Inject, Injectable, Scope } from 'graphql-modules'; +import LRU from 'lru-cache'; import type { RateLimitApi, RateLimitApiInput } from '@hive/rate-limit'; import { createTRPCProxyClient, httpLink } from '@trpc/client'; import { sentry } from '../../../shared/sentry'; @@ -6,6 +7,8 @@ import { Logger } from '../../shared/providers/logger'; import type { RateLimitServiceConfig } from './tokens'; import { RATE_LIMIT_SERVICE_CONFIG } from './tokens'; +const RETENTION_CACHE_TTL_IN_SECONDS = 120; + @Injectable({ global: true, scope: Scope.Singleton, @@ -13,6 +16,11 @@ import { RATE_LIMIT_SERVICE_CONFIG } from './tokens'; export class RateLimitProvider { private logger: Logger; private rateLimit; + private retentionCache = new LRU({ + max: 500, + ttl: RETENTION_CACHE_TTL_IN_SECONDS * 1000, + stale: false, + }); constructor( logger: Logger, @@ -57,8 +65,15 @@ export class RateLimitProvider { return null; } - this.logger.debug(`Getting retention for target id="${input.targetId}"`); + if (this.retentionCache.has(input.targetId)) { + return this.retentionCache.get(input.targetId); + } - return await this.rateLimit.getRetention.query(input); + this.logger.debug(`Fetching retention for target id="${input.targetId}"`); + + const value = await this.rateLimit.getRetention.query(input); + this.retentionCache.set(input.targetId, value); + + return value; } }