All checks were successful
CI / test (push) Successful in 51s
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2.2 KiB
2.2 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
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 output
See also
AGENTS.md— universal AI agent guide with common tasks, gotchas, and ASCII diagrams