database

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2026 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const SUBSYTEM = "database"

Variables

View Source
var (
	ErrNoHealthyConn            = errors.New("no healthy mysql connection available")
	ErrUnsupportedBalancingMode = errors.New("unsupported balancing mode")
)

Functions

func GetMysqlDsn

func GetMysqlDsn(connection *MysqlConnectionOptions, multiStatements bool) string

func IsTx

func IsTx(executor boil.ContextExecutor) bool

IsTx checks whether the given executor is a transaction

func Migrate

func Migrate(conn *MysqlConnectionOptions, config *MigrationConfig) error

func MustCommit

func MustCommit(tx *sql.Tx)

MustCommit commits the given transaction or panics if an error occurs

func MustRollbackTx

func MustRollbackTx(tx *sql.Tx)

MustRollbackTx rolls back the given transaction or panics if an error occurs

Types

type BalancingMode

type BalancingMode string
const (
	Random  BalancingMode = "random"  // the connection pool will return a random database node each time
	Ordered BalancingMode = "ordered" // the connection pool will always return the first available database node
)

type ClusterConfig

type ClusterConfig struct {
	IsGaleraCluster *bool               `yaml:"is_galera_cluster" json:"is_galera_cluster" mapstructure:"is_galera_cluster" validate:"required"`
	BalancingMode   BalancingMode       `yaml:"balancing_mode" json:"balancing_mode" mapstructure:"balancing_mode" validate:"required,oneof=random ordered"`
	User            string              `yaml:"user" json:"user" mapstructure:"user" validate:"required"`
	Password        string              `yaml:"password" json:"password" mapstructure:"password" validate:"required"`
	Database        string              `yaml:"database" json:"database" mapstructure:"database" validate:"required"`
	Connections     []*ConnectionConfig `yaml:"connections" json:"connections" mapstructure:"connections" validate:"gte=1,dive,required"`
}

type ConnectionConfig

type ConnectionConfig struct {
	Host string `yaml:"host" json:"host" mapstructure:"host" validate:"required"`
	Port int    `yaml:"port" json:"port" mapstructure:"port" validate:"required"`
}

type MigrationConfig

type MigrationConfig struct {
	MigrationDir string `yaml:"migration_dir" json:"migration_dir" mapstructure:"migration_dir" validate:"required"` // Directory for SQL migration files
}

type MysqlConnection

type MysqlConnection struct {
	Name   string
	Dsn    string
	Config *MysqlConnectionOptions
	// DB and IsActive are written by the health-check goroutine and read concurrently
	// by request goroutines via GetConnection/getActive, so both are accessed atomically
	// rather than under Mutex to stay race-free without holding a lock across network I/O.
	DB       atomic.Pointer[sql.DB]
	IsActive atomic.Bool
}

type MysqlConnectionOptions

type MysqlConnectionOptions struct {
	User     string
	Password string
	Database string
	Host     string
	Port     int
}

type MysqlConnectionPool

type MysqlConnectionPool struct {
	Connections []*MysqlConnection
	Mutex       *sync.Mutex
	PingsTicker *time.Ticker
	PingsDone   chan bool
	Config      *ClusterConfig
}

func NewMysqlConnectionPool

func NewMysqlConnectionPool(config *ClusterConfig) (*MysqlConnectionPool, error)

NewMysqlConnectionPool builds a connection pool for the configured cluster and starts a background goroutine that periodically re-checks node health.

If no node is reachable at startup it returns a non-nil pool together with ErrNoHealthyConn: this is a recoverable condition, not a fatal one. The background pinger keeps running and will mark nodes active again once they come back, so callers that want degraded/emergency operation can keep using the returned pool. Because the pinger owns a ticker and a goroutine, the caller must call Close() on the returned pool even when it received ErrNoHealthyConn.

func (*MysqlConnectionPool) BeginTx

func (m *MysqlConnectionPool) BeginTx() (*sql.Tx, error)

BeginTx starts a database transaction

func (*MysqlConnectionPool) Close

func (m *MysqlConnectionPool) Close()

Close closes all database connections from the pool

func (*MysqlConnectionPool) GetActiveConfig

func (m *MysqlConnectionPool) GetActiveConfig() (*MysqlConnectionOptions, error)

func (*MysqlConnectionPool) GetConnection

func (m *MysqlConnectionPool) GetConnection() (*sql.DB, error)

GetConnection returns an active connection or ErrNoHealthyConn if none of the connections from the pool is healthy

func (*MysqlConnectionPool) MustBeginTx

func (m *MysqlConnectionPool) MustBeginTx() *sql.Tx

MustBeginTx starts a database transaction or panics if an error occurs

func (*MysqlConnectionPool) MustGetConnection

func (m *MysqlConnectionPool) MustGetConnection() *sql.DB

MustGetConnection returns an active connection of panics if none of the connections from the pool is healthy

Jump to

Keyboard shortcuts

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