Some checks failed
CI / test (push) Failing after 13s
- slog.go: SlogLogger adapts *slog.Logger to dbx.Logger interface - scan.go: Collect[T] and CollectOne[T] generic helpers using pgx.RowToStructByName - cluster.go: slow query logging via Config.SlowQueryThreshold (Warn level in queryEnd) - stats.go: PoolStats with Cluster.Stats() aggregating pool stats across all nodes - config.go/node.go: NodeConfig.Tracer passthrough for pgx.QueryTracer (OpenTelemetry) - options.go: WithSlowQueryThreshold and WithTracer functional options - dbxtest/tx.go: RunInTx runs callback in always-rolled-back transaction for test isolation Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2.9 KiB
2.9 KiB
CLAUDE.md — dbx
Commands
go build ./... # compile
go test ./... # all tests
go test -race ./... # tests with race detector
go test -v -run TestName ./... # single test
go vet ./... # static analysis
Architecture
- Module:
git.codelab.vc/pkg/dbx, Go 1.24, depends on pgx/v5 - Single package
dbx(+dbxtestfor test helpers)
Core patterns
- Cluster is the entry point — connects master + replicas, routes writes to master, reads to replicas with master fallback
- Routing is method-based:
Exec/Query/QueryRow/Begin/BeginTx/CopyFrom/SendBatch→ master;ReadQuery/ReadQueryRow→ replicas - Retry with exponential backoff + jitter, node fallback; retrier.do() iterates nodes then backs off
- Balancer interface (
Next([]*Node) *Node) — built-inRoundRobinBalancerskips unhealthy nodes - Health checker — background goroutine pings all nodes on an interval, flips
Node.healthyatomic bool - RunTx — panic-safe transaction wrapper: recovers panics, rolls back, re-panics
- Querier injection —
InjectQuerier/ExtractQuerierpassQueriervia context for service layers - SlogLogger — adapts
*slog.Loggerto thedbx.Loggerinterface (slog.go) - Collect/CollectOne — generic scan helpers using
pgx.RowToStructByName(scan.go) - Slow query logging —
Config.SlowQueryThresholdtriggers Warn-level logging inqueryEnd - PoolStats —
Cluster.Stats()aggregates pool statistics across all nodes (stats.go) - Tracer passthrough —
NodeConfig.Tracer/WithTracersetspgx.QueryTracerfor OpenTelemetry - RunInTx — test helper that runs a callback in an always-rolled-back transaction (
dbxtest/tx.go)
Error classification
IsRetryable(err)— connection errors (class 08), serialization failures (40001), deadlocks (40P01), too_many_connections (53300)IsConnectionError(err)— PG class 08 + string matching for pgx-wrapped errorsIsConstraintViolation(err)— PG class 23PgErrorCode(err)— extract raw code from*pgconn.PgError
Conventions
- Struct-based
Configwithdefaults()method (not functional options for NewCluster constructor, butOptiontype exists forApplyOptionsin tests) - Functional options (
Option func(*Config)) used viaApplyOptions(e.g., in dbxtest) - stdlib-only tests — no testify, no gomock
atomic.Boolfor thread safety (Node.healthy,Cluster.closed)dbxtest.NewTestClusterskips tests when DB unreachable, auto-closes viat.Cleanupdbxtest.TestLoggerwrites totesting.Tfor test log outputdbxtest.RunInTxruns a callback in a transaction that is always rolled back
See also
AGENTS.md— universal AI agent guide with common tasks, gotchas, and ASCII diagrams