Documentation ¶
Index ¶
- Constants
- Variables
- func NewHashedStrategy(hashFunc HashFunc) *hashedStrategy
- func NewRRDNSService(hostnames []string, ipv4 bool, ipv6 bool) *rrDNSService
- func NewRoundRobinStrategy() *roundRobinStrategy
- func NewStaticService(nodes []Node) *staticService
- type Group
- type HashFunc
- type LoadBalancer
- type LoadBalancerOption
- type Metrics
- type Node
- type Service
- type Strategy
Constants ¶
View Source
const ( NodeFailedKey = "node-failed." RefreshKey = "refresh" )
View Source
const ( TTLUnlimited time.Duration = math.MaxInt64 // Never expire TTLNone time.Duration = 0 // Refresh after every Next() HealthyNodeMustAll float64 = 1.0 HealthyNodeAny float64 = 0.0 NodeFailedUnlimited float64 = 1.0 NodeFailedAny float64 = 0.0 )
View Source
const (
DefaultMinSampleSize = 10
)
View Source
const (
NodeCountUnlimited = (1<<bits.UintSize)/2 - 1
)
Variables ¶
View Source
var ( SampleNotEnoughError = errors.New("sample not enough") InvalidRatioError = errors.New("invalid ratio error") )
View Source
var ( DefaultLoadBalancerOption = LoadBalancerOption{ MaxNodeCount: NodeCountUnlimited, TTL: TTLUnlimited, MinHealthyNodeRatio: HealthyNodeAny, MaxNodeFailedRatio: NodeFailedUnlimited, MinSampleSize: DefaultMinSampleSize, } )
Functions ¶
func NewHashedStrategy ¶
func NewHashedStrategy(hashFunc HashFunc) *hashedStrategy
func NewRRDNSService ¶
NewRRDNSService is for Round-robin DNS load balancing solution. Usually multiple A or AAAA records are associated with single hostname. Node type: *net.IPAddr
For example:
Hostname www.a.com |- A 1.2.3.4 |- A 2.3.4.5 |- A 3.4.5.6 ...
Everytime a client wants to send a request, one of these A records will be chosen to establish connection.
func NewRoundRobinStrategy ¶
func NewRoundRobinStrategy() *roundRobinStrategy
func NewStaticService ¶
func NewStaticService(nodes []Node) *staticService
NewStaticService represents a simple static list of Node Node type: node.Node
Types ¶
type Group ¶
type Group struct {
// contains filtered or unexported fields
}
func (*Group) GetCurrentCount ¶
func (*Group) GetOriginalCount ¶
type LoadBalancer ¶
type LoadBalancer struct {
// contains filtered or unexported fields
}
func NewLoadBalancer ¶
func NewLoadBalancer(service Service, strategy Strategy, option ...LoadBalancerOption) *LoadBalancer
func (*LoadBalancer) Next ¶
func (lb *LoadBalancer) Next() (Node, error)
func (*LoadBalancer) NextFor ¶
func (lb *LoadBalancer) NextFor(input interface{}) (Node, error)
func (*LoadBalancer) NodeFailed ¶
func (lb *LoadBalancer) NodeFailed(node Node)
type LoadBalancerOption ¶
type LoadBalancerOption struct { // LoadBalancer will keep MaxNodeCount nodes in Next() result set // Please notice that refresh or exile will change the result set // Number of connections might be greater than this value, if any pre-connected node has been excluded in newer // result set, but no new connection will be established to these nodes MaxNodeCount int // Cache TTL TTL time.Duration // Refresh when healthy node ratio is below MinHealthyNodeRatio MinHealthyNodeRatio float64 // Node will be exiled if (failed / total) > MaxNodeFailedRatio MaxNodeFailedRatio float64 // At least MinSampleSize times a node has been returned through Next(), this node can be count for exile. MinSampleSize int }
type Metrics ¶
type Metrics struct { MinSampleSize int // contains filtered or unexported fields }
func NewMetrics ¶
NewMetrics initializes a Metrics instance, it might return nil if it's unnecessary or invalid
func (*Metrics) GetNodeFailedRatio ¶
func (*Metrics) NodeFailedInc ¶
func (*Metrics) ResetAllNodes ¶
func (m *Metrics) ResetAllNodes()
type Service ¶
type Service interface { // Nodes returns a new slice of available node Nodes() []Node // Refresh updates nodes Refresh() // NodeFailedCallbackFunc returns a callback function which will be triggered in another go routine when certain // node exiled by LoadBalancer NodeFailedCallbackFunc() func(node Node) }
Service represents a group of nodes provide identical functionality, cluster as a logical service This type should be thread safe
type Strategy ¶
type Strategy interface { // SetNodes update saved nodes SetNodes(nodes []Node) // Next returns a node address Next() (Node, error) // NextFor returns a node address assigned to request NextFor(interface{}) (Node, error) }
Strategy controls how the nodes are chosen This type should be thread safe
Source Files ¶
Click to show internal directories.
Click to hide internal directories.