Help us shape the future of Go! Take the 2026 Go Developer Survey

database

package
v1.11.3 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type PoolConfig

type PoolConfig struct {
	// 最大空闲连接数
	MaxIdleConns int `yaml:"max_idle_conns" json:"max_idle_conns"`

	// 最大打开连接数
	MaxOpenConns int `yaml:"max_open_conns" json:"max_open_conns"`

	// 连接最大生命周期
	ConnMaxLifetime time.Duration `yaml:"conn_max_lifetime" json:"conn_max_lifetime"`

	// 连接最大空闲时间
	ConnMaxIdleTime time.Duration `yaml:"conn_max_idle_time" json:"conn_max_idle_time"`

	// 健康检查间隔
	HealthCheckInterval time.Duration `yaml:"health_check_interval" json:"health_check_interval"`
}

PoolConfig 连接池配置

func DefaultPoolConfig

func DefaultPoolConfig() PoolConfig

DefaultPoolConfig 返回默认连接池配置

type PoolHealth added in v1.6.3

type PoolHealth int

PoolHealth represents the health state of the database pool.

const (
	PoolHealthy PoolHealth = iota
	PoolDegraded
	PoolUnhealthy
)

type PoolManager

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

PoolManager 数据库连接池管理器

func NewPoolManager

func NewPoolManager(db *gorm.DB, config PoolConfig, logger *zap.Logger) (*PoolManager, error)

NewPoolManager 创建连接池管理器

func (*PoolManager) Close

func (pm *PoolManager) Close() error

Close 关闭连接池

func (*PoolManager) DB

func (pm *PoolManager) DB() *gorm.DB

DB 返回 GORM 数据库实例

func (*PoolManager) GetStats

func (pm *PoolManager) GetStats() PoolStats

GetStats 获取友好格式的统计信息

func (*PoolManager) Health added in v1.6.3

func (pm *PoolManager) Health() PoolHealth

Health returns the current pool health state.

func (*PoolManager) IsHealthy added in v1.6.3

func (pm *PoolManager) IsHealthy() bool

IsHealthy returns true when the pool is in Healthy state.

func (*PoolManager) Ping

func (pm *PoolManager) Ping(ctx context.Context) error

Ping 检查数据库连接

func (*PoolManager) Stats

func (pm *PoolManager) Stats() sql.DBStats

Stats 返回连接池统计信息

func (*PoolManager) WithTransaction

func (pm *PoolManager) WithTransaction(ctx context.Context, fn TransactionFunc) error

WithTransaction 在事务中执行函数。 推荐:所有涉及多步写操作(如 Read-Modify-Write、批量更新)的业务逻辑应使用此方法包裹,以保证原子性与一致性。

func (*PoolManager) WithTransactionRetry

func (pm *PoolManager) WithTransactionRetry(ctx context.Context, maxRetries int, fn TransactionFunc) error

WithTransactionRetry 在事务中执行函数(带重试)

type PoolStats

type PoolStats struct {
	MaxOpenConnections int           `json:"max_open_connections"`
	OpenConnections    int           `json:"open_connections"`
	InUse              int           `json:"in_use"`
	Idle               int           `json:"idle"`
	WaitCount          int64         `json:"wait_count"`
	WaitDuration       time.Duration `json:"wait_duration"`
	MaxIdleClosed      int64         `json:"max_idle_closed"`
	MaxLifetimeClosed  int64         `json:"max_lifetime_closed"`
}

PoolStats 连接池统计信息(更友好的格式)

type TransactionFunc

type TransactionFunc func(tx *gorm.DB) error

TransactionFunc 事务函数类型

Jump to

Keyboard shortcuts

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