redis.NewFailoverClient的ReadTimeout配置为何未生效?
问题:go-redis Sentinel模式下ReadTimeout未触发超时错误
预期行为
当读取操作耗时超过ReadTimeout设定值时,客户端应当返回超时错误。
当前行为
读取操作实际耗时已远超配置的ReadTimeout,但并未返回任何错误。
复现步骤
- 配置
ReadTimeout参数连接Redis Sentinel,值越小越容易复现,示例中设置为30微秒。 - 执行一个耗时超过该
ReadTimeout的读取操作,观察结果。
复现代码
package main import ( "fmt" "github.com/go-redis/redis" "time" ) func main() { master := "mymaster" sentinelServers := []string{"localhost:26379"} password := "" poolSize := 9 db := 11 client := redis.NewFailoverClient(&redis.FailoverOptions{ MasterName: master, SentinelAddrs: sentinelServers, Password: password, PoolSize: poolSize, DB: db, ReadTimeout: time.Duration(30) * time.Microsecond, }) res, err := client.Ping().Result() if err != nil { fmt.Printf("Ping error %s\n", err) } fmt.Println(res) key := "key2" value := "val2" err = client.Set(key, value, time.Duration(30)*time.Second).Err() if err != nil { fmt.Printf("Writing error %s\n", err) } start := time.Now() val, err := client.Get(key).Result() if err != nil { fmt.Printf("Reading error %s\n", err) return } elapsed := time.Since(start) fmt.Printf("Cache read in %s\n", elapsed) if err != nil { fmt.Println(err) } fmt.Println(val) }
排查建议
- 检查
FailoverOptions中是否有其他超时配置(如SentinelTimeout)干扰ReadTimeout生效,部分场景下Sentinel相关超时配置可能会覆盖主连接的读写超时。 - 验证连接池复用逻辑:如果连接是从池中复用的,确认
ReadTimeout是否会应用到复用连接的命令执行阶段,而非仅在连接建立时生效。 - 尝试用普通
Client而非FailoverClient测试相同ReadTimeout配置,确认是否是Sentinel模式特有的问题。
内容的提问来源于stack exchange,提问作者Akalanka Weerasooriya
相关产品推荐
相关产品推荐

