mirror of
https://github.com/graphql-hive/console
synced 2026-05-23 09:08:34 +00:00
Reduce the amount of calls to check retention (#4113)
This commit is contained in:
parent
d249a0aeae
commit
3f3af4e53e
1 changed files with 17 additions and 2 deletions
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue