database

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

包 database 提供基于 GORM 的数据库连接池管理,支持健康检查、 统计信息采集与事务重试。

概述

本包通过 PoolManager 封装 GORM 与 database/sql 的连接池配置, 统一管理连接生命周期、空闲回收与最大连接数限制。后台健康检查 定时探活,异常时通过 zap 日志输出诊断信息。

核心类型

  • PoolManager:连接池管理器,持有 GORM DB 实例与底层 sql.DB, 提供 DB()、Ping()、Stats()、Close() 等生命周期方法。
  • PoolConfig:连接池配置,包含最大空闲连接数、最大打开连接数、 连接最大生命周期、空闲超时与健康检查间隔。
  • PoolStats:友好格式的连接池统计信息。
  • TransactionFunc:事务回调函数类型。

主要能力

  • 连接池调优:通过 MaxIdleConns/MaxOpenConns/ConnMaxLifetime 精细控制。
  • 健康检查:后台定时 PingContext 探活,输出连接数与空闲数。
  • 事务管理:WithTransaction 提供单次事务执行, WithTransactionRetry 支持指数退避重试(死锁、序列化失败等场景)。
  • 统计采集:GetStats 返回结构化的连接池运行指标。

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 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) 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 在事务中执行函数

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