Skip to content

refactor(connection): delegate URI parsing to tokio_postgres::Config#731

Merged
NikolayS merged 2 commits intomainfrom
refactor/uri-use-tokio-config
Mar 24, 2026
Merged

refactor(connection): delegate URI parsing to tokio_postgres::Config#731
NikolayS merged 2 commits intomainfrom
refactor/uri-use-tokio-config

Conversation

@NikolayS
Copy link
Copy Markdown
Owner

Summary

Replaces the hand-rolled parse_uri() implementation with delegation to tokio_postgres::Config::from_str().

Motivation

The custom URI parser had a bug (C5 from the #709 audit): host= and port= in the URI query string fell through a _ => {} catch-all and were silently discarded. Rather than patch individual params, this replaces the parser wholesale with the battle-tested tokio-postgres implementation.

What changes

New helpers:

  • extract_ssl_file_params() — extracts sslcert, sslkey, sslrootcert from URI or connstring. These are the only params tokio-postgres omits (it delegates TLS entirely to the caller).
  • sanitize_uri_for_tokio() — strips/rewrites params tokio-postgres doesn't recognise (sslcert, sslkey, sslrootcert) and maps rpg-extended sslmode/target_session_attrs values to accepted equivalents before parsing.

parse_uri() internals replaced:

  • Delegates to tokio_postgres::Config::from_str() on the sanitized URI
  • Reconstructs UriParams from the parsed Config
  • Captures rpg-extended sslmode / target_session_attrs values from the raw URI before sanitization

UriParams struct and all resolve_* callers: unchanged.

Bug fixed

C5 (#709): postgres://host/db?host=socket_dir&port=5433 — the query-string host and port params were silently dropped by the old parser. tokio-postgres handles these correctly.

Behavior change

Multi-host URI port assignment now matches real libpq behavior: a host without an explicit port gets the default (5432), not the last explicitly-seen port. Verified with psql: postgresql://localhost:5432,localhost:5433,localhost/postgres tries the third host on port 5432. Test updated accordingly.

Tests

1702/1702 unit tests pass. Existing parse_uri test coverage exercises the refactored path.

Closes #709 (C5 item).

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Mar 24, 2026

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 71.72414% with 41 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (main@5f7e000). Learn more about missing BASE report.

Files with missing lines Patch % Lines
src/connection.rs 71.72% 41 Missing ⚠️
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.
Additional details and impacted files
@@           Coverage Diff           @@
##             main     #731   +/-   ##
=======================================
  Coverage        ?   68.75%           
=======================================
  Files           ?       46           
  Lines           ?    31061           
  Branches        ?        0           
=======================================
  Hits            ?    21354           
  Misses          ?     9707           
  Partials        ?        0           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@NikolayS
Copy link
Copy Markdown
Owner Author

REV

16/16 CI green including both Windows targets (second push fixed the unreachable-pattern warning from the cfg(not(unix)) wildcard arm).

C5 fix verified

postgres://ignored:9999/postgres?host=localhost&port=15433&user=postgres

Before: host=/port= query params silently dropped, connection attempted to ignored:9999.
After: connects to localhost:15433. Fixed.

Code review

Scope is clean. UriParams struct and all resolve_* callers untouched. Only parse_uri() internals replaced. parse_conninfo() (key=value path) untouched.

extract_ssl_file_params — correct for the URI branch. The connstring branch is dead code: this function is only ever called from parse_uri(), never for connstrings. Not a bug — non-blocking cleanup for a follow-up.

sanitize_uri_for_tokio — correctly strips the three TLS file params and remaps extended sslmode/target_session_attrs values before handing off to tokio-postgres.

parse_uri — clean. Raw sslmode and tsa captured before sanitization so extended values survive. Port reconstruction handles 0/1/N cases correctly. #[cfg(unix)] gate on Host::Unix arm is correct.

Test update — multi-host URI port assignment now matches real libpq: a portless host gets 5432, not the last explicitly-seen port. Verified with psql.

1702/1702 unit tests pass.

Replace the hand-rolled parse_uri() implementation with delegation to
tokio_postgres::Config::from_str(), which handles all standard libpq
URI parameters including multi-host, Unix socket paths, percent-encoding,
sslmode, connect_timeout, options, target_session_attrs, and query-string
host=/port= params (fixing the C5 silent-drop bug).

The only params tokio-postgres omits are the three TLS file params
(sslcert, sslkey, sslrootcert) because it delegates TLS entirely to the
caller.  These are extracted from the raw URI via extract_ssl_file_params()
before the sanitized copy is handed to tokio-postgres.

rpg-extended sslmode values (allow, verify-ca, verify-full) and
target_session_attrs values (primary, standby, prefer-standby) are also
captured from the raw URI before sanitization.

As a side effect, multi-host URI port assignment now matches real libpq:
a host without an explicit port gets 5432, not the last explicitly-seen
port.  The test expectation is updated accordingly.
…-unix

On Windows, tokio_postgres::config::Host has no Unix variant so the
cfg(not(unix)) catch-all triggered an unreachable-patterns warning
(promoted to error by -D warnings).  Drop the dead arm entirely.
@NikolayS NikolayS force-pushed the refactor/uri-use-tokio-config branch from ec8f4f2 to 3d5884e Compare March 24, 2026 23:11
@NikolayS NikolayS merged commit 5d68046 into main Mar 24, 2026
16 checks passed
@NikolayS NikolayS deleted the refactor/uri-use-tokio-config branch March 24, 2026 23:22
NikolayS added a commit that referenced this pull request Mar 25, 2026
Changes since v0.8.2:
- fix(connection): delegate URI parsing to tokio_postgres::Config (#731)
- fix(connection): scan for any .s.PGSQL.<port> socket (#728)
- test(connection): add connection path integration tests (#730)
NikolayS added a commit that referenced this pull request Mar 25, 2026
Changes since v0.8.2:
- fix(connection): delegate URI parsing to tokio_postgres::Config (#731)
- fix(connection): scan for any .s.PGSQL.<port> socket (#728)
- test(connection): add connection path integration tests (#730)
NikolayS added a commit that referenced this pull request Mar 25, 2026
Changes since v0.8.2:
- fix(connection): delegate URI parsing to tokio_postgres::Config (#731)
- fix(connection): scan for any .s.PGSQL.<port> socket (#728)
- test(connection): add connection path integration tests (#730)
NikolayS added a commit that referenced this pull request Mar 25, 2026
Changes since v0.8.2:
- fix(connection): delegate URI parsing to tokio_postgres::Config (#731)
- fix(connection): scan for any .s.PGSQL.<port> socket (#728)
- test(connection): add connection path integration tests (#730)
NikolayS added a commit that referenced this pull request Mar 25, 2026
Changes since v0.8.2:
- fix(connection): delegate URI parsing to tokio_postgres::Config (#731)
- fix(connection): scan for any .s.PGSQL.<port> socket (#728)
- test(connection): add connection path integration tests (#730)
NikolayS added a commit that referenced this pull request Mar 25, 2026
Changes since v0.8.2:
- fix(connection): delegate URI parsing to tokio_postgres::Config (#731)
- fix(connection): scan for any .s.PGSQL.<port> socket (#728)
- test(connection): add connection path integration tests (#730)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

test: verify all connection paths work correctly

2 participants