database

package
v0.9.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 5, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

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

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) NumShards

func (s *ShardSet) NumShards() int

NumShards returns the number of open shards.

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

func (s *ShardSet) Open(ctx context.Context, cfg Config) error

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

func (s *ShardSet) SetMaxIdleConns(n int)

SetMaxIdleConns applies the given idle-connection pool size to every open shard.

func (*ShardSet) SetMaxOpenConns

func (s *ShardSet) SetMaxOpenConns(n int)

SetMaxOpenConns applies the given open-connection pool ceiling to every open shard.

func (*ShardSet) Shard

func (s *ShardSet) Shard(n int) (*sequel.DB, error)

Shard returns the database connection for the given 1-based shard index.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL