From 2a03a4d502095f09520e666b32b88e1549a1ee0d Mon Sep 17 00:00:00 2001 From: Kouhei Ueno Date: Sat, 12 Jul 2014 15:06:36 +0900 Subject: [PATCH] make random.go threadsafe --- pkg/scheduler/random.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/pkg/scheduler/random.go b/pkg/scheduler/random.go index 23a076636a8..6cf0e9057bc 100644 --- a/pkg/scheduler/random.go +++ b/pkg/scheduler/random.go @@ -18,14 +18,15 @@ package scheduler import ( "math/rand" + "sync" "github.com/GoogleCloudPlatform/kubernetes/pkg/api" ) // RandomScheduler chooses machines uniformly at random. type RandomScheduler struct { - // TODO: rand.Rand is *NOT* thread safe. - random *rand.Rand + random *rand.Rand + randomLock sync.Mutex } func MakeRandomScheduler(random *rand.Rand) Scheduler { @@ -40,5 +41,8 @@ func (s *RandomScheduler) Schedule(pod api.Pod, minionLister MinionLister) (stri if err != nil { return "", err } + + s.randomLock.Lock() + defer s.randomLock.Unlock() return machines[s.random.Int()%len(machines)], nil }