🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
fix(callouts): ratelimits should fail open (#14329)
Nimareab19e4dc5d7
diff --git a/web/src/__tests__/server/unit/web-callout-rate-limit.servertest.ts b/web/src/__tests__/server/unit/web-callout-rate-limit.servertest.tsindex f6ece54512..85007ce364 100644--- a/web/src/__tests__/server/unit/web-callout-rate-limit.servertest.ts+++ b/web/src/__tests__/server/unit/web-callout-rate-limit.servertest.ts@@ -59,11 +59,6 @@ describe("web callout rate limiting", () => {}) as any;const pending = () => new Promise<never>(() => undefined);- const expectUnavailable = (promise: Promise<unknown>) =>- expect(promise).rejects.toMatchObject({- code: "TOO_MANY_REQUESTS",- message: "Web callout invocation is temporarily unavailable.",- });beforeEach(() => {(env as any).LANGFUSE_RATE_LIMITS_ENABLED = "true";@@ -80,7 +75,7 @@ describe("web callout rate limiting", () => {resetWebCalloutInFlightLimitsForTests();});- it("uses scoped 10/60 Redis buckets, shares cold connects, and fails closed", async () => {+ it("uses scoped 10/60 Redis buckets, shares cold connects, and fails open when Redis is unavailable", async () => {const client = redis();await WebCalloutRateLimitService.getInstance(client).consume(context);@@ -139,17 +134,33 @@ describe("web callout rate limiting", () => {(env as any).LANGFUSE_RATE_LIMITS_ENABLED = "true";WebCalloutRateLimitService.shutdown();- await expectUnavailable(+ await expect(WebCalloutRateLimitService.getInstance(null).consume(context),- );+ ).resolves.toBeUndefined();WebCalloutRateLimitService.shutdown();+ mocks.consume.mockClear();mocks.consume.mockRejectedValueOnce(new Error("redis down"));- await expectUnavailable(+ await expect(WebCalloutRateLimitService.getInstance(redis()).consume(context),- );+ ).resolves.toBeUndefined();+ expect(mocks.consume).toHaveBeenCalledTimes(1);WebCalloutRateLimitService.shutdown();+ mocks.consume.mockReset();+ mocks.consume+ .mockResolvedValueOnce({})+ .mockRejectedValueOnce(new Error("redis down"));+ await expect(+ WebCalloutRateLimitService.getInstance(redis()).consume(context),+ ).resolves.toBeUndefined();+ expect(mocks.consume.mock.calls).toEqual([+ ["org-1:project-1:endpoint-1:user-1"],+ ["org-1:project-1:endpoint-1"],+ ]);++ WebCalloutRateLimitService.shutdown();+ mocks.consume.mockReset();mocks.consume.mockRejectedValueOnce(new (RateLimiterRes as any)(2300));await expect(WebCalloutRateLimitService.getInstance(redis()).consume(context),diff --git a/web/src/features/web-callouts/server/rateLimit.ts b/web/src/features/web-callouts/server/rateLimit.tsindex 82ff2f5367..6739433936 100644--- a/web/src/features/web-callouts/server/rateLimit.ts+++ b/web/src/features/web-callouts/server/rateLimit.ts@@ -116,17 +116,26 @@ export class WebCalloutRateLimitService {const redis = WebCalloutRateLimitService.redis;if (!redis) {- this.failClosed(context, "Redis is not configured for web callouts");+ this.allowBecauseRateLimitUnavailable(+ context,+ "Redis is not configured for web callouts",+ );+ return;}if (Date.now() < this.redisUnavailableUntilMs) {- this.failClosed(context, "Redis is temporarily unavailable");+ this.allowBecauseRateLimitUnavailable(+ context,+ "Redis is temporarily unavailable",+ );+ return;}try {await this.ensureRedisReady(redis);} catch (error) {this.markRedisUnavailable(context, error);+ return;}const keys = limitKey(context);@@ -145,7 +154,15 @@ export class WebCalloutRateLimitService {rejectIfRedisNotReady: true,});- await this.consumeLimiter(userLimiter, keys.user, context);+ const userLimitAvailable = await this.consumeLimiter(+ userLimiter,+ keys.user,+ context,+ );+ if (!userLimitAvailable) {+ return;+ }+await this.consumeLimiter(endpointLimiter, keys.endpoint, context);}@@ -165,9 +182,10 @@ export class WebCalloutRateLimitService {limiter: RateLimiterRedis,key: string,context: WebCalloutLimitContext,- ) {+ ): Promise<boolean> {try {await limiter.consume(key);+ return true;} catch (error) {if (error instanceof RateLimiterRes) {recordWebCalloutInvokeMetric("rate_limited", context);@@ -178,13 +196,14 @@ export class WebCalloutRateLimitService {}this.markRedisUnavailable(context, error);+ return false;}}private markRedisUnavailable(context: WebCalloutLimitContext,error: unknown,- ): never {+ ) {this.redisUnavailableUntilMs = Date.now() + REDIS_RETRY_COOLDOWN_MS;logger.warn("Web callout rate limiter unavailable", {orgId: context.orgId,@@ -192,18 +211,23 @@ export class WebCalloutRateLimitService {endpointId: context.endpointId,error: error instanceof Error ? error.message : "Unknown error",});- this.failClosed(context, "Redis is temporarily unavailable");+ this.allowBecauseRateLimitUnavailable(+ context,+ "Redis is temporarily unavailable",+ );}- private failClosed(context: WebCalloutLimitContext, reason: string): never {+ private allowBecauseRateLimitUnavailable(+ context: WebCalloutLimitContext,+ reason: string,+ ) {recordWebCalloutInvokeMetric("rate_limit_unavailable", context);- logger.warn("Web callout invocation blocked because rate limiting failed", {+ logger.warn("Web callout invocation allowed because rate limiting failed", {orgId: context.orgId,projectId: context.projectId,endpointId: context.endpointId,reason,});- throw tooManyRequests("Web callout invocation is temporarily unavailable.");}}