...

Source file src/github.com/go-kit/kit/sd/lb/random.go

Documentation: github.com/go-kit/kit/sd/lb

     1  package lb
     2  
     3  import (
     4  	"math/rand"
     5  
     6  	"github.com/go-kit/kit/endpoint"
     7  	"github.com/go-kit/kit/sd"
     8  )
     9  
    10  // NewRandom returns a load balancer that selects services randomly.
    11  func NewRandom(s sd.Endpointer, seed int64) Balancer {
    12  	return &random{
    13  		s: s,
    14  		r: rand.New(rand.NewSource(seed)),
    15  	}
    16  }
    17  
    18  type random struct {
    19  	s sd.Endpointer
    20  	r *rand.Rand
    21  }
    22  
    23  func (r *random) Endpoint() (endpoint.Endpoint, error) {
    24  	endpoints, err := r.s.Endpoints()
    25  	if err != nil {
    26  		return nil, err
    27  	}
    28  	if len(endpoints) <= 0 {
    29  		return nil, ErrNoEndpoints
    30  	}
    31  	return endpoints[r.r.Intn(len(endpoints))], nil
    32  }
    33  

View as plain text