-
Notifications
You must be signed in to change notification settings - Fork 117
feat(router): env-configurable DB pool size + async billing debit for scale #959
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
Open
steventohme
wants to merge
5
commits into
main
Choose a base branch
from
router-db-pool-scaling
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e0cb4cb
feat(router): env-configurable DB pool size + async billing debit for…
steventohme 3727959
fix(router): drain async billing debits on shutdown + CodeQL/nit clea…
steventohme c0c24a9
fix(router): parse ROUTER_POSTGRES_MAX_CONNS at int32 width (CodeQL)
steventohme 6997d9f
fix(router): bound billing drain, drain serverErr path, poll async tests
steventohme 92a30db
style(router): trim doc comments per review nits
steventohme 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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -81,12 +81,18 @@ func main() { | |||||||||||||
| // 6 conns covers MarkUsed writes plus session-pin traffic (auth-cache and | ||||||||||||||
| // the in-proc LRU absorb most reads). If pgxpool wait p95 climbs above 1ms | ||||||||||||||
| // with pinning on, that's the migrate-to-Memorystore signal, not a bigger pool. | ||||||||||||||
| cfg.MaxConns = 6 | ||||||||||||||
| // ROUTER_POSTGRES_MAX_CONNS overrides for high-concurrency deploys; | ||||||||||||||
| // raise only after checking pgxpool wait p95 (see comment above). | ||||||||||||||
| cfg.MaxConns = parseEnvInt32("ROUTER_POSTGRES_MAX_CONNS", 6) | ||||||||||||||
| cfg.MinConns = 1 | ||||||||||||||
| cfg.MaxConnLifetime = 30 * time.Minute | ||||||||||||||
| cfg.MaxConnIdleTime = 10 * time.Minute | ||||||||||||||
| cfg.HealthCheckPeriod = 1 * time.Minute | ||||||||||||||
|
|
||||||||||||||
| // Tracks async billing debits so graceful shutdown can drain them before | ||||||||||||||
| // pool.Close (see the drain Wait call after srv.Shutdown). | ||||||||||||||
| billingInflight := &observability.TrackedGroup{} | ||||||||||||||
|
|
||||||||||||||
| pool, err := pgxpool.NewWithConfig(context.Background(), cfg) | ||||||||||||||
| if err != nil { | ||||||||||||||
| logger.Error("Failed to construct postgres pool", "err", err) | ||||||||||||||
|
|
@@ -844,7 +850,8 @@ func main() { | |||||||||||||
| WithCompaction(compactionSz, compactionPct). | ||||||||||||||
| WithAvailableModels(routingTargets). | ||||||||||||||
| WithDefaultBaselineModel(resolveDefaultBaselineModel()). | ||||||||||||||
| WithBillingService(billingSvc) | ||||||||||||||
| WithBillingService(billingSvc). | ||||||||||||||
| WithBillingDrainGroup(billingInflight) | ||||||||||||||
| for _, spec := range configuredPolicySpecs { | ||||||||||||||
| proxySvc = proxySvc.WithPolicyStrategy(spec) | ||||||||||||||
| logger.Info("Generic policy sidecar wired", "strategy", spec.Strategy, "candidate_models", len(routingTargets)) | ||||||||||||||
|
|
@@ -1000,15 +1007,21 @@ func main() { | |||||||||||||
| logger.Info("Received shutdown signal; draining", "signal", sig.String()) | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // Cloud Run gives 10s between SIGTERM and SIGKILL; budget across three | ||||||||||||||
| // Cloud Run gives 10s between SIGTERM and SIGKILL; budget across four | ||||||||||||||
| // flush stages (defer on apm.Shutdown would never run in time): | ||||||||||||||
| // srv.Shutdown 6.0s + emitter.Shutdown 1.5s + apm.Shutdown 1.5s = 9.0s, | ||||||||||||||
| // leaving ~1s slack. | ||||||||||||||
| shutdownCtx, cancel := context.WithTimeout(context.Background(), 6*time.Second) | ||||||||||||||
| // srv.Shutdown 4.5s + billing drain 1.5s + emitter.Shutdown 1.5s | ||||||||||||||
| // + apm.Shutdown 1.5s = 9.0s, leaving ~1s slack. | ||||||||||||||
| shutdownCtx, cancel := context.WithTimeout(context.Background(), 4500*time.Millisecond) | ||||||||||||||
| defer cancel() | ||||||||||||||
| if err := srv.Shutdown(shutdownCtx); err != nil { | ||||||||||||||
| logger.Error("Graceful shutdown failed", "err", err) | ||||||||||||||
| } | ||||||||||||||
| // srv.Shutdown only waits for handler goroutines; billing debits run in | ||||||||||||||
| // SafeGoTracked goroutines it doesn't know about. Drain them before the | ||||||||||||||
| // deferred pool.Close runs so a deploy or scale-to-zero can't SIGKILL an | ||||||||||||||
| // in-flight debit and leave served inference unbilled. Bounded by each | ||||||||||||||
| // debit's own 5s timeout; the 1.5s window above covers the ledger write. | ||||||||||||||
|
steventohme marked this conversation as resolved.
Outdated
|
||||||||||||||
| billingInflight.Wait() | ||||||||||||||
|
steventohme marked this conversation as resolved.
Outdated
|
||||||||||||||
| emitterCtx, emitterCancel := context.WithTimeout(context.Background(), 1500*time.Millisecond) | ||||||||||||||
| defer emitterCancel() | ||||||||||||||
| if err := emitter.Shutdown(emitterCtx); err != nil { | ||||||||||||||
|
|
@@ -1320,6 +1333,23 @@ func parseEnvInt(key string, fallback int) int { | |||||||||||||
| return n | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // parseEnvInt32 reads an env var as a positive int32. Parses at bit size 32 so | ||||||||||||||
| // the value is int32-width at the source — no int->int32 narrowing on the | ||||||||||||||
| // returned value. Returns fallback when the var is unset, empty, out of | ||||||||||||||
| // int32 range, or unparseable. | ||||||||||||||
|
steventohme marked this conversation as resolved.
Comment on lines
+1345
to
+1348
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Was 4 lines; sentences 3–4 restate the fallback logic visible in the function body. |
||||||||||||||
| func parseEnvInt32(key string, fallback int32) int32 { | ||||||||||||||
| raw := config.GetOr(key, "") | ||||||||||||||
| if raw == "" { | ||||||||||||||
| return fallback | ||||||||||||||
| } | ||||||||||||||
| n, err := strconv.ParseInt(raw, 10, 32) | ||||||||||||||
| if err != nil || n <= 0 { | ||||||||||||||
| observability.Get().Warn("Invalid env var; using default", "key", key, "value", raw, "default", fallback) | ||||||||||||||
| return fallback | ||||||||||||||
| } | ||||||||||||||
| return int32(n) | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // parseEnvFloat reads an env var as a float64, falling back on unset/empty/ | ||||||||||||||
| // unparseable. Zero and negative values are valid — e.g. operators set | ||||||||||||||
| // ROUTER_SWITCH_EV_THRESHOLD_USD <= 0 to force aggressive planner switching. | ||||||||||||||
|
|
||||||||||||||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.