Skip to content

Commit d4ea123

Browse files
committed
Use read lock for query methods in discovery.go
It is better for query methods to use read lock than executive lock. Change-Id: Iacc76abbda7a01891e2f7115eff2f99000203ba2 Signed-off-by: grapebaba <[email protected]>
1 parent 8002995 commit d4ea123

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

core/discovery/discovery.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ func (di *DiscoveryImpl) RemoveNode(address string) bool {
7272

7373
// GetAllNodes returns an array of all addresses saved in the discovery list
7474
func (di *DiscoveryImpl) GetAllNodes() []string {
75-
di.Lock()
76-
defer di.Unlock()
75+
di.RLock()
76+
defer di.RUnlock()
7777
var addresses []string
7878
for address, valid := range di.nodes {
7979
if valid {
@@ -87,8 +87,8 @@ func (di *DiscoveryImpl) GetAllNodes() []string {
8787
func (di *DiscoveryImpl) GetRandomNodes(n int) []string {
8888
var pick string
8989
randomNodes := make([]string, n)
90-
di.Lock()
91-
defer di.Unlock()
90+
di.RLock()
91+
defer di.RUnlock()
9292
for i := 0; i < n; i++ {
9393
for {
9494
pick = di.seq[di.random.Intn(len(di.nodes))]
@@ -103,8 +103,8 @@ func (di *DiscoveryImpl) GetRandomNodes(n int) []string {
103103

104104
// FindNode returns true if its address is stored in the discovery list
105105
func (di *DiscoveryImpl) FindNode(address string) bool {
106-
di.Lock()
107-
defer di.Unlock()
106+
di.RLock()
107+
defer di.RUnlock()
108108
_, ok := di.nodes[address]
109109
return ok
110110
}

0 commit comments

Comments
 (0)