storage

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2025 License: Apache-2.0 Imports: 11 Imported by: 0

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

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 NewBase

func NewBase(name string, typ Type) *Base

NewBase 创建基类

func (*Base) CompareAndSwapState

func (b *Base) CompareAndSwapState(old, new State) bool

CompareAndSwapState CAS 状态

func (*Base) Name

func (b *Base) Name() string

Name 返回存储名称

func (*Base) SetState

func (b *Base) SetState(s State)

SetState 设置状态

func (*Base) State

func (b *Base) State() State

State 返回连接状态

func (*Base) Type

func (b *Base) Type() Type

Type 返回存储类型

type Builder

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

Builder 存储构建器

func NewBuilder

func NewBuilder(s Storage) *Builder

NewBuilder 创建构建器

func (*Builder) Build

func (b *Builder) Build() Storage

Build 构建最终存储(装饰器顺序:Reconnectable -> Traced -> Metriced)

func (*Builder) WithMetrics

func (b *Builder) WithMetrics(m *Metrics) *Builder

WithMetrics 启用指标

func (*Builder) WithReconnect

func (b *Builder) WithReconnect(cfg ReconnectConfig) *Builder

WithReconnect 启用自动重连

func (*Builder) WithTracing

func (b *Builder) WithTracing(tracer trace.Tracer) *Builder

WithTracing 启用追踪

type Collector

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

Collector 连接池指标收集器

func NewCollector

func NewCollector(metrics *PoolMetrics, interval time.Duration) *Collector

NewCollector 创建收集器

func (*Collector) Register

func (c *Collector) Register(s Storage)

Register 注册存储

func (*Collector) Start

func (c *Collector) Start(ctx context.Context)

Start 启动收集

func (*Collector) Stop

func (c *Collector) Stop()

Stop 停止收集

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 基础配置

func DefaultConfig

func DefaultConfig() Config

DefaultConfig 默认配置

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 存储管理器接口

func NewManager

func NewManager() Manager

NewManager 创建存储管理器

type Metriced

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

Metriced 带指标的存储装饰器

func NewMetriced

func NewMetriced(s Storage, m *Metrics) *Metriced

NewMetriced 创建带指标的存储

func (*Metriced) Close

func (m *Metriced) Close(ctx context.Context) error

func (*Metriced) Connect

func (m *Metriced) Connect(ctx context.Context) error

func (*Metriced) Name

func (m *Metriced) Name() string

func (*Metriced) Ping

func (m *Metriced) Ping(ctx context.Context) error

func (*Metriced) State

func (m *Metriced) State() State

func (*Metriced) Type

func (m *Metriced) Type() Type

func (*Metriced) Unwrap

func (m *Metriced) Unwrap() Storage

type Metrics

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

Metrics 存储操作指标 (用于装饰器)

func NewMetrics

func NewMetrics(mp observability.MetricsProvider) *Metrics

NewMetrics 创建存储操作指标

type PoolMetrics

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

PoolMetrics 连接池指标

func NewPoolMetrics

func NewPoolMetrics(mp observability.MetricsProvider, subsystem string) *PoolMetrics

NewPoolMetrics 创建连接池指标

func (*PoolMetrics) Record

func (m *PoolMetrics) Record(name string, typ Type, stats Stats)

Record 记录连接池统计

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) Close

func (r *Reconnectable) Close(ctx context.Context) error

func (*Reconnectable) Connect

func (r *Reconnectable) Connect(ctx context.Context) error

func (*Reconnectable) Name

func (r *Reconnectable) Name() string

func (*Reconnectable) Ping

func (r *Reconnectable) Ping(ctx context.Context) error

func (*Reconnectable) State

func (r *Reconnectable) State() State

func (*Reconnectable) Type

func (r *Reconnectable) Type() Type

func (*Reconnectable) Unwrap

func (r *Reconnectable) Unwrap() Storage

Unwrap 获取底层存储

type State

type State int32

State 连接状态

const (
	StateDisconnected State = iota
	StateConnecting
	StateConnected
	StateDisconnecting
)

func (State) String

func (s State) String() string

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 StatsProvider

type StatsProvider interface {
	Stats() Stats
}

StatsProvider 提供连接池统计的存储

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 存储接口

func Unwrap

func Unwrap(s Storage) Storage

Unwrap 解包装饰器获取原始存储

type Traced

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

Traced 追踪装饰器

func NewTraced

func NewTraced(s Storage, tracer trace.Tracer) *Traced

NewTraced 创建追踪装饰器

func (*Traced) Close

func (t *Traced) Close(ctx context.Context) error

func (*Traced) Connect

func (t *Traced) Connect(ctx context.Context) error

func (*Traced) Name

func (t *Traced) Name() string

func (*Traced) Ping

func (t *Traced) Ping(ctx context.Context) error

func (*Traced) State

func (t *Traced) State() State

func (*Traced) Type

func (t *Traced) Type() Type

func (*Traced) Unwrap

func (t *Traced) Unwrap() Storage

Unwrap 获取底层存储

type Type

type Type string

Type 存储类型

const (
	TypeMySQL         Type = "mysql"
	TypeRedis         Type = "redis"
	TypeMongoDB       Type = "mongodb"
	TypeClickHouse    Type = "clickhouse"
	TypeElasticsearch Type = "elasticsearch"
	TypePostgreSQL    Type = "postgresql"
	TypeUnknown       Type = "unknown"
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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