Browse Source

control.go: fix race condition on randr

Quentin Perez 9 years ago
parent
commit
6807d66b46
1 changed files with 6 additions and 1 deletions
  1. 6 1
      control.go

+ 6 - 1
control.go

@@ -11,11 +11,14 @@ import (
 	"sync/atomic"
 	"time"
 
+	"sync"
+
 	"golang.org/x/net/context"
 )
 
 var (
-	randr *rand.Rand
+	randr    *rand.Rand
+	mutRandr sync.Mutex
 )
 
 func init() {
@@ -135,7 +138,9 @@ func hostInfo(addr string, defaultPort int) (*HostInfo, error) {
 }
 
 func shuffleHosts(hosts []*HostInfo) []*HostInfo {
+	mutRandr.Lock()
 	perm := randr.Perm(len(hosts))
+	mutRandr.Unlock()
 	shuffled := make([]*HostInfo, len(hosts))
 
 	for i, host := range hosts {