You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: support percentage based db limits with reload support
**Summary**
Introduce a context aware DB dial path, a new `ConnPercentage` knob to cap
Auth's share of Postgres connections, and background wiring to apply pool
changes on config reloads.
**Storage / DB**
- Add `DialContext(ctx, *conf.GlobalConfiguration)` and keep `Dial(...)`
as a thin wrapper. `serve` now passes its cancelable context so startup
can't hang indefinitely.
- `Connection` now keeps a handle to the underlying `*sql.DB` (via
`popConnToStd`) when available.
- New helpers:
- `newConnectionDetails` and `applyDBDriver` to build `pop.ConnectionDetails`
and derive driver when omitted.
- `Connection.Copy()` to retain `sqldb` reference and updated locations that
copy (`WithContext, Transaction)`.
- Runtime tuning API: `(*Connection).ApplyConfig(ctx, cfg, le)` computes and
applies connection limits to the underlying `*sql.DB`.
- Fixed limits come from `MaxPoolSize`, `MaxIdlePoolSize`,
`ConnMaxLifetime`, `ConnMaxIdleTime`.
- If `ConnPercentage` is set (1-100), compute limits from
`SHOW max_connections`, prefer percentage over fixed pool sizes, and
set idle = open.
- Retains previous behavior when `ConnPercentage` is `0`
- No-op (and error) if `*sql.DB` is unavailable.
**API worker**
- `apiworker.New` now accepts the DB connection.
- Split worker into three goroutines (via `errgroup`):
- `configNotifier` fans out reload signals,
- `templateWorker` refreshes template cache,
- `dbWorker` applies DB connection limits on boot and each reload.
**Serve**
- Use `storage.DialContext(ctx, cfg)` and then `db = db.WithContext(ctx)` so
the DB handle participates in request/trace context and shutdown.
**Observability**
- Add `observability.NewLogEntry(*logrus.Entry)` to construct chi middleware
log entries.
- Structured logs around applying DB limits.
**Configuration knobs** (`GOTRUE_DB_*`)
- `GOTRUE_DB_CONN_PERCENTAGE` (int, clamped to `[0,100]`):
- `0` (default) disables percentage-based sizing.
- `1-100` reserves that % of `max_connections` for the Auth server.
**Tests**
- `internal/storage/dial_test.go`
- `DialContext` happy path and invalid driver/URL error path.
- Reflection bridge to `*sql.DB` (`popConnToStd`) including
`WithContext`-wrapped connection behavior.
- `ApplyConfig` end-to-end: verify pool sizing and stats reflect limits.
- Percentage math and precedence vs fixed pools across edge cases.
- `internal/conf/configuration_test.go`
- Validation clamps `ConnPercentage` to `[0,100]`.
0 commit comments