2019-08-03 17:21:12 +03:00
|
|
|
package redis
|
2016-07-02 15:52:10 +03:00
|
|
|
|
|
|
|
import (
|
2019-07-30 12:13:00 +03:00
|
|
|
"context"
|
2016-07-02 15:52:10 +03:00
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"strings"
|
|
|
|
|
2018-02-22 15:14:30 +03:00
|
|
|
"github.com/go-redis/redis/internal/proto"
|
|
|
|
)
|
2016-07-02 15:52:10 +03:00
|
|
|
|
2019-08-03 17:21:12 +03:00
|
|
|
func isRetryableError(err error, retryTimeout bool) bool {
|
2019-07-30 12:13:00 +03:00
|
|
|
switch err {
|
2019-08-08 10:36:13 +03:00
|
|
|
case nil, context.Canceled, context.DeadlineExceeded:
|
2018-10-14 11:27:34 +03:00
|
|
|
return false
|
2019-07-30 12:13:00 +03:00
|
|
|
case io.EOF:
|
2018-08-23 16:13:42 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
if netErr, ok := err.(net.Error); ok {
|
|
|
|
if netErr.Timeout() {
|
|
|
|
return retryTimeout
|
|
|
|
}
|
|
|
|
return true
|
2017-08-31 15:22:47 +03:00
|
|
|
}
|
2019-08-02 00:59:53 +03:00
|
|
|
|
2017-08-31 15:22:47 +03:00
|
|
|
s := err.Error()
|
|
|
|
if s == "ERR max number of clients reached" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(s, "LOADING ") {
|
|
|
|
return true
|
|
|
|
}
|
2018-06-18 13:07:31 +03:00
|
|
|
if strings.HasPrefix(s, "READONLY ") {
|
|
|
|
return true
|
|
|
|
}
|
2017-08-31 15:22:47 +03:00
|
|
|
if strings.HasPrefix(s, "CLUSTERDOWN ") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
2016-07-02 15:52:10 +03:00
|
|
|
}
|
|
|
|
|
2019-08-03 17:21:12 +03:00
|
|
|
func isRedisError(err error) bool {
|
2018-02-22 15:14:30 +03:00
|
|
|
_, ok := err.(proto.RedisError)
|
2016-07-02 15:52:10 +03:00
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2019-08-03 17:21:12 +03:00
|
|
|
func isBadConn(err error, allowTimeout bool) bool {
|
2019-08-08 10:43:10 +03:00
|
|
|
if err == nil {
|
2016-07-02 15:52:10 +03:00
|
|
|
return false
|
|
|
|
}
|
2019-08-03 17:21:12 +03:00
|
|
|
if isRedisError(err) {
|
2019-08-08 10:43:10 +03:00
|
|
|
// Close connections in read only state in case domain addr is used
|
|
|
|
// and domain resolves to a different Redis Server. See #790.
|
|
|
|
return isReadOnlyError(err)
|
2016-07-02 15:52:10 +03:00
|
|
|
}
|
|
|
|
if allowTimeout {
|
|
|
|
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-08-03 17:21:12 +03:00
|
|
|
func isMovedError(err error) (moved bool, ask bool, addr string) {
|
|
|
|
if !isRedisError(err) {
|
2016-07-02 15:52:10 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s := err.Error()
|
2019-07-25 13:53:00 +03:00
|
|
|
switch {
|
|
|
|
case strings.HasPrefix(s, "MOVED "):
|
2016-07-02 15:52:10 +03:00
|
|
|
moved = true
|
2019-07-25 13:53:00 +03:00
|
|
|
case strings.HasPrefix(s, "ASK "):
|
2016-07-02 15:52:10 +03:00
|
|
|
ask = true
|
2019-07-25 13:53:00 +03:00
|
|
|
default:
|
2016-07-02 15:52:10 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-09-14 14:00:34 +03:00
|
|
|
ind := strings.LastIndex(s, " ")
|
2016-07-02 15:52:10 +03:00
|
|
|
if ind == -1 {
|
|
|
|
return false, false, ""
|
|
|
|
}
|
|
|
|
addr = s[ind+1:]
|
|
|
|
return
|
|
|
|
}
|
2016-10-09 06:30:01 +03:00
|
|
|
|
2019-08-03 17:21:12 +03:00
|
|
|
func isLoadingError(err error) bool {
|
2017-08-15 10:12:43 +03:00
|
|
|
return strings.HasPrefix(err.Error(), "LOADING ")
|
2016-10-09 06:30:01 +03:00
|
|
|
}
|
2019-02-27 13:53:44 +03:00
|
|
|
|
2019-08-03 17:21:12 +03:00
|
|
|
func isReadOnlyError(err error) bool {
|
2019-02-27 13:53:44 +03:00
|
|
|
return strings.HasPrefix(err.Error(), "READONLY ")
|
|
|
|
}
|