Skip to content

Commit bbadd65

Browse files
cxljsofekshenawa
authored andcommitted
chore(doc): improve code readability (#3446)
- 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 6f1aac4 commit bbadd65

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
@@ -364,8 +364,7 @@ type clusterNode struct {
364364
failing uint32 // atomic
365365
loaded uint32 // atomic
366366

367-
// last time the latency measurement was performed for the node, stored in nanoseconds
368-
// from epoch
367+
// last time the latency measurement was performed for the node, stored in nanoseconds from epoch
369368
lastLatencyMeasurement int64 // atomic
370369
}
371370

@@ -502,13 +501,12 @@ type clusterNodes struct {
502501
closed bool
503502
onNewNode []func(rdb *Client)
504503

505-
_generation uint32 // atomic
504+
generation uint32 // atomic
506505
}
507506

508507
func newClusterNodes(opt *ClusterOptions) *clusterNodes {
509508
return &clusterNodes{
510-
opt: opt,
511-
509+
opt: opt,
512510
addrs: opt.Addrs,
513511
nodes: make(map[string]*clusterNode),
514512
}
@@ -568,12 +566,11 @@ func (c *clusterNodes) Addrs() ([]string, error) {
568566
}
569567

570568
func (c *clusterNodes) NextGeneration() uint32 {
571-
return atomic.AddUint32(&c._generation, 1)
569+
return atomic.AddUint32(&c.generation, 1)
572570
}
573571

574572
// GC removes unused nodes.
575573
func (c *clusterNodes) GC(generation uint32) {
576-
//nolint:prealloc
577574
var collected []*clusterNode
578575

579576
c.mu.Lock()
@@ -626,23 +623,20 @@ func (c *clusterNodes) GetOrCreate(addr string) (*clusterNode, error) {
626623
fn(node.Client)
627624
}
628625

629-
c.addrs = appendIfNotExists(c.addrs, addr)
626+
c.addrs = appendIfNotExist(c.addrs, addr)
630627
c.nodes[addr] = node
631628

632629
return node, nil
633630
}
634631

635632
func (c *clusterNodes) get(addr string) (*clusterNode, error) {
636-
var node *clusterNode
637-
var err error
638633
c.mu.RLock()
634+
defer c.mu.RUnlock()
635+
639636
if c.closed {
640-
err = pool.ErrClosed
641-
} else {
642-
node = c.nodes[addr]
637+
return nil, pool.ErrClosed
643638
}
644-
c.mu.RUnlock()
645-
return node, err
639+
return c.nodes[addr], nil
646640
}
647641

648642
func (c *clusterNodes) All() ([]*clusterNode, error) {
@@ -673,8 +667,9 @@ func (c *clusterNodes) Random() (*clusterNode, error) {
673667
//------------------------------------------------------------------------------
674668

675669
type clusterSlot struct {
676-
start, end int
677-
nodes []*clusterNode
670+
start int
671+
end int
672+
nodes []*clusterNode
678673
}
679674

680675
type clusterSlotSlice []*clusterSlot
@@ -734,9 +729,9 @@ func newClusterState(
734729
nodes = append(nodes, node)
735730

736731
if i == 0 {
737-
c.Masters = appendUniqueNode(c.Masters, node)
732+
c.Masters = appendIfNotExist(c.Masters, node)
738733
} else {
739-
c.Slaves = appendUniqueNode(c.Slaves, node)
734+
c.Slaves = appendIfNotExist(c.Slaves, node)
740735
}
741736
}
742737

@@ -1295,7 +1290,7 @@ func (c *ClusterClient) loadState(ctx context.Context) (*clusterState, error) {
12951290
continue
12961291
}
12971292

1298-
return newClusterState(c.nodes, slots, node.Client.opt.Addr)
1293+
return newClusterState(c.nodes, slots, addr)
12991294
}
13001295

13011296
/*
@@ -2017,7 +2012,7 @@ func (c *ClusterClient) MasterForKey(ctx context.Context, key string) (*Client,
20172012
if err != nil {
20182013
return nil, err
20192014
}
2020-
return node.Client, err
2015+
return node.Client, nil
20212016
}
20222017

20232018
func (c *ClusterClient) context(ctx context.Context) context.Context {
@@ -2027,26 +2022,13 @@ func (c *ClusterClient) context(ctx context.Context) context.Context {
20272022
return context.Background()
20282023
}
20292024

2030-
func appendUniqueNode(nodes []*clusterNode, node *clusterNode) []*clusterNode {
2031-
for _, n := range nodes {
2032-
if n == node {
2033-
return nodes
2034-
}
2035-
}
2036-
return append(nodes, node)
2037-
}
2038-
2039-
func appendIfNotExists(ss []string, es ...string) []string {
2040-
loop:
2041-
for _, e := range es {
2042-
for _, s := range ss {
2043-
if s == e {
2044-
continue loop
2045-
}
2025+
func appendIfNotExist[T comparable](vals []T, newVal T) []T {
2026+
for _, v := range vals {
2027+
if v == newVal {
2028+
return vals
20462029
}
2047-
ss = append(ss, e)
20482030
}
2049-
return ss
2031+
return append(vals, newVal)
20502032
}
20512033

20522034
//------------------------------------------------------------------------------

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)