github.com/soulinfo/etcd@v0.1.2-0.20130922053317-3a0a8c89e859/machines.go (about)

     1  package main
     2  
     3  // machineNum returns the number of machines in the cluster
     4  func machineNum() int {
     5  	response, _ := etcdStore.RawGet("_etcd/machines")
     6  
     7  	return len(response)
     8  }
     9  
    10  // getMachines gets the current machines in the cluster
    11  func getMachines(toURL func(string) (string, bool)) []string {
    12  
    13  	peers := r.Peers()
    14  
    15  	machines := make([]string, len(peers)+1)
    16  
    17  	leader, ok := toURL(r.Leader())
    18  	self, _ := toURL(r.Name())
    19  	i := 1
    20  
    21  	if ok {
    22  		machines[0] = leader
    23  		if leader != self {
    24  			machines[1] = self
    25  			i = 2
    26  		}
    27  	} else {
    28  		machines[0] = self
    29  	}
    30  
    31  	// Add all peers to the slice
    32  	for peerName, _ := range peers {
    33  		if machine, ok := toURL(peerName); ok {
    34  			// do not add leader twice
    35  			if machine != leader {
    36  				machines[i] = machine
    37  				i++
    38  			}
    39  		}
    40  	}
    41  	return machines
    42  }