feat: add client caching to reduce OAuth token requests#63
Open
spbsoluble wants to merge 2 commits intomainfrom
Open
feat: add client caching to reduce OAuth token requests#63spbsoluble wants to merge 2 commits intomainfrom
spbsoluble wants to merge 2 commits intomainfrom
Conversation
Previously, every certificate request reconciliation created a new Command API client, which meant a new OAuth token was fetched for each request. For customers with OAuth provider quotas, this caused rate limiting issues. This change introduces a ClientCache that: - Caches Command API clients by configuration hash - Reuses cached clients across reconciliations for the same issuer - Allows the underlying oauth2 library's token caching to work as intended - Is thread-safe for concurrent reconciliations The cache key is a SHA-256 hash of all configuration fields that affect the client connection (hostname, API path, credentials, scopes, etc.), ensuring different issuers get different clients while the same issuer reuses its client. Fixes: OAuth token re-authentication on every request Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
ClientCachethat caches Command API clients by configuration hashgolang.org/x/oauth2token caching to work as intendedProblem
When using OAuth authentication, every certificate request reconciliation created a new Command API client. This meant:
clientcredentials.Config.TokenSource()was created each timeFor customers with OAuth provider quotas (common with enterprise IdPs), this caused rate limiting and quota exhaustion issues.
Solution
The fix introduces a
ClientCacheininternal/command/client_cache.gothat:sync.RWMutexfor safe concurrent access during reconciliationsIssuerReconcilerandCertificateRequestReconcileruse the same cache instancecmd/main.goneeded modification to create and inject the cacheCache Key Design
The cache key includes all fields that affect the client connection:
This ensures:
Test plan
configHash()functionClientCachebasic operationsManual testing
To verify the fix works:
🤖 Generated with Claude Code