perf(agent): cache IP whitelist rules across requests#324
Open
matthv wants to merge 1 commit into
Open
Conversation
`Facades::Whitelist.check_ip` runs on every authenticated request and instantiated a fresh `IpWhitelist` each time, whose rules were memoized only in instance variables. Every request therefore issued a new HTTPS call to `GET /liana/v1/ip-whitelist-rules` (new TLS connection, no pooling) — profiling showed this dominated ~65% of request wall-time. Cache the fetched configuration in a `FileCache` with the `permission_expiration` TTL, mirroring `Permissions`. Adds `IpWhitelist.invalidate_cache` (ready for the SSE cache-invalidation `TODO`). The API call is isolated in `fetch_ip_whitelist_from_api` so failures are never cached. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
512ad61 to
57fbc5b
Compare
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.
Context
ForestAdminAgent::Routes::AbstractAuthenticatedRoutecallsFacades::Whitelist.check_ipon every authenticated request, andcheck_ipbuilds a freshIpWhitelistinstance each time.IpWhitelistmemoized its rules only in instance variables (@rules/@use_ip_whitelist), with no cross-request cache — so every request issued a new HTTPS call toGET /liana/v1/ip-whitelist-rules, opening a new TLS connection each time (no pooling).Profiling a trivial endpoint (
.../relationships/transactions/count) under load showed ~65% of request wall-time inTCPSocket#initialize/Net::HTTP#connect, all originating fromIpWhitelist#fetch_rules. On a mini benchmark this made cheap endpoints 3–6× slower than they should be (e.g. a count: ~33 ms with only ~1.7 ms of SQL).By contrast, permissions are already cached (
FileCache,permission_expirationTTL) — 0 permission fetches over the same burst.Change
IpWhitelistnow caches the fetched configuration in aFileCache('ip_whitelist', cache_dir, permission_expiration)viaget_or_set, mirroringPermissions:permission_expiration(same as permissions);fetch_ip_whitelist_from_api, so failed fetches raise and are not cached.Measured effect
Same scenario, before → after (median of 7, warm):
The per-request outbound HTTPS call to the Forest server is gone (first request warms the cache, subsequent ones read from
FileCache).Tests
ip_whitelist_spec.rb: addedbefore { described_class.invalidate_cache }(the disk cache otherwise leaks between examples), plus a new test asserting the API is fetched once across two instances.spec_helper.rb: addedpermission_expiration: 100to the shared agent config (needed now that the service builds aFileCachewith that TTL)..rubocop.yml:ip_whitelist.rbadded to theMetrics/ClassLengthexclude list, alongsidepermissions.rb.Full
forest_admin_agentsuite: 719 examples, 0 failures. RuboCop clean.🤖 Generated with Claude Code
Note
Cache IP whitelist rules across requests using a file-backed cache
forest.ip_whitelist) toIpWhitelistso the API is called only once per TTL window instead of on every request.fetch_ip_whitelist_from_api, withfetch_rulesnow reading from cache viaget_or_set.IpWhitelist.invalidate_cacheclass method to manually clear the cached whitelist entry.beforehook callinginvalidate_cachefor isolation and a new test asserting a single API call across multiple instances.Macroscope summarized 57fbc5b.