The dial functions use the single variable v
. The functions use the last value assigned to v
when called.
To fix the problem, declare a variable inside the scope of the for loop and use that variable in the dial function:
for k, v := range connections {
v := v // Declare variable scoped inside for loop
redisPool := &redis.Pool{
Dial: func() (redis.Conn, error) {
return redis.Dial("tcp", v) // uses variable scoped inside for loop
},
...
Separate from this issue, the pool settings don't make sense. Because the pool is configured to discard connections that have been idle for 10 seconds, the test on borrow function never pings the server.