Reduce the amount of calls to check retention (#4113)

This commit is contained in:
Dotan Simha 2024-02-29 15:56:55 +02:00 committed by GitHub
parent d249a0aeae
commit 3f3af4e53e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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<string, number>({
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;
}
}