Documentation
¶
Overview ¶
Package storage 提供数据存储抽象层。
支持的存储类型:
- MySQL/PostgreSQL
- Redis
- MongoDB
- ClickHouse
- Elasticsearch
核心功能:
- 统一的存储接口
- 连接池管理、健康检查、重连机制
- 链路追踪和指标采集
使用示例:
mgr := storage.NewManager()
db := mysql.New("main", cfg)
mgr.Register(db)
mgr.ConnectAll(ctx)
Index ¶
- type Base
- type Builder
- type Collector
- type Config
- type HealthStatus
- type Manager
- type Metriced
- type Metrics
- type PoolMetrics
- type ReconnectConfig
- type Reconnectable
- func (r *Reconnectable) Close(ctx context.Context) error
- func (r *Reconnectable) Connect(ctx context.Context) error
- func (r *Reconnectable) Name() string
- func (r *Reconnectable) Ping(ctx context.Context) error
- func (r *Reconnectable) State() State
- func (r *Reconnectable) Type() Type
- func (r *Reconnectable) Unwrap() Storage
- type State
- type Stats
- type StatsProvider
- type Storage
- type Traced
- type Type
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Base ¶
type Base struct {
// contains filtered or unexported fields
}
Base 存储基类,提供通用功能
func (*Base) CompareAndSwapState ¶
CompareAndSwapState CAS 状态
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
Builder 存储构建器
func (*Builder) WithReconnect ¶
func (b *Builder) WithReconnect(cfg ReconnectConfig) *Builder
WithReconnect 启用自动重连
type Collector ¶
type Collector struct {
// contains filtered or unexported fields
}
Collector 连接池指标收集器
func NewCollector ¶
func NewCollector(metrics *PoolMetrics, interval time.Duration) *Collector
NewCollector 创建收集器
type Config ¶
type Config struct {
Name string `json:"name" yaml:"name"`
ConnectTimeout time.Duration `json:"connect_timeout" yaml:"connect_timeout"`
ReadTimeout time.Duration `json:"read_timeout" yaml:"read_timeout"`
WriteTimeout time.Duration `json:"write_timeout" yaml:"write_timeout"`
MaxRetries int `json:"max_retries" yaml:"max_retries"`
EnableTracing bool `json:"enable_tracing" yaml:"enable_tracing"`
EnableMetrics bool `json:"enable_metrics" yaml:"enable_metrics"`
}
Config 基础配置
type HealthStatus ¶
type HealthStatus struct {
Name string `json:"name"`
Type Type `json:"type"`
State string `json:"state"`
Healthy bool `json:"healthy"`
Latency time.Duration `json:"latency"`
Error string `json:"error,omitempty"`
Stats *Stats `json:"stats,omitempty"`
CheckedAt time.Time `json:"checked_at"`
}
HealthStatus 健康状态
type Manager ¶
type Manager interface {
// Register 注册存储
Register(storage Storage) error
// Get 获取存储
Get(name string) (Storage, bool)
// MustGet 获取存储,不存在则 panic
MustGet(name string) Storage
// GetByType 按类型获取
GetByType(typ Type) []Storage
// ConnectAll 连接所有存储
ConnectAll(ctx context.Context) error
// CloseAll 关闭所有存储
CloseAll(ctx context.Context) error
// HealthCheck 健康检查
HealthCheck(ctx context.Context) []HealthStatus
// List 列出所有存储名称
List() []string
}
Manager 存储管理器接口
type Metriced ¶
type Metriced struct {
// contains filtered or unexported fields
}
Metriced 带指标的存储装饰器
type Metrics ¶
type Metrics struct {
// contains filtered or unexported fields
}
Metrics 存储操作指标 (用于装饰器)
type PoolMetrics ¶
type PoolMetrics struct {
// contains filtered or unexported fields
}
PoolMetrics 连接池指标
func NewPoolMetrics ¶
func NewPoolMetrics(mp observability.MetricsProvider, subsystem string) *PoolMetrics
NewPoolMetrics 创建连接池指标
type ReconnectConfig ¶
type ReconnectConfig struct {
MaxRetries int // 最大重试次数
InitialInterval time.Duration // 初始重试间隔
MaxInterval time.Duration // 最大重试间隔
Multiplier float64 // 退避乘数
}
ReconnectConfig 重连配置
func DefaultReconnectConfig ¶
func DefaultReconnectConfig() ReconnectConfig
DefaultReconnectConfig 默认重连配置
type Reconnectable ¶
type Reconnectable struct {
// contains filtered or unexported fields
}
Reconnectable 可重连存储装饰器
func NewReconnectable ¶
func NewReconnectable(s Storage, cfg ReconnectConfig) *Reconnectable
NewReconnectable 创建可重连存储
func (*Reconnectable) Name ¶
func (r *Reconnectable) Name() string
func (*Reconnectable) State ¶
func (r *Reconnectable) State() State
func (*Reconnectable) Type ¶
func (r *Reconnectable) Type() Type
type Stats ¶
type Stats 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"`
}
Stats 连接池统计
type Storage ¶
type Storage interface {
// Connect 建立连接
Connect(ctx context.Context) error
// Ping 健康检查
Ping(ctx context.Context) error
// Close 关闭连接
Close(ctx context.Context) error
// Name 存储标识
Name() string
// Type 存储类型
Type() Type
// State 连接状态
State() State
}
Storage 存储接口
Source Files
¶
Click to show internal directories.
Click to hide internal directories.