Skip to content

Commit a7fb1aa

Browse files
committed
improve code readability
- replace two similar functions `appendUniqueNode` and `appendIfNotExists` with a generic function. - simplify the implementation of the `get` method in `clusterNodes` - keep the member name `_generation` of `clusterNodes` consistent with other types. - rename a data member `_masterAddr` to `masterAddr`. Signed-off-by: Xiaolong Chen <[email protected]>
1 parent 3c85d09 commit a7fb1aa

File tree

3 files changed

+29
-47
lines changed

3 files changed

+29
-47
lines changed

osscluster.go

Lines changed: 21 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,7 @@ type clusterNode struct {
342342
failing uint32 // atomic
343343
loaded uint32 // atomic
344344

345-
// last time the latency measurement was performed for the node, stored in nanoseconds
346-
// from epoch
345+
// last time the latency measurement was performed for the node, stored in nanoseconds from epoch
347346
lastLatencyMeasurement int64 // atomic
348347
}
349348

@@ -480,13 +479,12 @@ type clusterNodes struct {
480479
closed bool
481480
onNewNode []func(rdb *Client)
482481

483-
_generation uint32 // atomic
482+
generation uint32 // atomic
484483
}
485484

486485
func newClusterNodes(opt *ClusterOptions) *clusterNodes {
487486
return &clusterNodes{
488-
opt: opt,
489-
487+
opt: opt,
490488
addrs: opt.Addrs,
491489
nodes: make(map[string]*clusterNode),
492490
}
@@ -546,12 +544,11 @@ func (c *clusterNodes) Addrs() ([]string, error) {
546544
}
547545

548546
func (c *clusterNodes) NextGeneration() uint32 {
549-
return atomic.AddUint32(&c._generation, 1)
547+
return atomic.AddUint32(&c.generation, 1)
550548
}
551549

552550
// GC removes unused nodes.
553551
func (c *clusterNodes) GC(generation uint32) {
554-
//nolint:prealloc
555552
var collected []*clusterNode
556553

557554
c.mu.Lock()
@@ -604,23 +601,20 @@ func (c *clusterNodes) GetOrCreate(addr string) (*clusterNode, error) {
604601
fn(node.Client)
605602
}
606603

607-
c.addrs = appendIfNotExists(c.addrs, addr)
604+
c.addrs = appendIfNotExist(c.addrs, addr)
608605
c.nodes[addr] = node
609606

610607
return node, nil
611608
}
612609

613610
func (c *clusterNodes) get(addr string) (*clusterNode, error) {
614-
var node *clusterNode
615-
var err error
616611
c.mu.RLock()
612+
defer c.mu.RUnlock()
613+
617614
if c.closed {
618-
err = pool.ErrClosed
619-
} else {
620-
node = c.nodes[addr]
615+
return nil, pool.ErrClosed
621616
}
622-
c.mu.RUnlock()
623-
return node, err
617+
return c.nodes[addr], nil
624618
}
625619

626620
func (c *clusterNodes) All() ([]*clusterNode, error) {
@@ -651,8 +645,9 @@ func (c *clusterNodes) Random() (*clusterNode, error) {
651645
//------------------------------------------------------------------------------
652646

653647
type clusterSlot struct {
654-
start, end int
655-
nodes []*clusterNode
648+
start int
649+
end int
650+
nodes []*clusterNode
656651
}
657652

658653
type clusterSlotSlice []*clusterSlot
@@ -712,9 +707,9 @@ func newClusterState(
712707
nodes = append(nodes, node)
713708

714709
if i == 0 {
715-
c.Masters = appendUniqueNode(c.Masters, node)
710+
c.Masters = appendIfNotExist(c.Masters, node)
716711
} else {
717-
c.Slaves = appendUniqueNode(c.Slaves, node)
712+
c.Slaves = appendIfNotExist(c.Slaves, node)
718713
}
719714
}
720715

@@ -1273,7 +1268,7 @@ func (c *ClusterClient) loadState(ctx context.Context) (*clusterState, error) {
12731268
continue
12741269
}
12751270

1276-
return newClusterState(c.nodes, slots, node.Client.opt.Addr)
1271+
return newClusterState(c.nodes, slots, addr)
12771272
}
12781273

12791274
/*
@@ -1995,7 +1990,7 @@ func (c *ClusterClient) MasterForKey(ctx context.Context, key string) (*Client,
19951990
if err != nil {
19961991
return nil, err
19971992
}
1998-
return node.Client, err
1993+
return node.Client, nil
19991994
}
20001995

20011996
func (c *ClusterClient) context(ctx context.Context) context.Context {
@@ -2005,26 +2000,13 @@ func (c *ClusterClient) context(ctx context.Context) context.Context {
20052000
return context.Background()
20062001
}
20072002

2008-
func appendUniqueNode(nodes []*clusterNode, node *clusterNode) []*clusterNode {
2009-
for _, n := range nodes {
2010-
if n == node {
2011-
return nodes
2012-
}
2013-
}
2014-
return append(nodes, node)
2015-
}
2016-
2017-
func appendIfNotExists(ss []string, es ...string) []string {
2018-
loop:
2019-
for _, e := range es {
2020-
for _, s := range ss {
2021-
if s == e {
2022-
continue loop
2023-
}
2003+
func appendIfNotExist[T comparable](vals []T, newVal T) []T {
2004+
for _, v := range vals {
2005+
if v == newVal {
2006+
return vals
20242007
}
2025-
ss = append(ss, e)
20262008
}
2027-
return ss
2009+
return append(vals, newVal)
20282010
}
20292011

20302012
//------------------------------------------------------------------------------

redis.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ func (c *baseClient) initConn(ctx context.Context, cn *pool.Conn) error {
383383

384384
// for redis-server versions that do not support the HELLO command,
385385
// RESP2 will continue to be used.
386-
if err = conn.Hello(ctx, c.opt.Protocol, username, password, c.opt.ClientName).Err(); err == nil {
386+
if err = conn.Hello(ctx, c.opt.Protocol, username, password, c.opt.ClientName).Err(); err == nil {
387387
// Authentication successful with HELLO command
388388
} else if !isRedisError(err) {
389389
// When the server responds with the RESP protocol and the result is not a normal

sentinel.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -657,10 +657,10 @@ type sentinelFailover struct {
657657
onFailover func(ctx context.Context, addr string)
658658
onUpdate func(ctx context.Context)
659659

660-
mu sync.RWMutex
661-
_masterAddr string
662-
sentinel *SentinelClient
663-
pubsub *PubSub
660+
mu sync.RWMutex
661+
masterAddr string
662+
sentinel *SentinelClient
663+
pubsub *PubSub
664664
}
665665

666666
func (c *sentinelFailover) Close() error {
@@ -921,7 +921,7 @@ func parseReplicaAddrs(addrs []map[string]string, keepDisconnected bool) []strin
921921

922922
func (c *sentinelFailover) trySwitchMaster(ctx context.Context, addr string) {
923923
c.mu.RLock()
924-
currentAddr := c._masterAddr //nolint:ifshort
924+
currentAddr := c.masterAddr //nolint:ifshort
925925
c.mu.RUnlock()
926926

927927
if addr == currentAddr {
@@ -931,10 +931,10 @@ func (c *sentinelFailover) trySwitchMaster(ctx context.Context, addr string) {
931931
c.mu.Lock()
932932
defer c.mu.Unlock()
933933

934-
if addr == c._masterAddr {
934+
if addr == c.masterAddr {
935935
return
936936
}
937-
c._masterAddr = addr
937+
c.masterAddr = addr
938938

939939
internal.Logger.Printf(ctx, "sentinel: new master=%q addr=%q",
940940
c.opt.MasterName, addr)

0 commit comments

Comments
 (0)