Documentation
¶
Overview ¶
Package database owns the engine's sharded SQL connections: it opens and migrates every shard, routes by 1-based shard index, fans an operation out over all shards, and closes them. It is DB-lifecycle-complete (open → migrate → size → fan-out → close) and speaks only in resolved connection-pool sizes - the sizing *policy* (how many connections a worker count implies) stays with the caller, which hands ShardSet the two computed integers via Config / SetMax*Conns.
Index ¶
- type Config
- type ShardSet
- func (s *ShardSet) Close()
- func (s *ShardSet) NumShards() int
- func (s *ShardSet) OnEach(ctx context.Context, ...) error
- func (s *ShardSet) Open(ctx context.Context, cfg Config) error
- func (s *ShardSet) SetMaxIdleConns(n int)
- func (s *ShardSet) SetMaxOpenConns(n int)
- func (s *ShardSet) Shard(n int) (*sequel.DB, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// DSN is the sequel data source name; "%d" is substituted with the 1-based shard index. In test mode
// (TestID set) an empty DSN falls back to SEQUEL_TESTING_DSN, then to the SQLite in-memory default.
DSN string
NumShards int
// TestID, when non-empty, wraps each shard via sequel.CreateTestingDatabase into an isolated, auto-dropped
// database keyed on (driver, baseDSN, TestID) - the base DSN's %d already distinguishes the shards.
TestID string
Logger *slog.Logger
TracerProvider trace.TracerProvider // nil → otel.GetTracerProvider()
MeterProvider metric.MeterProvider // nil → otel.GetMeterProvider()
// MaxIdleConns / MaxOpenConns are the resolved per-shard pool sizes, computed by the caller and applied
// verbatim. The connection-sizing formula is not this package's concern.
MaxIdleConns int
MaxOpenConns int
}
Config is the full open-time configuration of a ShardSet. Field names mirror the corresponding sequel.DB / *sql.DB setters (Logger→SetLogger, TracerProvider→SetTracerProvider, MeterProvider→SetMeterProvider, MaxIdleConns→SetMaxIdleConns, MaxOpenConns→SetMaxOpenConns), so the mapping from field to setter is 1:1.
type ShardSet ¶
type ShardSet struct {
// contains filtered or unexported fields
}
ShardSet is the engine's set of open, migrated database shards. The zero value is a usable, empty set. The shard count is fixed for the set's life (established at Open), so a caller may size per-shard state from NumShards() and index it by the shard arg without racing a concurrent change.
func (*ShardSet) Close ¶
func (s *ShardSet) Close()
Close closes all shard connections and empties the set.
func (*ShardSet) OnEach ¶
func (s *ShardSet) OnEach(ctx context.Context, op func(ctx context.Context, db *sequel.DB, shard int) error) error
OnEach fans op out over every shard concurrently using an errgroup-style wait, returning the first error.
func (*ShardSet) Open ¶
Open opens and migrates every shard, applying cfg's pool sizes and telemetry. On any shard failure it closes the shards already opened this attempt (so a partial failure leaks no connections and leaves the set empty for a clean retry) and returns the error. Not safe to call concurrently with itself; call once at startup.
func (*ShardSet) SetMaxIdleConns ¶
SetMaxIdleConns applies the given idle-connection pool size to every open shard.
func (*ShardSet) SetMaxOpenConns ¶
SetMaxOpenConns applies the given open-connection pool ceiling to every open shard.