From 6064e889b7307f5b0734250354f6caf88d659d92 Mon Sep 17 00:00:00 2001 From: Ahmet Alp Balkan Date: Mon, 15 Jun 2015 14:14:15 -0700 Subject: [PATCH] scheduler/random: Use private instance of rand This does not pollute the global rand instance. It's good for predictability and determinism in the tests and behavior. Reduces program state. Signed-off-by: Ahmet Alp Balkan --- scheduler/strategy/random.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/scheduler/strategy/random.go b/scheduler/strategy/random.go index c7a08d3c51..2aac01ff6e 100644 --- a/scheduler/strategy/random.go +++ b/scheduler/strategy/random.go @@ -10,11 +10,13 @@ import ( ) // RandomPlacementStrategy randomly places the container into the cluster. -type RandomPlacementStrategy struct{} +type RandomPlacementStrategy struct { + r *rand.Rand +} // Initialize is exported func (p *RandomPlacementStrategy) Initialize() error { - rand.Seed(time.Now().UTC().UnixNano()) + p.r = rand.New(rand.NewSource(time.Now().UTC().UnixNano())) return nil } @@ -26,7 +28,7 @@ func (p *RandomPlacementStrategy) Name() string { // PlaceContainer is exported func (p *RandomPlacementStrategy) PlaceContainer(config *cluster.ContainerConfig, nodes []*node.Node) (*node.Node, error) { if size := len(nodes); size > 0 { - return nodes[rand.Intn(size)], nil + return nodes[p.r.Intn(size)], nil } return nil, errors.New("No nodes running in the cluster")