Repository navigation
fix: share token cache across Laravel Octane workers - #38
Open
Falgunimoridhara wants to merge 1 commit into
Open
Falgunimoridhara wants to merge 1 commit into
Falgunimoridhara wants to merge 1 commit into
Conversation
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.
Fix: share token cache across Laravel Octane workers (prevents HTTP 429)
Closes #10
Problem
Every call to
OauthClient::createToken()sends a new request to the OAuth server. Under Laravel Octane(Swoole/RoadRunner) each worker has its own memory, so all workers request tokens independently, and the OAuth
server answers with HTTP 429.
Solution
Add an opt-in
TokenManagerthat keeps the token in a Laravel cache store shared by all workers, with atomiclocks so only one worker requests a token at a time.
getToken($scopes, $ttl = 3600):current token (stale-while-revalidate) and never block.
lock_waitseconds, then read itfrom the cache. A double-check inside the lock avoids duplicate requests.
The real expiry comes from the
expirefield of the/tokenresponse (unix timestamp), capped at the requested$ttl. Ifexpireis missing or already in the past,$ttlis used.Changes
New
src/TokenManager.php: cache-backed token provider (takes anIlluminate\Contracts\Cache\Repository; throws ifthe store has no lock support).
src/Laravel/OpenapiServiceProvider.php: auto-discovered provider bindingOauthClientandTokenManager.config/openapi.php: credentials plus atokenblock (store,prefix,refresh_skew,lock_seconds,lock_wait).store = nulluses the application's default cache store.tests/TokenManagerTest.php,tests/Fakes/CountingOauthClient.php,tests/Fakes/StaleFirstReadRepository.php.Modified
src/OauthClient.php: addedgetUsername()andgetBaseUrl()(used to build the cache key).composer.json:illuminate/cache(^9 || ^10 || ^11 || ^12) inrequire-devandsuggest;extra.laravel.providersfor auto-discovery.README.md,CLAUDE.md: documentation.Client,CurlTransportandArrayCacheare unchanged.Usage
How Has This Been Tested?
./vendor/bin/phpunit tests/: 44 tests (6 skipped, as before).