From fa76dd0e87967c95e6f7bef33a230ed2c263fe2d Mon Sep 17 00:00:00 2001 From: Vladimir Mihailenco Date: Sun, 14 Oct 2018 11:27:34 +0300 Subject: [PATCH] Add releaseConnStrict --- cluster.go | 12 ++---------- internal/error.go | 3 +++ redis.go | 21 +++++++++++---------- ring.go | 6 +----- 4 files changed, 17 insertions(+), 25 deletions(-) diff --git a/cluster.go b/cluster.go index 9c62fb6..55bc5ba 100644 --- a/cluster.go +++ b/cluster.go @@ -1272,11 +1272,7 @@ func (c *ClusterClient) defaultProcessPipeline(cmds []Cmder) error { } err = c.pipelineProcessCmds(node, cn, cmds, failedCmds) - if err == nil || internal.IsRedisError(err) { - node.Client.connPool.Put(cn) - } else { - node.Client.connPool.Remove(cn) - } + node.Client.releaseConnStrict(cn, err) }(node, cmds) } @@ -1466,11 +1462,7 @@ func (c *ClusterClient) defaultProcessTxPipeline(cmds []Cmder) error { } err = c.txPipelineProcessCmds(node, cn, cmds, failedCmds) - if err == nil || internal.IsRedisError(err) { - node.Client.connPool.Put(cn) - } else { - node.Client.connPool.Remove(cn) - } + node.Client.releaseConnStrict(cn, err) }(node, cmds) } diff --git a/internal/error.go b/internal/error.go index bda97ba..5feb861 100644 --- a/internal/error.go +++ b/internal/error.go @@ -9,6 +9,9 @@ import ( ) func IsRetryableError(err error, retryTimeout bool) bool { + if err == nil { + return false + } if err == io.EOF { return true } diff --git a/redis.go b/redis.go index 6405671..92a07fc 100644 --- a/redis.go +++ b/redis.go @@ -77,14 +77,20 @@ func (c *baseClient) getConn() (*pool.Conn, error) { return cn, nil } -func (c *baseClient) releaseConn(cn *pool.Conn, err error) bool { +func (c *baseClient) releaseConn(cn *pool.Conn, err error) { if internal.IsBadConn(err, false) { c.connPool.Remove(cn) - return false + } else { + c.connPool.Put(cn) } +} - c.connPool.Put(cn) - return true +func (c *baseClient) releaseConnStrict(cn *pool.Conn, err error) { + if err == nil || internal.IsRedisError(err) { + c.connPool.Put(cn) + } else { + c.connPool.Remove(cn) + } } func (c *baseClient) initConn(cn *pool.Conn) error { @@ -248,12 +254,7 @@ func (c *baseClient) generalProcessPipeline(cmds []Cmder, p pipelineProcessor) e } canRetry, err := p(cn, cmds) - - if err == nil || internal.IsRedisError(err) { - c.connPool.Put(cn) - break - } - c.connPool.Remove(cn) + c.releaseConnStrict(cn, err) if !canRetry || !internal.IsRetryableError(err, true) { break diff --git a/ring.go b/ring.go index 42a4e6c..689f144 100644 --- a/ring.go +++ b/ring.go @@ -614,11 +614,7 @@ func (c *Ring) defaultProcessPipeline(cmds []Cmder) error { } canRetry, err := shard.Client.pipelineProcessCmds(cn, cmds) - if err == nil || internal.IsRedisError(err) { - shard.Client.connPool.Put(cn) - return - } - shard.Client.connPool.Remove(cn) + shard.Client.releaseConnStrict(cn, err) if canRetry && internal.IsRetryableError(err, true) { mu.Lock()