connpool

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrPoolClosed = errors.New("connection pool is closed")
	ErrTimeout    = errors.New("connection pool: get connection timed out")
)

Functions

This section is empty.

Types

type AdaptiveOptions

type AdaptiveOptions struct {
	// 基础连接池选项
	BaseOptions Options

	// 健康检查相关
	HealthCheckInterval time.Duration // 健康检查间隔
	HealthCheckTimeout  time.Duration // 健康检查超时

	// 自适应调整相关
	EnableAdaptive      bool          // 是否启用自适应调整
	LoadCheckInterval   time.Duration // 负载检查间隔
	HighLoadThreshold   float64       // 高负载阈值 (0.0-1.0)
	LowLoadThreshold    float64       // 低负载阈值 (0.0-1.0)
	ScaleUpFactor       float64       // 扩容因子 (如1.2表示增加20%)
	ScaleDownFactor     float64       // 缩容因子 (如0.8表示减少20%)
	MinConnections      int           // 最小连接数
	MaxConnections      int           // 最大连接数
	ScaleCooldownPeriod time.Duration // 两次缩放操作之间的冷却期
}

AdaptiveOptions 自适应连接池选项

func DefaultAdaptiveOptions

func DefaultAdaptiveOptions() AdaptiveOptions

DefaultAdaptiveOptions 返回默认的自适应连接池选项

type AdaptivePool

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

AdaptivePool 自适应连接池实现

func (*AdaptivePool) Close

func (ap *AdaptivePool) Close() error

Close 关闭连接池

func (*AdaptivePool) Get

func (ap *AdaptivePool) Get(ctx context.Context) (*PoolConn, error)

Get 从连接池获取一个连接

func (*AdaptivePool) GetExtendedStatus

func (ap *AdaptivePool) GetExtendedStatus() AdaptiveStatus

GetExtendedStatus 返回扩展的连接池状态信息

func (*AdaptivePool) Preconnect

func (ap *AdaptivePool) Preconnect(ctx context.Context, count int) error

预热连接池,创建一定数量的初始连接

func (*AdaptivePool) Put

func (ap *AdaptivePool) Put(conn *PoolConn) error

Put 归还一个连接到池中

func (*AdaptivePool) Status

func (ap *AdaptivePool) Status() Status

Status 返回连接池状态

type AdaptiveStatus

type AdaptiveStatus struct {
	Status          Status
	SuccessCount    uint64
	FailureCount    uint64
	TimeoutCount    uint64
	HealthFailures  uint64
	ScaleUpEvents   uint32
	ScaleDownEvents uint32
}

AdaptiveStatus 扩展的连接池状态

type ConnectionFactory

type ConnectionFactory func(ctx context.Context, target string) (*grpc.ClientConn, error)

ConnectionFactory 用于创建新的连接

type Options

type Options struct {
	// 最大空闲连接数
	MaxIdle int
	// 最大活跃连接数
	MaxActive int
	// 连接最大空闲时间,超过这个时间将被关闭
	IdleTimeout time.Duration
	// 获取连接的最大等待时间
	WaitTimeout time.Duration
	// 是否在连接池满时阻塞等待
	Wait bool
}

Options 配置连接池参数

type Pool

type Pool interface {
	// Get 获取一个连接
	Get(ctx context.Context) (*PoolConn, error)
	// Put 归还一个连接
	Put(conn *PoolConn) error
	// Close 关闭连接池
	Close() error
	// Status 返回连接池状态信息
	Status() Status
}

Pool 连接池接口

func NewAdaptivePool

func NewAdaptivePool(target string, factory ConnectionFactory, opts AdaptiveOptions) Pool

NewAdaptivePool 创建新的自适应连接池

func NewPool

func NewPool(target string, factory ConnectionFactory, opts Options) Pool

NewPool 创建新的连接池

type PoolConn

type PoolConn struct {
	*grpc.ClientConn
	// contains filtered or unexported fields
}

PoolConn 包装grpc.ClientConn,添加连接池管理相关信息

func (*PoolConn) Close

func (pc *PoolConn) Close() error

Close 关闭连接或将连接归还到池中

type Status

type Status struct {
	// 活跃连接数
	ActiveCount int
	// 空闲连接数
	IdleCount int
	// 等待获取连接的请求数
	WaitCount int64
	// 连接池最大连接数
	MaxActive int
}

Status 连接池状态

Jump to

Keyboard shortcuts

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