Skip to content

Commit

Permalink
Merge branch 'fix/recognise-readonly-error-from-luascript' into 'master'
Browse files Browse the repository at this point in the history
recognise read only error returned from Lua script

See merge request dbops/Redis!5
  • Loading branch information
RonninSaga committed Oct 23, 2023
2 parents c0ab781 + 1058d7a commit dd6fb6e
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 @@ -936,7 +936,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 @@ -1620,7 +1620,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 @@ -58,7 +58,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 @@ -86,7 +86,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 @@ -136,8 +136,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 dd6fb6e

Please sign in to comment.