I create more than 100 connections. Probably an error happens in dial process. The error probably is a normal case. But it lead to the creation of the whole pool failure. And discard all created connections.
My suggestion:
1 try times should be given. For example:
func NewCustom(network, addr string, size int, df DialFunc) (Pool, error) {
var err error
var continuousErrTimes int
pool := make([]redis.Client, 0, size)
for i := 0; i < size; i++ {
client, err = df(network, addr)
if err != nil {
continuousErrTimes++
for _, client = range pool {
client.Close()
}
if continuousErrTimes> cstTryTimes { //It should try serveral times.
pool = pool[0:]
break
}
} else {
continuousErrTimes= 0
}
pool = append(pool, client)
}
2 Tolerance case of failure. For example:
func (c Cluster) newPool(addr string, clearThrottle bool) (pool.Pool, error) {
p, err := pool.NewCustom("tcp", addr, c.o.PoolSize, df)
if err != nil {
if p.Avail() == 0 {//All failure and return error
c.poolThrottles[addr] = time.After(c.o.PoolThrottle)
return nil, err
}
return p, nil// some connections fail but some success. the pool is available. returning the pool probably is more reasonable
}
return p, err
}
I create more than 100 connections. Probably an error happens in dial process. The error probably is a normal case. But it lead to the creation of the whole pool failure. And discard all created connections.
My suggestion: 1 try times should be given. For example: func NewCustom(network, addr string, size int, df DialFunc) (Pool, error) { var err error var continuousErrTimes int pool := make([]redis.Client, 0, size) for i := 0; i < size; i++ { client, err = df(network, addr) if err != nil { continuousErrTimes++ for _, client = range pool { client.Close() } if continuousErrTimes> cstTryTimes { //It should try serveral times. pool = pool[0:] break } } else { continuousErrTimes= 0 } pool = append(pool, client) } 2 Tolerance case of failure. For example: func (c Cluster) newPool(addr string, clearThrottle bool) (pool.Pool, error) { p, err := pool.NewCustom("tcp", addr, c.o.PoolSize, df) if err != nil { if p.Avail() == 0 {//All failure and return error c.poolThrottles[addr] = time.After(c.o.PoolThrottle) return nil, err } return p, nil// some connections fail but some success. the pool is available. returning the pool probably is more reasonable } return p, err }