feat(decisioning): LazyPlatformRouter for tenant-on-first-request construction#552
Merged
feat(decisioning): LazyPlatformRouter for tenant-on-first-request construction#552
Conversation
…struction Drop-in alternative to PlatformRouter that defers per-tenant DecisioningPlatform construction to first request, with a bounded LRU + TTL cache. Closes the eager-router pain point for adopters with N tenants × per-tenant SDK auth handshake (Google Ad Manager service-account, Kevel API key) where boot scales O(N). * Async or sync factory; awaited via inspect.isawaitable, matching CallableSubdomainTenantRouter's convention (PR #544). * Bounded cache: cache_size > 0 mandatory (default 256); cache_ttl_seconds >= 0 (default 3600.0; 0 = size-only eviction). Distinct from CallableSubdomainTenantRouter which rejects ttl=0 — there tenants go stale, here platform adapters don't (unless the factory reads mutable config — docstring calls that out). * invalidate(tenant_id=None) for hot-reload. Per-tenant + global generation counter snapshots prevent the invalidate-during-build race from resurrecting an evicted slot. * Drop-in: isinstance(router, DecisioningPlatform) is true, serve() accepts it identically, ACCOUNT_NOT_FOUND / UNSUPPORTED_FEATURE projection matches PlatformRouter. * No singleflight in v1 — concurrent cold requests each build (locked by test_concurrent_cold_requests_each_build_v1_contract). * proposal_managers stays an eager dict (cheap to hold). * platform_for_tenant() async sibling-API parity for admin/health endpoints. * Extracted _select_proposal_method module-level so PlatformRouter and LazyPlatformRouter share the same routing logic without drift. Closes #547. Co-Authored-By: Claude Opus 4.7 (1M context) <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
Closes #547. Drop-in alternative to
PlatformRouterthat defers per-tenantDecisioningPlatformconstruction to first request, with a bounded LRU + TTL cache. Closes the eager-router pain point for adopters with N tenants × per-tenant SDK auth handshake (Google Ad Manager service-account, Kevel API key) where boot scales O(N).inspect.isawaitable, matchingCallableSubdomainTenantRouter's convention (feat(server): CallableSubdomainTenantRouter for DB-backed tenant lookups #544)cache_size > 0mandatory (default 256);cache_ttl_seconds >= 0(default 3600.0; 0 = size-only eviction). Distinct fromCallableSubdomainTenantRouterwhich rejects ttl=0 — tenants go stale, platform adapters don't (unless the factory reads mutable config — docstring calls that out)invalidate(tenant_id=None)for hot-reload. Per-tenant + global generation counter snapshots prevent the invalidate-during-build race from resurrecting an evicted slotisinstance(router, DecisioningPlatform)is true,serve()accepts it identically,ACCOUNT_NOT_FOUND/UNSUPPORTED_FEATUREprojection matchesPlatformRoutertest_concurrent_cold_requests_each_build_v1_contract)platform_for_tenant()async sibling-API parity for admin/health endpoints_select_proposal_methodmodule-level so both routers share the same routing logic without driftTest plan
tests/test_lazy_platform_router.pycover: drop-in compat, lazy build, async/sync factory + child, LRU eviction, TTL expiry (with monkeypatched clock),cache_ttl_seconds=0size-only mode, construction validation, factory rejection paths (None / wrong type / raises),invalidatesemantics, invalidate-during-build race contract (specific tenant + global), thundering herd v1 contract (concurrent cold = both build), proposal_managers routing (manager vs fall-through),platform_for_tenantintrospection, unknown-tenant + unsupported-method projectiontest_platform_router.py,test_proposal_manager.py,test_subdomain_tenant_router.pyruff check+mypyclean on changed filesReviewed by
Pre-PR review by
code-reviewer,ad-tech-protocol-expert,dx-expert. Material concerns addressed:_select_proposal_methodduplication between PlatformRouter (method) and LazyPlatformRouter (module-level) → consolidated to module-level onlyplatform_for_tenant()parity with eager router → addedrecovery == "terminal"assertion → added🤖 Generated with Claude Code