diff --git a/biome.json b/biome.json index 1cf4830..afb2511 100644 --- a/biome.json +++ b/biome.json @@ -11,7 +11,9 @@ "**", "!worker-configuration.d.ts", "!src/gql/graphql-env.d.ts", - "!src/gql/schema.gql" + "!src/gql/schema.gql", + "!src/cloudflare/gql/graphql-env.d.ts", + "!src/cloudflare/gql/schema.gql" ] }, "formatter": { diff --git a/src/cloudflare/client.ts b/src/cloudflare/client.ts index f688336..7eb0249 100644 --- a/src/cloudflare/client.ts +++ b/src/cloudflare/client.ts @@ -2379,23 +2379,12 @@ export class CloudflareMetricsClient { // Singleton factory // ============================================================================= -let cachedClient: CloudflareMetricsClient | null = null; -let cachedEnvHash: string | null = null; +const clientCache = new WeakMap(); type RateLimiter = { limit: (opts: { key: string }) => Promise<{ success: boolean }>; }; -/** - * Generates hash from environment for client caching. - * - * @param env Environment variables. - * @returns Hash string (API token). - */ -function envHash(env: Env): string { - return env.CLOUDFLARE_API_TOKEN; -} - const MAX_RETRIES = 3; const BASE_DELAY_MS = 250; @@ -2433,16 +2422,16 @@ function createRateLimitedFetch( } /** - * Gets or creates singleton CloudflareMetricsClient with rate limiting and env-based caching. + * Gets or creates singleton CloudflareMetricsClient with rate limiting. + * Uses WeakMap keyed on rate limiter for automatic GC when env is released. * * @param env Environment variables. * @returns CloudflareMetricsClient singleton instance. */ export function getCloudflareMetricsClient(env: Env): CloudflareMetricsClient { - const currentHash = envHash(env); - - if (cachedClient && cachedEnvHash === currentHash) { - return cachedClient; + const existing = clientCache.get(env.CF_API_RATE_LIMITER); + if (existing) { + return existing; } const loggerConfig = configFromEnv(env); @@ -2459,7 +2448,7 @@ export function getCloudflareMetricsClient(env: Env): CloudflareMetricsClient { logger, ); - cachedClient = new CloudflareMetricsClient({ + const client = new CloudflareMetricsClient({ apiToken: env.CLOUDFLARE_API_TOKEN, scrapeDelaySeconds: env.SCRAPE_DELAY_SECONDS, timeWindowSeconds: env.TIME_WINDOW_SECONDS, @@ -2468,6 +2457,6 @@ export function getCloudflareMetricsClient(env: Env): CloudflareMetricsClient { fetch: rateLimitedFetch, }); - cachedEnvHash = currentHash; - return cachedClient; + clientCache.set(env.CF_API_RATE_LIMITER, client); + return client; }