You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

error.go 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package redis
  2. import (
  3. "context"
  4. "io"
  5. "net"
  6. "strings"
  7. "github.com/go-redis/redis/v8/internal/pool"
  8. "github.com/go-redis/redis/v8/internal/proto"
  9. )
  10. // ErrClosed performs any operation on the closed client will return this error.
  11. var ErrClosed = pool.ErrClosed
  12. type Error interface {
  13. error
  14. // RedisError is a no-op function but
  15. // serves to distinguish types that are Redis
  16. // errors from ordinary errors: a type is a
  17. // Redis error if it has a RedisError method.
  18. RedisError()
  19. }
  20. var _ Error = proto.RedisError("")
  21. func shouldRetry(err error, retryTimeout bool) bool {
  22. switch err {
  23. case io.EOF, io.ErrUnexpectedEOF:
  24. return true
  25. case nil, context.Canceled, context.DeadlineExceeded:
  26. return false
  27. }
  28. if v, ok := err.(timeoutError); ok {
  29. if v.Timeout() {
  30. return retryTimeout
  31. }
  32. return true
  33. }
  34. s := err.Error()
  35. if s == "ERR max number of clients reached" {
  36. return true
  37. }
  38. if strings.HasPrefix(s, "LOADING ") {
  39. return true
  40. }
  41. if strings.HasPrefix(s, "READONLY ") {
  42. return true
  43. }
  44. if strings.HasPrefix(s, "CLUSTERDOWN ") {
  45. return true
  46. }
  47. if strings.HasPrefix(s, "TRYAGAIN ") {
  48. return true
  49. }
  50. return false
  51. }
  52. func isRedisError(err error) bool {
  53. _, ok := err.(proto.RedisError)
  54. return ok
  55. }
  56. func isBadConn(err error, allowTimeout bool) bool {
  57. switch err {
  58. case nil:
  59. return false
  60. case context.Canceled, context.DeadlineExceeded:
  61. return true
  62. }
  63. if isRedisError(err) {
  64. // Close connections in read only state in case domain addr is used
  65. // and domain resolves to a different Redis Server. See #790.
  66. return isReadOnlyError(err)
  67. }
  68. if allowTimeout {
  69. if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
  70. return !netErr.Temporary()
  71. }
  72. }
  73. return true
  74. }
  75. func isMovedError(err error) (moved bool, ask bool, addr string) {
  76. if !isRedisError(err) {
  77. return
  78. }
  79. s := err.Error()
  80. switch {
  81. case strings.HasPrefix(s, "MOVED "):
  82. moved = true
  83. case strings.HasPrefix(s, "ASK "):
  84. ask = true
  85. default:
  86. return
  87. }
  88. ind := strings.LastIndex(s, " ")
  89. if ind == -1 {
  90. return false, false, ""
  91. }
  92. addr = s[ind+1:]
  93. return
  94. }
  95. func isLoadingError(err error) bool {
  96. return strings.HasPrefix(err.Error(), "LOADING ")
  97. }
  98. func isReadOnlyError(err error) bool {
  99. return strings.HasPrefix(err.Error(), "READONLY ")
  100. }
  101. //------------------------------------------------------------------------------
  102. type timeoutError interface {
  103. Timeout() bool
  104. }