Repository navigation
driver/postgres: a DSN without connect_timeout gets a 10 s default (v1.6.0, Refs #1212) - #1223
Merged
Merged
Conversation
|
Caution Review failedThe pull request is closed.
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
…e default reaches the dialer and that the error names it; document that PG* variables are not read (Refs #1212)
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.
driver/postgres: a DSN without connect_timeout gets a 10 s default; connect_timeout=0 keeps no timeout. Refs #1212 (finishes it; maintainer decision 2026-11-01, review r1 of #1216 section 4).
The problem
#1216 made
connect_timeoutbound the dial and the startup together, but kept the documented "zero means no client-side timeout" for the unset case. So withcontext.Background()and noconnect_timeoutin the DSN, a server that accepts the connection and never answers still heldOpen/acquire for ever. Redis and memcached default to 5 s. The maintainer decided on a 10 s default whenconnect_timeoutis absent (a backend fork plus SCRAM, and serverless databases that resume from pause, take seconds; 10 s still turns a forever-hang into a clear error), and an explicitconnect_timeout=0keeps the libpq meaning, no timeout. A negative value stays rejected at DSN parse (#1216).The change
Options.connectTimeoutExplicit(precedent:autoCacheStatementsExplicit) records that the DSN parser saw aconnect_timeout, in both the key=value and the URL form (they sharesetOption). Absent and0both gaveConnectTimeout == 0before; now they differ.Options.effectiveConnectTimeout()is the budget:ConnectTimeoutif > 0; none for an explicit 0; the default (10 s,defaultConnectTimeout) if the key was absent. In a DSN built in code (no parser), zero is "not set" (the default applies) and a negative value is "no timeout"; that is documented onOptions.ConnectTimeout.beginDial,connectDeadlineand the startup bound all use it, so the three dial paths (direct, unix event loop, net.Conn loop),Pool,OpenandConnectorget it the same way (nothing else readsConnectTimeout).startup timed out (default connect_timeout 10s; set connect_timeout=0 for none)for the default,(connect_timeout 3s)for a set one. Both matcherrors.Is(err, context.DeadlineExceeded).Options.ConnectTimeoutdescribes the default, the explicit 0, the negative value, the code-built DSN, and still says the DNS lookup is not bounded.testHookDefaultConnectTimeout(nil in production, like driver/redis: bound the connection handshake by DialTimeout (v1.6.0, Refs #1205) #1209's) lowers the default so the tests do not wait 10 s.Commits:
d7db3d6the tests (RED on main; the hook is declared there, inert, so the tests compile on main),0f08aa7the fix (and319f2d8, the r2 follow-up).RED / GREEN
RED,
d7db3d6alone (macOS arm64,go test ./driver/postgres -run 'Default|Explicit' -v; the failures are the 2 s watchdog message, not a test timeout):GREEN,
dc6ad43: all pass. Noconnect_timeout,context.Background(), silent server, default lowered to 300 ms: the dial fails after 300 ms on all three paths and both DSN forms, and throughOpen+Ping.connect_timeout=0with a caller deadline of 900 ms: ends at the caller's 900 ms, the error is the caller's (not "startup timed out").connect_timeout=1with the default lowered to 300 ms: ends at 1 s with the driver's error.TestEffectiveConnectTimeout1212pins the table (absent = 10 s default, 0 = none, N = N s, in both forms; code-built DSN zero = default, negative = none).Mutants (each alone,
go test -run 'Default|Explicit|Effective|1212' -count=1)connect_timeout=0is not rememberedconnect_timeoutloses to the defaultChecks
-race -run 'Default|Explicit|Effective|1212|ParseDSN' -count=20Linux dockergolang:1.27.3--cpus 2: ok; wholedriver/postgres-race -count=1: ok (68 s). macOS-race -count=20of the new tests: ok.GOOS=windows go vet ./driver/postgres: clean.golangci-lintv2.15.0GOOS=linux: only the two pre-existingerrcheckfindings in untouched test files.Release note
driver/postgres: when the DSN has no
connect_timeout, the dial and the startup are now bounded by a default of 10 s (before: no bound other than the caller's context, so a server that accepted the connection and never answered heldOpen/acquire for ever undercontext.Background()). Setconnect_timeout=0to keep the old behaviour (wait indefinitely, as in libpq), or a larger value for servers that are slow to accept or authenticate (for example serverless databases resuming from pause (Aurora Serverless v2 with auto-pause resumes in up to about 15 s, longer than the default: setconnect_timeout=30, or 0, for it)). A negativeconnect_timeoutis rejected, as before.Review r2 follow-up (commit
319f2d8)connect_timeout = -1: must be >= 0 (0: no timeout; absent: the 10s default); the test checks it.TestDialerTimeoutIsTheBudget1212assertsbeginDial'snet.Dialer.Timeoutis the budget (absent: 10 s, or the hook's value; 0: none; N: N seconds; both DSN forms). The mutant that keeps the rawConnectTimeoutfor the dialer, which survived (a host that drops connection attempts would hold a dial with noconnect_timeoutfor the OS connect timeout, one to two minutes), now fails it.TestStartupIsBoundedByTheDefaultConnectTimeout1212requiresdefault connect_timeout 300ms; set connect_timeout=0 for none, and the explicit-timeout cases require its absence (the mutant "the label never says default" now fails).ConnectTimeoutdoc says thePG*environment variables (PGCONNECT_TIMEOUT...) are not read. The fix commit cited above is corrected (dc6ad43was amended into0f08aa7).319f2d8: plainTESTING_STRICT_ALLOC_BUDGETS=1 go test ./driver/postgres -run '^TestAllocBudgets$'ok (macOS, Linux docker);-race -run 'Default|Explicit|Effective|Dialer|1212|ParseDSN' -count=20Linux docker--cpus 2ok (231 s); wholedriver/postgres-race -count=1Linux ok (66 s);GOOS=windows go vetclean; golangci-lint only the two pre-existingerrcheckfindings.