Skip to content

Commit a5d952c

Browse files
jilei-grabtaxiRonninSaga
authored andcommitted
recognise read only error returned from Lua script
1 parent bce02c1 commit a5d952c

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

Diff for: cluster.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,7 @@ func (c *ClusterClient) process(ctx context.Context, cmd Cmder) error {
810810
if lastErr == nil {
811811
return nil
812812
}
813-
if isReadOnly := isReadOnlyError(lastErr); isReadOnly || lastErr == pool.ErrClosed {
813+
if isReadOnly := IsReadOnlyError(lastErr); isReadOnly || lastErr == pool.ErrClosed {
814814
if isReadOnly {
815815
c.state.LazyReload()
816816
}
@@ -1491,7 +1491,7 @@ func (c *ClusterClient) Watch(ctx context.Context, fn func(*Tx) error, keys ...s
14911491
continue
14921492
}
14931493

1494-
if isReadOnly := isReadOnlyError(err); isReadOnly || err == pool.ErrClosed {
1494+
if isReadOnly := IsReadOnlyError(err); isReadOnly || err == pool.ErrClosed {
14951495
if isReadOnly {
14961496
c.state.LazyReload()
14971497
}

Diff for: error.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func shouldRetry(err error, retryTimeout bool) bool {
4747
if strings.HasPrefix(s, "LOADING ") {
4848
return true
4949
}
50-
if strings.HasPrefix(s, "READONLY ") {
50+
if IsReadOnlyError(err) {
5151
return true
5252
}
5353
if strings.HasPrefix(s, "CLUSTERDOWN ") {
@@ -75,7 +75,7 @@ func isBadConn(err error, allowTimeout bool, addr string) bool {
7575

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

128-
func isReadOnlyError(err error) bool {
129-
return strings.HasPrefix(err.Error(), "READONLY ")
128+
func IsReadOnlyError(err error) bool {
129+
redisError := err.Error()
130+
if strings.HasPrefix(redisError, "READONLY ") {
131+
return true
132+
}
133+
134+
// For a Lua script that includes write commands, the read-only error string
135+
// contains "-READONLY" rather than beginning with "READONLY "
136+
return strings.Contains(redisError, "-READONLY")
130137
}
131138

132139
func isMovedSameConnAddr(err error, addr string) bool {

0 commit comments

Comments
 (0)