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"
|
|
|
|
|
2020-05-21 10:16:44 +03:00
|
|
|
"github.com/go-redis/redis/v8/internal/pool"
|
2020-03-11 17:29:16 +03:00
|
|
|
"github.com/go-redis/redis/v8/internal/proto"
|
2018-02-22 15:14:30 +03:00
|
|
|
)
|
2016-07-02 15:52:10 +03:00
|
|
|
|
2020-05-21 09:24:19 +03:00
|
|
|
var ErrClosed = pool.ErrClosed
|
|
|
|
|
|
|
|
type Error interface {
|
|
|
|
error
|
|
|
|
|
|
|
|
// RedisError is a no-op function but
|
|
|
|
// serves to distinguish types that are Redis
|
|
|
|
// errors from ordinary errors: a type is a
|
|
|
|
// Redis error if it has a RedisError method.
|
|
|
|
RedisError()
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ Error = proto.RedisError("")
|
|
|
|
|
2020-07-24 14:57:12 +03:00
|
|
|
func shouldRetry(err error, retryTimeout bool) bool {
|
2019-07-30 12:13:00 +03:00
|
|
|
switch err {
|
2020-07-24 14:57:12 +03:00
|
|
|
case io.EOF, io.ErrUnexpectedEOF:
|
|
|
|
return true
|
2019-08-08 10:36:13 +03:00
|
|
|
case nil, context.Canceled, context.DeadlineExceeded:
|
2018-10-14 11:27:34 +03:00
|
|
|
return false
|
2018-08-23 16:13:42 +03:00
|
|
|
}
|
2020-07-24 14:57:12 +03:00
|
|
|
|
|
|
|
if v, ok := err.(timeoutError); ok {
|
|
|
|
if v.Timeout() {
|
2018-08-23 16:13:42 +03:00
|
|
|
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
|
|
|
|
}
|
2020-11-12 12:47:04 +03:00
|
|
|
if strings.HasPrefix(s, "TRYAGAIN ") {
|
|
|
|
return true
|
|
|
|
}
|
2020-07-24 14:57:12 +03:00
|
|
|
|
2017-08-31 15:22:47 +03:00
|
|
|
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 {
|
2020-12-06 12:05:42 +03:00
|
|
|
switch err {
|
|
|
|
case nil:
|
2016-07-02 15:52:10 +03:00
|
|
|
return false
|
2020-12-06 12:05:42 +03:00
|
|
|
case context.Canceled, context.DeadlineExceeded:
|
|
|
|
return true
|
2016-07-02 15:52:10 +03:00
|
|
|
}
|
2020-07-24 14:57:12 +03:00
|
|
|
|
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
|
|
|
}
|
2020-07-24 14:57:12 +03:00
|
|
|
|
2016-07-02 15:52:10 +03:00
|
|
|
if allowTimeout {
|
|
|
|
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
|
2020-07-24 14:57:12 +03:00
|
|
|
return !netErr.Temporary()
|
2016-07-02 15:52:10 +03:00
|
|
|
}
|
|
|
|
}
|
2020-07-24 14:57:12 +03:00
|
|
|
|
2016-07-02 15:52:10 +03:00
|
|
|
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 ")
|
|
|
|
}
|
2020-07-24 14:57:12 +03:00
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
type timeoutError interface {
|
|
|
|
Timeout() bool
|
|
|
|
}
|