redispool

package
v0.0.0-...-2824e21 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2023 License: MIT Imports: 4 Imported by: 0

README

Redis Connection Pool

这个包实现了Redis连接池。

特性

  • 设置最小和最大连接数
  • 连接重用,避免重复创建连接
  • 连接超时设置
  • 健康检查机制,关闭失效连接
  • 定期清理过期连接

用法

pool := redispool.New(max, min, timeout)

pool.Open()

conn, err := pool.Acquire() 

// 使用连接

pool.Release(conn)

pool.Close() 

接口

  • New 创建连接池,传入最大连接数、最小连接数和获取连接超时时间
  • Open 打开连接池,初始化连接
  • Acquire 获取一个连接
  • Release 释放使用完的连接
  • Close 关闭连接池
  • Cleaner 定期清理过期连接
  • Check 健康检查连接

实现

  • 使用channel管理连接池
  • 打开连接后放入连接池重用
  • 获取连接时优先返回已有连接
  • 定期清理过期和失效连接
  • 小于最小连接数时打开新连接

TODO

  • 添加连接池统计和指标
  • 从配置文件初始化连接池
  • 连接泄漏检测

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RedisConn

type RedisConn struct {
	Conn      *redis.Client
	HeartBeat time.Time
	TimeOut   time.Duration
}

RedisConn encapsulates the Redis connection.

type RedisConnectionPool

type RedisConnectionPool struct {
	OpenConnection func() (*RedisConn, error)
	// contains filtered or unexported fields
}

RedisConnectionPool manages a set of Redis connections.

func New

func New(maxConn, minConn int, waitTimeout time.Duration) *RedisConnectionPool

New Creates a new Redis connection pool

func (*RedisConnectionPool) Acquire

func (pool *RedisConnectionPool) Acquire() (*RedisConn, error)

Acquire Acquire a connection

func (*RedisConnectionPool) Check

func (pool *RedisConnectionPool) Check(conn *RedisConn) bool

Checker checks if the connection is available

func (*RedisConnectionPool) Cleaner

func (pool *RedisConnectionPool) Cleaner()

Cleaner Clean up expired connections while maintaining minimum number of connections

func (*RedisConnectionPool) Close

func (pool *RedisConnectionPool) Close()

Close closes the connection pool

func (*RedisConnectionPool) CloseExpiredConnections

func (pool *RedisConnectionPool) CloseExpiredConnections()

CloseExpiredConnections Close expired connections

func (*RedisConnectionPool) MaintainMinConnections

func (pool *RedisConnectionPool) MaintainMinConnections()

MaintainMinConnections maintaining minimum number of connections

func (*RedisConnectionPool) Open

func (pool *RedisConnectionPool) Open() error

Open Initialize the connection pool

func (*RedisConnectionPool) Release

func (pool *RedisConnectionPool) Release(conn *RedisConn)

Release releases connections to the pool

Jump to

Keyboard shortcuts

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