github.com/andy2046/gopie@v0.7.0/pkg/subset/subset.go (about)

     1  // Package subset implements deterministic subsetting.
     2  package subset
     3  
     4  /*
     5     https://landing.google.com/sre/book/chapters/load-balancing-datacenter.html
     6  */
     7  
     8  import (
     9  	"math/rand"
    10  )
    11  
    12  // Subset returns a subset of backends with size subsetSize.
    13  func Subset(backends []string, clientID, subsetSize int) []string {
    14  
    15  	subsetCount := len(backends) / subsetSize
    16  
    17  	// Group clients into rounds; each round uses the same shuffled list:
    18  	round := clientID / subsetCount
    19  
    20  	r := rand.New(rand.NewSource(int64(round)))
    21  	r.Shuffle(len(backends), func(i, j int) { backends[i], backends[j] = backends[j], backends[i] })
    22  
    23  	// The subset id corresponding to the current client:
    24  	subsetID := clientID % subsetCount
    25  
    26  	start := subsetID * subsetSize
    27  	return backends[start : start+subsetSize]
    28  }