-
Notifications
You must be signed in to change notification settings - Fork 3
double cache api keys #81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e4c351d
double cache api keys
samuelcolvin 41cd199
fix: project cache
ddanielcruzz 4d88b66
Merge branch 'main' of github.com:pydantic/pydantic-ai-gateway into d…
ddanielcruzz c098110
fix: fallback
ddanielcruzz 7ce75fc
refactor: narrow delete api key types
ddanielcruzz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| /** biome-ignore-all lint/suspicious/useAwait: don't care in tests */ | ||
| import { createExecutionContext, env, waitOnExecutionContext } from 'cloudflare:test' | ||
| import type { KeysDb } from '@pydantic/ai-gateway' | ||
| import { describe, expect } from 'vitest' | ||
| import { apiKeyAuth, changeProjectState } from '../src/auth' | ||
| import type { ApiKeyInfo } from '../src/types' | ||
| import { test } from './setup' | ||
| import { buildGatewayEnv, IDS } from './worker' | ||
|
|
||
| class CountingKeysDb implements KeysDb { | ||
| callCount = 0 | ||
| private wrapped: KeysDb | ||
|
|
||
| constructor(wrapped: KeysDb) { | ||
| this.wrapped = wrapped | ||
| } | ||
|
|
||
| async getApiKey(key: string): Promise<ApiKeyInfo | null> { | ||
| this.callCount++ | ||
| return this.wrapped.getApiKey(key) | ||
| } | ||
|
|
||
| async disableKey(id: number, reason: string, newStatus: string, expirationTtl?: number): Promise<void> { | ||
| return this.wrapped.disableKey(id, reason, newStatus, expirationTtl) | ||
| } | ||
| } | ||
|
|
||
| describe('apiKeyAuth cache invalidation', () => { | ||
| test('caches api key and returns cached value', async () => { | ||
| const ctx = createExecutionContext() | ||
| const baseOptions = buildGatewayEnv(env, [], fetch) | ||
| const countingDb = new CountingKeysDb(baseOptions.keysDb) | ||
| const options = { ...baseOptions, keysDb: countingDb } | ||
|
|
||
| const request = new Request('https://example.com', { headers: { Authorization: 'healthy' } }) | ||
|
|
||
| // First call should fetch from DB | ||
| const apiKey1 = await apiKeyAuth(request, ctx, options) | ||
| expect(apiKey1.key).toBe('healthy') | ||
| // Wait for cache to be set (it's set asynchronously via runAfter) | ||
| await waitOnExecutionContext(ctx) | ||
| expect(countingDb.callCount).toBe(1) | ||
|
|
||
| // Verify cache was set | ||
| const cached = await env.KV.get('apiKeyAuth:test:healthy') | ||
| expect(cached).toBeTypeOf('string') | ||
|
|
||
| // Second call should use cache, not hit DB | ||
| const ctx2 = createExecutionContext() | ||
| const apiKey2 = await apiKeyAuth(request, ctx2, options) | ||
| expect(apiKey2.key).toBe('healthy') | ||
|
|
||
| expect(countingDb.callCount).toBe(1) | ||
| }) | ||
|
|
||
| test('invalidates cache when project state changes', async () => { | ||
| const ctx = createExecutionContext() | ||
| const baseOptions = buildGatewayEnv(env, [], fetch) | ||
| const countingDb = new CountingKeysDb(baseOptions.keysDb) | ||
| const options = { ...baseOptions, keysDb: countingDb } | ||
|
|
||
| const request = new Request('https://example.com', { headers: { Authorization: 'healthy' } }) | ||
|
|
||
| // First call - fetch from DB and cache | ||
| await apiKeyAuth(request, ctx, options) | ||
| await waitOnExecutionContext(ctx) | ||
| expect(countingDb.callCount).toBe(1) | ||
|
|
||
| const cached1 = await env.KV.getWithMetadata('apiKeyAuth:test:healthy') | ||
| expect(cached1.value).not.toBeNull() | ||
| expect(cached1.metadata).toBeNull() | ||
|
|
||
| // Second call - should use cache, not hit DB | ||
| const ctx2 = createExecutionContext() | ||
| await apiKeyAuth(request, ctx2, options) | ||
| await waitOnExecutionContext(ctx2) | ||
| expect(countingDb.callCount).toBe(1) | ||
|
|
||
| // Change project state - this invalidates the cache | ||
| await changeProjectState(IDS.projectDefault, options) | ||
|
|
||
| const projectState = await env.KV.get(`projectState:test:${IDS.projectDefault}`) | ||
| expect(projectState).not.toBeNull() | ||
|
|
||
| // Third call - cache is invalidated, should hit DB again | ||
| const ctx3 = createExecutionContext() | ||
| const apiKey3 = await apiKeyAuth(request, ctx3, options) | ||
| expect(apiKey3.key).toBe('healthy') | ||
| await waitOnExecutionContext(ctx3) | ||
|
|
||
| expect(countingDb.callCount).toBe(2) | ||
| }) | ||
| }) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The magic number 30 should be extracted to a named constant (e.g.,
CACHE_TTL_DAYS) to clarify that this represents 30 days in seconds. This improves readability and maintainability:const CACHE_TTL_DAYS = 30; const CACHE_TTL = 86400 * CACHE_TTL_DAYS