mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-31 07:20:13 +00:00
make random.go threadsafe
This commit is contained in:
parent
f84a948c9c
commit
2a03a4d502
@ -18,14 +18,15 @@ package scheduler
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||||
)
|
)
|
||||||
|
|
||||||
// RandomScheduler chooses machines uniformly at random.
|
// RandomScheduler chooses machines uniformly at random.
|
||||||
type RandomScheduler struct {
|
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 {
|
func MakeRandomScheduler(random *rand.Rand) Scheduler {
|
||||||
@ -40,5 +41,8 @@ func (s *RandomScheduler) Schedule(pod api.Pod, minionLister MinionLister) (stri
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
s.randomLock.Lock()
|
||||||
|
defer s.randomLock.Unlock()
|
||||||
return machines[s.random.Int()%len(machines)], nil
|
return machines[s.random.Int()%len(machines)], nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user