Skip to content

Commit

Permalink
Use read lock for query methods in discovery.go
Browse files Browse the repository at this point in the history
It is better for query methods to use read lock than executive lock.

Change-Id: Iacc76abbda7a01891e2f7115eff2f99000203ba2
Signed-off-by: grapebaba <281165273@qq.com>
  • Loading branch information
GrapeBaBa committed Aug 8, 2016
1 parent 8002995 commit d4ea123
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions core/discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ func (di *DiscoveryImpl) RemoveNode(address string) bool {

// GetAllNodes returns an array of all addresses saved in the discovery list
func (di *DiscoveryImpl) GetAllNodes() []string {
di.Lock()
defer di.Unlock()
di.RLock()
defer di.RUnlock()
var addresses []string
for address, valid := range di.nodes {
if valid {
Expand All @@ -87,8 +87,8 @@ func (di *DiscoveryImpl) GetAllNodes() []string {
func (di *DiscoveryImpl) GetRandomNodes(n int) []string {
var pick string
randomNodes := make([]string, n)
di.Lock()
defer di.Unlock()
di.RLock()
defer di.RUnlock()
for i := 0; i < n; i++ {
for {
pick = di.seq[di.random.Intn(len(di.nodes))]
Expand All @@ -103,8 +103,8 @@ func (di *DiscoveryImpl) GetRandomNodes(n int) []string {

// FindNode returns true if its address is stored in the discovery list
func (di *DiscoveryImpl) FindNode(address string) bool {
di.Lock()
defer di.Unlock()
di.RLock()
defer di.RUnlock()
_, ok := di.nodes[address]
return ok
}
Expand Down

0 comments on commit d4ea123

Please sign in to comment.