connection

package
v0.2.7 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2026 License: Apache-2.0 Imports: 9 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoAvailableSlaves = errors.New("no available read replica databases")
	ErrInvalidStrategy   = errors.New("invalid load balance strategy")
)

Functions

func SetDriverRegistry added in v0.2.0

func SetDriverRegistry(registry DriverRegistry)

SetDriverRegistry is called by the data package to inject the registry.

Types

type CacheDriver added in v0.2.0

type CacheDriver interface {
	Name() string
	Connect(ctx context.Context, cfg any) (any, error)
	Close(conn any) error
	Ping(ctx context.Context, conn any) error
}

type Connections

type Connections struct {
	DBM *DBManager
	RC  any
	MS  any
	ES  any
	OS  any
	MGM any
	Neo any
	RMQ any
	KFK any
	// contains filtered or unexported fields
}

func New

func New(conf *config.Config) (*Connections, error)

func (*Connections) Close

func (d *Connections) Close() (errs []error)

func (*Connections) DB

func (d *Connections) DB() *sql.DB

func (*Connections) DBRead

func (d *Connections) DBRead() (*sql.DB, error)

func (*Connections) Ping

func (d *Connections) Ping(ctx context.Context) error

func (*Connections) PingRedis added in v0.2.5

func (d *Connections) PingRedis(ctx context.Context) error

PingRedis checks the configured Redis client.

func (*Connections) ReadDB

func (d *Connections) ReadDB() (*sql.DB, error)

func (*Connections) SearchHealth added in v0.2.5

func (d *Connections) SearchHealth(ctx context.Context) map[string]error

SearchHealth checks all configured search clients and returns per-engine errors. An empty result means no search engine is configured.

type DBManager

type DBManager struct {
	// contains filtered or unexported fields
}

DBManager manages database connections for read-write splitting

func NewDBManager

func NewDBManager(conf *config.Database) (*DBManager, error)

NewDBManager creates a new database manager with read-write splitting

func (*DBManager) Close

func (dm *DBManager) Close() error

Close closes all database connections

func (*DBManager) Health

func (dm *DBManager) Health(ctx context.Context) error

Health checks the health of all database connections

func (*DBManager) Master

func (dm *DBManager) Master() *sql.DB

Master returns the master database connection

func (*DBManager) Slave

func (dm *DBManager) Slave() (*sql.DB, error)

Slave returns a slave database connection based on the load balancing strategy

func (*DBManager) SlaveContext added in v0.2.5

func (dm *DBManager) SlaveContext(ctx context.Context) (*sql.DB, error)

SlaveContext returns a read database connection. If no read replicas are configured, the primary connection is returned. If replicas are configured but unavailable, an error is returned instead of silently routing reads to primary.

type DatabaseDriver added in v0.2.0

type DatabaseDriver interface {
	Name() string
	Connect(ctx context.Context, cfg any) (any, error)
	Close(conn any) error
	Ping(ctx context.Context, conn any) error
}

DatabaseDriver mirrors the interface from data package to avoid import cycle.

type DriverRegistry added in v0.2.0

type DriverRegistry interface {
	GetDatabaseDriver(name string) (DatabaseDriver, error)
	GetCacheDriver(name string) (CacheDriver, error)
	GetSearchDriver(name string) (SearchDriver, error)
	GetMessageDriver(name string) (MessageDriver, error)
}

DriverRegistry defines the driver lookups needed from the parent data package.

type LoadBalancer

type LoadBalancer interface {
	Next([]*sql.DB) (*sql.DB, error)
}

LoadBalancer LoadBalancer interface

type MessageDriver added in v0.2.0

type MessageDriver interface {
	Name() string
	Connect(ctx context.Context, cfg any) (any, error)
	Close(conn any) error
}

type RandomBalancer

type RandomBalancer struct{}

RandomBalancer Implement random strategy

func (*RandomBalancer) Next

func (rb *RandomBalancer) Next(slaves []*sql.DB) (*sql.DB, error)

type RoundRobinBalancer

type RoundRobinBalancer struct {
	// contains filtered or unexported fields
}

RoundRobinBalancer Implement polling strategy

func NewRoundRobinBalancer

func NewRoundRobinBalancer() *RoundRobinBalancer

NewRoundRobinBalancer Create new RoundRobinBalancer

func (*RoundRobinBalancer) Next

func (rb *RoundRobinBalancer) Next(slaves []*sql.DB) (*sql.DB, error)

type SearchDriver added in v0.2.0

type SearchDriver interface {
	Name() string
	Connect(ctx context.Context, cfg any) (any, error)
	Close(conn any) error
}

type WeightBalancer

type WeightBalancer struct {
	// contains filtered or unexported fields
}

WeightBalancer Implement weight strategy

func NewWeightBalancer

func NewWeightBalancer(slaves []*config.DBNode) *WeightBalancer

func (*WeightBalancer) Next

func (wb *WeightBalancer) Next(slaves []*sql.DB) (*sql.DB, error)

Jump to

Keyboard shortcuts

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