gogrpcpool

package module
v0.1.2-alpha Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

README

go-grpc-pool

go grpc ClientConn connection pool, supports connection preparation, connection number control, and connection reuse

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrConnTooManyReference = errors.New("connection too many reference")
	ErrConnIsClosing        = errors.New("connection is closing")
	ErrTargetNotAvailable   = errors.New("target not available")
	ErrNoConnAvailable      = errors.New("no connection available")
	ErrWaitConnReadyTimeout = errors.New("wait connection ready timeout")
)

Functions

This section is empty.

Types

type Conn

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

grpc 客户端连接管理

1. 实例化时设置 ref 为0,每次引用时加一,释放时减一 2. 连接被建立时,需要启动一个goroutine来运行 run() 方法,该方法会实时检测连接的状态,来实时将可用状态下的连接推入到 readyTunnel 中 3. 当 closing = true 时,连接将不允许再被引用,也就不能够再推到 readyTunnel 中,也意味着 ref 的值不会再增加 4. 当 closing = true 且 ref为0 时, 在Pool中会被 idleConnManager 关闭和删除 5. 当 ref >= refMax 时,连接也将不能够再被引用,也就不能够再推到 readyTunnel 中,但是 run 方法会每隔1ms进行一次ref检测,当ref < refMax 时,连接将再次被推入到 readyTunnel 中

func (*Conn) Describe

func (c *Conn) Describe() string

描述信息

func (*Conn) Refer

func (c *Conn) Refer() *grpc.ClientConn

引用grpc客户端连接

type Options

type Options struct {
	Debug            bool              // 开启调试模式之后,会在运行时打印连接使用情况的统计信息
	DescribeDuration time.Duration     // 连接使用情况的打印周期
	CheckPeriod      time.Duration     // 定时清理多出连接的周期
	ConnTimeOut      time.Duration     // 新建连接的超时时间
	ConnBlock        bool              // 初始化连接建立时候是否使用阻塞模式,仅在第一次 初始化空闲连接时候进行阻塞
	Target           string            // grpc 地址
	Dopts            []grpc.DialOption // grpc 拨号选项
	MaxConns         int32             // 最大连接数, -1 = unlimited
	MaxIdleConns     int32             // 最大空闲连接数, min = 1
	MaxRefs          int32             // 每个连接的最大可同时引用的次数
	NewConnRate      int32             // 新连接建立所遵循的指标, 结合 MaxRefs 来确定是否需要建立新连接,当已建立的连接的引用的总次数占它们总的最大可引用次数的 1/NewConnRate 时会尝试建立新的连接;
}

func (*Options) Dial

func (o *Options) Dial(tunnel chan<- *Conn, block bool) (*Conn, error)

type Pool

type Pool struct {
	sync.RWMutex // lock
	// contains filtered or unexported fields
}

连接池 1. 一定要时刻保证连接池约靠后的部分引用数是越小的

func NewPool

func NewPool(opts Options) *Pool

实例化连接池

func (*Pool) Acquire

func (p *Pool) Acquire(d time.Duration) (*Conn, error)

寻求一个可用的连接

func (*Pool) Close

func (p *Pool) Close()

关闭连接

func (*Pool) Describe

func (p *Pool) Describe() string

输出连接池状态

func (*Pool) DescribeTimer

func (p *Pool) DescribeTimer()

debug 打印

func (*Pool) Release

func (p *Pool) Release(conn *Conn)

释放一个连接的引用数 1. 需要在 Acquire 之后,在 defer 中执行,避免忘记执行 2. 当连接的引用数为0时,说明连接处于空闲状态,对空闲连接数加一

func (*Pool) Run

func (p *Pool) Run()

启动

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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