[Random] Pick a node for O(1), not for O(n)

Signed-off-by: Anton Tiurin <noxiouz@yandex.ru>
This commit is contained in:
Anton Tiurin 2015-04-02 00:23:35 +03:00
parent 1f2bd67555
commit f760b25e12
1 changed files with 3 additions and 6 deletions

View File

@ -21,12 +21,9 @@ func (p *RandomPlacementStrategy) Initialize() error {
// PlaceContainer is exported // PlaceContainer is exported
func (p *RandomPlacementStrategy) PlaceContainer(config *dockerclient.ContainerConfig, nodes []cluster.Node) (cluster.Node, error) { func (p *RandomPlacementStrategy) PlaceContainer(config *dockerclient.ContainerConfig, nodes []cluster.Node) (cluster.Node, error) {
if size := len(nodes); size > 0 { if size := len(nodes); size > 0 {
n := rand.Intn(len(nodes)) index := rand.Intn(len(nodes))
for i, node := range nodes { return nodes[index], nil
if i == n {
return node, nil
}
}
} }
return nil, errors.New("No nodes running in the cluster") return nil, errors.New("No nodes running in the cluster")
} }