An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
fix(analytics): deliver rate-limit events via after() (#921)
Yoshiki Miura99c90de51add
diff --git a/lib/rate-limit/__tests__/adaptive-limit.test.ts b/lib/rate-limit/__tests__/adaptive-limit.test.tsindex e7a96f86..d79dc120 100644--- a/lib/rate-limit/__tests__/adaptive-limit.test.ts+++ b/lib/rate-limit/__tests__/adaptive-limit.test.ts@@ -5,6 +5,14 @@ import { checkAndEnforceAdaptiveLimit } from '@/lib/rate-limit/adaptive-limit'const mockRedisIncr = vi.fn()const mockRedisExpire = vi.fn()const mockTrack = vi.fn()+const mockAfter = vi.fn()++// `after` throws outside a request scope, so stand in for the runtime and+// run the task inline. Keeping it a spy also lets us assert that delivery+// is handed to it rather than left floating.+vi.mock('next/server', () => ({+ after: (task: Promise<unknown>) => mockAfter(task)+}))vi.mock('@upstash/redis', () => ({Redis: vi.fn().mockImplementation(function () {@@ -25,6 +33,7 @@ describe('checkAndEnforceAdaptiveLimit', () => {mockRedisExpire.mockReset()mockTrack.mockReset()mockTrack.mockResolvedValue(undefined)+ mockAfter.mockReset()process.env.MORPHIC_CLOUD_DEPLOYMENT = 'true'process.env.UPSTASH_REDIS_REST_URL = 'https://example.com'process.env.UPSTASH_REDIS_REST_TOKEN = 'token'@@ -138,6 +147,17 @@ describe('checkAndEnforceAdaptiveLimit', () => {})})+ it('hands blocked-event delivery to after so the 429 cannot cut it short', async () => {+ mockRedisIncr.mockResolvedValue(31)+ mockRedisExpire.mockResolvedValue(1)++ const response = await checkAndEnforceAdaptiveLimit('user-9')++ expect(response?.status).toBe(429)+ expect(mockAfter).toHaveBeenCalledTimes(1)+ expect(mockAfter.mock.calls[0][0]).toBeInstanceOf(Promise)+ })+it('does not emit analytics when redis is unavailable (no enforcement)', async () => {mockRedisIncr.mockRejectedValue(new Error('boom'))diff --git a/lib/rate-limit/adaptive-limit.ts b/lib/rate-limit/adaptive-limit.tsindex 1bd00c4a..f492952e 100644--- a/lib/rate-limit/adaptive-limit.ts+++ b/lib/rate-limit/adaptive-limit.ts@@ -1,3 +1,5 @@+import { after } from 'next/server'+import { Redis } from '@upstash/redis'import { trackAdaptiveLimitEvent } from '@/lib/analytics'@@ -122,13 +124,21 @@ export async function checkAndEnforceAdaptiveLimit(// Only emit analytics for real (Redis-backed) checks. Local dev / cloud// without Upstash returns enforced=false and we skip tracking to avoid// polluting the dashboard with no-op events.+ //+ // Hand the capture to `after` rather than letting it float: the blocked+ // path returns a 429 immediately, so an un-awaited PostHog flush can be+ // cut short when the serverless runtime freezes. Allowed requests go on+ // to a long-lived stream and would survive, which would bias the block+ // rate downward exactly where it matters.if (result.enforced) {- void trackAdaptiveLimitEvent({- outcome: result.allowed ? 'allowed' : 'blocked',- userId,- used: result.used,- limit: result.limit- })+ after(+ trackAdaptiveLimitEvent({+ outcome: result.allowed ? 'allowed' : 'blocked',+ userId,+ used: result.used,+ limit: result.limit+ })+ )}if (!result.allowed) {diff --git a/lib/rate-limit/chat-limits.ts b/lib/rate-limit/chat-limits.tsindex 7ab1dbf5..05e43706 100644--- a/lib/rate-limit/chat-limits.ts+++ b/lib/rate-limit/chat-limits.ts@@ -1,3 +1,5 @@+import { after } from 'next/server'+import { Redis } from '@upstash/redis'import { trackChatLimitEvent } from '@/lib/analytics'@@ -130,13 +132,21 @@ export async function checkAndEnforceOverallChatLimit(// Only emit analytics for real (Redis-backed) checks. Local dev / cloud// without Upstash returns enforced=false and we skip tracking to avoid// polluting the dashboard with no-op events.+ //+ // Hand the capture to `after` rather than letting it float: the blocked+ // path returns a 429 immediately, so an un-awaited PostHog flush can be+ // cut short when the serverless runtime freezes. Allowed requests go on+ // to a long-lived stream and would survive, which would bias the block+ // rate downward exactly where it matters.if (result.enforced) {- void trackChatLimitEvent({- outcome: result.allowed ? 'allowed' : 'blocked',- userId,- used: result.used,- limit: result.limit- })+ after(+ trackChatLimitEvent({+ outcome: result.allowed ? 'allowed' : 'blocked',+ userId,+ used: result.used,+ limit: result.limit+ })+ )}if (!result.allowed) {