mediocregopher / radix.v2

Redis client for Go
http://godoc.org/github.com/mediocregopher/radix.v2
MIT License
433 stars 92 forks source link

Hidden trouble in newPool and NewCustom--Tolerate occasional but normal failure in creating connection pool #50

Closed zhaoyuxi closed 6 years ago

zhaoyuxi commented 7 years ago

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 }