micro/sync/lock/redis/pool.go

30 lines
360 B
Go
Raw Normal View History

2019-05-31 02:43:23 +03:00
package redis
import (
"sync"
"github.com/gomodule/redigo/redis"
)
type pool struct {
sync.Mutex
i int
addrs []string
}
func (p *pool) Get() redis.Conn {
for i := 0; i < 3; i++ {
p.Lock()
addr := p.addrs[p.i%len(p.addrs)]
p.i++
p.Unlock()
c, err := redis.Dial("tcp", addr)
if err != nil {
continue
}
return c
}
return nil
}