Skip to content

Commit

Permalink
recognise read only error returned from Lua script
Browse files Browse the repository at this point in the history
  • Loading branch information
jilei-grabtaxi authored and RonninSaga committed May 21, 2024
1 parent 9107b69 commit 2ce2eab
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
4 changes: 2 additions & 2 deletions cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,7 @@ func (c *ClusterClient) process(ctx context.Context, cmd Cmder) error {
if lastErr == nil {
return nil
}
if isReadOnly := isReadOnlyError(lastErr); isReadOnly || lastErr == pool.ErrClosed {
if isReadOnly := IsReadOnlyError(lastErr); isReadOnly || lastErr == pool.ErrClosed {
if isReadOnly {
c.state.LazyReload()
}
Expand Down Expand Up @@ -1484,7 +1484,7 @@ func (c *ClusterClient) Watch(ctx context.Context, fn func(*Tx) error, keys ...s
continue
}

if isReadOnly := isReadOnlyError(err); isReadOnly || err == pool.ErrClosed {
if isReadOnly := IsReadOnlyError(err); isReadOnly || err == pool.ErrClosed {
if isReadOnly {
c.state.LazyReload()
}
Expand Down
15 changes: 11 additions & 4 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func shouldRetry(err error, retryTimeout bool) bool {
if strings.HasPrefix(s, "LOADING ") {
return true
}
if strings.HasPrefix(s, "READONLY ") {
if IsReadOnlyError(err) {
return true
}
if strings.HasPrefix(s, "CLUSTERDOWN ") {
Expand Down Expand Up @@ -75,7 +75,7 @@ func isBadConn(err error, allowTimeout bool, addr string) bool {

if isRedisError(err) {
switch {
case isReadOnlyError(err):
case IsReadOnlyError(err):
// Close connections in read only state in case domain addr is used
// and domain resolves to a different Redis Server. See #790.
return true
Expand Down Expand Up @@ -125,8 +125,15 @@ func isLoadingError(err error) bool {
return strings.HasPrefix(err.Error(), "LOADING ")
}

func isReadOnlyError(err error) bool {
return strings.HasPrefix(err.Error(), "READONLY ")
func IsReadOnlyError(err error) bool {
redisError := err.Error()
if strings.HasPrefix(redisError, "READONLY ") {
return true
}

// For a Lua script that includes write commands, the read-only error string
// contains "-READONLY" rather than beginning with "READONLY "
return strings.Contains(redisError, "-READONLY")
}

func isMovedSameConnAddr(err error, addr string) bool {
Expand Down

0 comments on commit 2ce2eab

Please sign in to comment.