From eca4e5d04c5ec7206040ddbe179a24ca4ac12fa1 Mon Sep 17 00:00:00 2001 From: Kirill Motkov Date: Wed, 17 Apr 2019 16:14:30 +0300 Subject: [PATCH] Some code improvements * Rewrite if-else chain as a switch. * Rewrite switch statement with only one case as if. * Remove always true condition. * Simplify some functions. --- command.go | 8 ++++---- redis.go | 6 ++---- sentinel.go | 7 ++----- 3 files changed, 8 insertions(+), 13 deletions(-) diff --git a/command.go b/command.go index 7cb3d3b..dde513b 100644 --- a/command.go +++ b/command.go @@ -717,12 +717,12 @@ func (cmd *StringSliceCmd) readReply(rd *proto.Reader) error { func stringSliceParser(rd *proto.Reader, n int64) (interface{}, error) { ss := make([]string, 0, n) for i := int64(0); i < n; i++ { - s, err := rd.ReadString() - if err == Nil { + switch s, err := rd.ReadString(); { + case err == Nil: ss = append(ss, "") - } else if err != nil { + case err != nil: return nil, err - } else { + default: ss = append(ss, s) } } diff --git a/redis.go b/redis.go index 644c8da..a767376 100644 --- a/redis.go +++ b/redis.go @@ -201,9 +201,7 @@ func (c *baseClient) defaultProcess(cmd Cmder) error { return err } - err = cn.WithReader(c.cmdTimeout(cmd), func(rd *proto.Reader) error { - return cmd.readReply(rd) - }) + err = cn.WithReader(c.cmdTimeout(cmd), cmd.readReply) c.releaseConn(cn, err) if err != nil && internal.IsRetryableError(err, cmd.readTimeout() == nil) { continue @@ -237,7 +235,7 @@ func (c *baseClient) cmdTimeout(cmd Cmder) time.Duration { func (c *baseClient) Close() error { var firstErr error if c.onClose != nil { - if err := c.onClose(); err != nil && firstErr == nil { + if err := c.onClose(); err != nil { firstErr = err } } diff --git a/sentinel.go b/sentinel.go index 691d171..016aa76 100644 --- a/sentinel.go +++ b/sentinel.go @@ -90,9 +90,7 @@ func NewFailoverClient(failoverOpt *FailoverOptions) *Client { opt: opt, connPool: failover.Pool(), - onClose: func() error { - return failover.Close() - }, + onClose: failover.Close, }, } c.baseClient.init() @@ -391,8 +389,7 @@ func (c *sentinelFailover) listen(pubsub *PubSub) { break } - switch msg.Channel { - case "+switch-master": + if msg.Channel == "+switch-master" { parts := strings.Split(msg.Payload, " ") if parts[0] != c.masterName { internal.Logf("sentinel: ignore addr for master=%q", parts[0])