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 中 6. 仅当 ref <= 0 且 lastReferAt 在 closeWait 之前,才能将 closing 设置为 true
type Options ¶
type Options struct { Debug bool // 开启调试模式之后,会在运行时打印连接使用情况的统计信息 DescribeDuration time.Duration // 连接使用情况的打印周期 CheckPeriod time.Duration // 定时清理多出连接的周期 CloseWait time.Duration // 关闭等待周期, 即:当最后一次引用时间距离当前时间超过 closeWait 时,连接可以被关闭 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 时会尝试建立新的连接; }
Source Files
¶
Click to show internal directories.
Click to hide internal directories.