type Pool struct {
// Dial is an application supplied function for creating and configuring a
// connection.
//
// The connection returned from Dial must not be in a special state
// (subscribed to pubsub channel, transaction started, ...).
Dial func() (Conn, error)
// TestOnBorrow is an optional application supplied function for checking
// the health of an idle connection before the connection is used again by
// the application. Argument t is the time that the connection was returned
// to the pool. If the function returns an error, then the connection is
// closed.
TestOnBorrow func(c Conn, t time.Time) error
// Maximum number of idle connections in the pool.
MaxIdle int
// Maximum number of connections allocated by the pool at a given time.
// When zero, there is no limit on the number of connections in the pool.
MaxActive int
// Close connections after remaining idle for this duration. If the value
// is zero, then idle connections are not closed. Applications should set
// the timeout to a value less than the server's timeout.
IdleTimeout time.Duration
// If Wait is true and the pool is at the MaxActive limit, then Get() waits
// for a connection to be returned to the pool before returning.
Wait bool
// Close connections older than this duration. If the value is zero, then
// the pool does not close connections based on age.
MaxConnLifetime time.Duration
chInitialized uint32 // set to 1 when field ch is initialized
mu sync.Mutex // mu protects the following fields
closed bool // set to true when the pool is closed.
active int // the number of open connections in the pool
ch chan struct{} // limits open connections when p.Wait is true
idle idleList // idle connections
}
参数名 | 含义 |
---|---|
MaxIdle | 连接池中最大的空闲连接数 |
MaxActive | 允许的最大连接Redis的连接数,设置为0则没有限制 |
IdleTimeout | 空闲超时时间,超过此时间后,则会关闭连接。若此值设置为0,则不会关闭连接,应用应设置一个小于服务超时的值 |
Wait | 若为true ,则当连接数达到MaxActive时,使用Get()获取新的连接时将会等待,直到有连接释放连接 |
MaxConnLifetime | 最大连接生命时长,当连接存活时间超过改值,则会被关闭,若设置为0,则不会因为存活时间关闭连接 |
Dial | 用于创建和配置连接的支持方法,通常用于DB选择/连接超时时间/读超时时间/写超时时间/密码认证等的初始化 |