From ac9e1abf54f65c2c11acdc67a2dc593b11ecda52 Mon Sep 17 00:00:00 2001 From: Vladimir Mihailenco Date: Wed, 27 Feb 2019 12:53:44 +0200 Subject: [PATCH] Retry master node on readonly errors. Fixes #977 --- cluster.go | 4 ++-- internal/error.go | 7 ++++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/cluster.go b/cluster.go index c25601bd..0cecc62c 100644 --- a/cluster.go +++ b/cluster.go @@ -862,7 +862,7 @@ func (c *ClusterClient) Watch(fn func(*Tx) error, keys ...string) error { continue } - if err == pool.ErrClosed { + if err == pool.ErrClosed || internal.IsReadOnlyError(err) { node, err = c.slotMasterNode(slot) if err != nil { return err @@ -960,7 +960,7 @@ func (c *ClusterClient) defaultProcess(cmd Cmder) error { continue } - if err == pool.ErrClosed { + if err == pool.ErrClosed || internal.IsReadOnlyError(err) { node = nil continue } diff --git a/internal/error.go b/internal/error.go index 5feb861e..34f6bd4d 100644 --- a/internal/error.go +++ b/internal/error.go @@ -47,7 +47,8 @@ func IsBadConn(err error, allowTimeout bool) bool { return false } if IsRedisError(err) { - return strings.HasPrefix(err.Error(), "READONLY ") + // #790 + return IsReadOnlyError(err) } if allowTimeout { if netErr, ok := err.(net.Error); ok && netErr.Timeout() { @@ -82,3 +83,7 @@ func IsMovedError(err error) (moved bool, ask bool, addr string) { func IsLoadingError(err error) bool { return strings.HasPrefix(err.Error(), "LOADING ") } + +func IsReadOnlyError(err error) bool { + return strings.HasPrefix(err.Error(), "READONLY ") +}