Skip to content

feat: configurable, reused DB connection pool - #425

Open
jabbrwcky wants to merge 7 commits into
crossplane-contrib:masterfrom
jabbrwcky:feat/configurable-connection-pool
Open

feat: configurable, reused DB connection pool#425
jabbrwcky wants to merge 7 commits into
crossplane-contrib:masterfrom
jabbrwcky:feat/configurable-connection-pool

Conversation

@jabbrwcky

Copy link
Copy Markdown
Contributor

Description of your changes

Every DB client previously opened a new *sql.DB via sql.Open on every query and closed it immediately, so the provider held an effectively unbounded number of short-lived connections against each server.

This PR introduces a process-wide, DSN-keyed connection-pool cache (pkg/clients/pool): managed resources targeting the same server (driver + DSN) now share a single bounded *sql.DB. Pools apply MaxOpenConns / MaxIdleConns / ConnMaxLifetime / ConnMaxIdleTime and are evicted after going idle, so a credential rotation (new DSN) does not leak pools.

The pool is tunable per ProviderConfig / ClusterProviderConfig via a new optional connectionPool block, for all three databases (MySQL, PostgreSQL, MSSQL), cluster- and namespaced-scoped. Omitted fields use defaults: maxOpenConnections=10, maxIdleConnections=5, maxConnLifetime=1h, maxConnIdleTime=10m.

Notable design points for reviewers:

  • The xsql.DB interface intentionally gains no Close(): the cache owns pool lifecycle, so the per-controller Disconnect methods stay no-ops.
  • Pools are shared per (driver, DSN); the config that first creates a pool for a DSN wins (in practice a DSN maps to one ProviderConfig).
  • Defaults are applied per-field and on a nil connectionPool, so existing ProviderConfigs keep working unchanged.

See docs/connection-pool.md for details.

Fixes #195

I have:

  • Read and followed Crossplane's contribution process.
  • Run make reviewable to ensure this PR is ready for review.

How has this code been tested

  • New unit tests for the pool cache (reuse per DSN, distinct pools per driver/DSN, idle-sweep eviction, active pools not swept).
  • Updated client and controller unit tests for the new constructor signatures.
  • make and make reviewable pass (generate is a no-op diff, lint and full go test ./... are green).

🤖 Generated with Claude Code

@jabbrwcky
jabbrwcky force-pushed the feat/configurable-connection-pool branch 2 times, most recently from 37dff8c to a077839 Compare July 23, 2026 08:08
jabbrwcky and others added 7 commits July 23, 2026 10:48
Every DB client previously opened a new *sql.DB via sql.Open on every query
and closed it immediately, so the provider held an effectively unbounded
number of short-lived connections against each server.

Add a process-wide connection-pool cache keyed by driver and DSN: managed
resources that target the same server (same driver + DSN) now share a
single bounded *sql.DB. Config carries MaxOpenConns / MaxIdleConns /
ConnMaxLifetime / ConnMaxIdleTime; pools that go unused are evicted so a
credential rotation (which changes the DSN) does not leak pools.

This package has no callers yet; it is wired into the DB clients and made
configurable via ProviderConfig in the following commits.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: Jens Hausherr <jens.hausherr@pflege.de>
Exec/Query/Scan (and postgresql's ExecTx) now fetch a connection from
pool.Get(driver, dsn, cfg) instead of calling sql.Open and closing it after
every call. New(...) for all three clients gains a trailing pool.Config
parameter so callers can tune the shared pool.

No caller passes a non-zero-value pool.Config yet; that lands with the
ProviderConfig API and controller wiring in the following commits.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: Jens Hausherr <jens.hausherr@pflege.de>
Add an optional ConnectionPool field (MaxOpenConnections, MaxIdleConnections,
MaxConnLifetime, MaxConnIdleTime) plus a nil-safe ToPoolConfig() method to
every ProviderConfigSpec / ClusterProviderConfigSpec, for all three databases
and both cluster and namespaced scopes. Omitted fields, or an omitted
connectionPool block entirely, default to pool.Default.

Regenerate zz_generated.deepcopy.go and the CRD manifests under package/crds
via `make generate`.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: Jens Hausherr <jens.hausherr@pflege.de>
Every connector's newDB/newClient field and every Connect implementation
now computes a pool.Config from the resource's ProviderConfig (cluster
controllers via pc.Spec.ConnectionPool, namespaced controllers via the
provider.GetProviderConfig helper's ProviderInfo.PoolConfig) and passes it
as the last argument to the client constructor. The 3 namespaced
provider.go helpers (mysql/postgresql/mssql) gain a PoolConfig field on
ProviderInfo, populated from ConnectionPool.ToPoolConfig() for both the
ProviderConfig and ClusterProviderConfig kinds.

Test mocks are updated to match the new constructor signatures.

With this commit the connection-pool feature is fully wired end to end.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: Jens Hausherr <jens.hausherr@pflege.de>
Add docs/connection-pool.md covering the connectionPool fields, defaults
(maxOpenConnections=10, maxIdleConnections=5, maxConnLifetime=1h,
maxConnIdleTime=10m), and pool-sharing/eviction behavior. Add a
connectionPool block to the cluster ProviderConfig example for each
database.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: Jens Hausherr <jens.hausherr@pflege.de>
The cache test opener called sql.Open with an unregistered driver name,
which returns (nil, err) on current Go. The stub ignored the error and
cached a nil *sql.DB, so the cleanup's db.Close() nil-panicked in CI
(SIGSEGV in TestGetReusesSamePool).

Hand the cache a real, non-nil, closeable *sql.DB via sql.OpenDB with a
fake driver.Connector — no registered driver needed and the tests never
query. Also nil-guard the cleanup Close defensively.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: Jens Hausherr <jens.hausherr@pflege.de>
ToPoolConfig() (6 apis packages), pool.Get's error path, Config.apply, and
the 3 namespaced GetProviderConfig helpers were new logic introduced by the
connection-pool feature with 0% coverage anywhere in the repo. Add targeted
unit tests for each:

- ToPoolConfig(): table-driven, nil receiver / per-field defaulting / all-set,
  in all 6 apis/{cluster,namespaced}/{mysql,postgresql,mssql}/v1alpha1
  packages that define the (duplicated) ConnectionPool type.
- pool.Get: error propagation when openDB fails (nothing gets cached).
- Config.apply: verifies MaxOpenConnections via sql.DBStats (the only field
  database/sql exposes a public getter for); the other three setters are
  smoke-tested since they have no equivalent public getter.
- GetProviderConfig (namespaced mysql/postgresql/mssql provider helpers):
  both ProviderConfig/ClusterProviderConfig branches, error paths, and that
  a connectionPool override reaches ProviderInfo.PoolConfig, using the
  existing test.MockClient pattern already used by this repo's reconciler
  tests.

Deliberately out of scope: DB client Exec/Query/Scan and reconciler Connect
success paths remain 0%-covered, but that predates this branch (confirmed
unchanged vs master) and would need a fake-driver/sqlmock or fake-kube-client
investment disproportionate to this change.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: Jens Hausherr <jens.hausherr@pflege.de>
@jabbrwcky
jabbrwcky force-pushed the feat/configurable-connection-pool branch from b546801 to b072699 Compare July 23, 2026 12:55
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.

Provider should use connection pooling or single transaction to minimize load on database when reconciling

1 participant