Documentation
¶
Overview ¶
Example ¶
// define some service ins := []*myService{{ Address: "192.168.0.90:9000", Memory: 5, // here memory as weight }, { Address: "192.168.0.90:9001", Memory: 3, // here memory as weight }, { Address: "192.168.0.90:9002", Memory: 2, // here memory as weight }} var selector loadbalance.Selector[string, *myService] // all methods is concurrncy safe and fast selector = loadbalance.NewDynamicWeighted[string, *myService]() // add some instances selector.Add(ins...) // select a instance _ = selector.Select() // delete a instances selector.Del(ins[2]) // get a instance by its ID,the first result is instance, // second result is the instance wether exist _, _ = selector.Get("192.168.0.90:9002") _ = selector.Size() // check all instance by for-each selector.ForEach(func(_ string, ms *myService) bool { fmt.Println(ms) return true })
Output:
Index ¶
- func GetHashFunc[T Hashable]() func(T) uint64
- func Hash[T Hashable](elem T) uint64
- type ConsistentHash
- func (c *ConsistentHash[T]) Add(instances ...Instance[T]) int
- func (c *ConsistentHash[T]) Del(instances ...Instance[T]) int
- func (c *ConsistentHash[T]) ForEach(callback func(T, Instance[T]) bool)
- func (c *ConsistentHash[T]) Get(key T) (Instance[T], bool)
- func (c *ConsistentHash[T]) Select(key string) Instance[T]
- func (c *ConsistentHash[T]) Size() int
- type DynamicWeighted
- func (sl *DynamicWeighted[T, I]) Add(instances ...I) int
- func (sl *DynamicWeighted[T, I]) Del(instances ...I) int
- func (sl *DynamicWeighted[T, I]) ForEach(callback func(T, I) bool)
- func (sl *DynamicWeighted[T, I]) Get(key T) (ins I, ok bool)
- func (sl *DynamicWeighted[T, I]) Select() (ins I)
- func (sl *DynamicWeighted[T, I]) Size() int
- type Hashable
- type Instance
- type InstanceList
- type Random
- type RoundRobin
- type Selector
- type SelectorBy
- type SourceAddressHash
- func (kh *SourceAddressHash[T]) Add(instances ...Instance[T]) int
- func (kh *SourceAddressHash[T]) Del(instances ...Instance[T]) int
- func (kh *SourceAddressHash[T]) ForEach(callback func(T, Instance[T]) bool)
- func (kh *SourceAddressHash[T]) Get(key T) (Instance[T], bool)
- func (kh *SourceAddressHash[T]) SelectBy(key string) Instance[T]
- func (kh *SourceAddressHash[T]) Size() int
- type WeightNode
- type WeightedRandom
- func (wr *WeightedRandom[T, I]) Add(instances ...I) int
- func (wr *WeightedRandom[T, I]) Del(instances ...I) int
- func (wr *WeightedRandom[T, I]) ForEach(callback func(T, I) bool)
- func (wr *WeightedRandom[T, I]) Get(key T) (I, bool)
- func (wr *WeightedRandom[T, I]) Select() (ins I)
- func (wr *WeightedRandom[T, I]) Size() int
- type WeightedRoundRobin
- func (wrr *WeightedRoundRobin[T, I]) Add(instances ...I) int
- func (wrr *WeightedRoundRobin[T, I]) Del(instances ...I) int
- func (wrr *WeightedRoundRobin[T, I]) ForEach(callback func(T, I) bool)
- func (wrr *WeightedRoundRobin[T, I]) Get(key T) (I, bool)
- func (wrr *WeightedRoundRobin[T, I]) Select() (ins I)
- func (wrr *WeightedRoundRobin[T, I]) Size() int
- type XorShift64
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetHashFunc ¶
Types ¶
type ConsistentHash ¶
type ConsistentHash[T Hashable] struct { // contains filtered or unexported fields }
func NewConsistentHash ¶
func NewConsistentHash[T Hashable](replicas ...int) *ConsistentHash[T]
func (*ConsistentHash[T]) Add ¶
func (c *ConsistentHash[T]) Add(instances ...Instance[T]) int
Add 方法用来添加缓存节点,参数为节点key,比如使用IP
func (*ConsistentHash[T]) Del ¶
func (c *ConsistentHash[T]) Del(instances ...Instance[T]) int
func (*ConsistentHash[T]) ForEach ¶
func (c *ConsistentHash[T]) ForEach(callback func(T, Instance[T]) bool)
func (*ConsistentHash[T]) Get ¶
func (c *ConsistentHash[T]) Get(key T) (Instance[T], bool)
func (*ConsistentHash[T]) Select ¶
func (c *ConsistentHash[T]) Select(key string) Instance[T]
Select 方法根据给定的对象获取最靠近它的那个节点
func (*ConsistentHash[T]) Size ¶
func (c *ConsistentHash[T]) Size() int
type DynamicWeighted ¶ added in v0.0.2
type DynamicWeighted[T Hashable, I Instance[T]] struct { // contains filtered or unexported fields }
DynamicWeighted uses two queue implements a concurrency safe , fast , dynamic weighted load-balance supported select, add and delete instance operation and these time complexity of operations all are O(1). The performance of this implementation is unrelated to the number of instance.
At first,define a struct named `instanceWrapper` which is includes two filed that one named `instance`(a type implements generic interface `Instance[T]`) and the other named `weight` (int).
Then the step of select algorithm as follows:
- pop node every time from this queue if the weight of current `instanceWrapper` is `minInt64` , indicate that the instance was flagged delete, re-pop a Wrapper and give the current `instanceWrapper` to gc.
- While the main queue is empty,swap the main queue and second queue.
- After popping `instanceWrapper` that it didn't flag deleted, then the weight of current `instanceWrapper` subtract one,
- if the weight is 0 , reset the weight by the method of instance named `InstanceWeight()` , and push to the second queue, otherwise push to the main queue. At last,get and return the instance from the current `instanceWrapper`.
some notes :
- type I better is a type that can be compared with `nil`, such as a pointer or an interface.
- this processing needs the protection of the mutex lock, After doing many implements includes using lock-free queue and atomic operations and doing more times test and benchmark,the performance of current way is the best.
- Maybe my implementation used lock-free queue has some problem, the result of implementation that used lock-free queue and atomic operation of benchmark test always about 180 ns/op.
func NewDynamicWeighted ¶ added in v0.0.4
func NewDynamicWeighted[T Hashable, I Instance[T]]() *DynamicWeighted[T, I]
func (*DynamicWeighted[T, I]) Add ¶ added in v0.0.2
func (sl *DynamicWeighted[T, I]) Add(instances ...I) int
Add some instances and return the number of successful operation
func (*DynamicWeighted[T, I]) Del ¶ added in v0.0.2
func (sl *DynamicWeighted[T, I]) Del(instances ...I) int
Del some instances and return the number of successful operation
func (*DynamicWeighted[T, I]) ForEach ¶ added in v0.0.2
func (sl *DynamicWeighted[T, I]) ForEach(callback func(T, I) bool)
ForEach every instances. it is concurrency safe.
func (*DynamicWeighted[T, I]) Get ¶ added in v0.0.2
func (sl *DynamicWeighted[T, I]) Get(key T) (ins I, ok bool)
Get the value corresponding to the key
func (*DynamicWeighted[T, I]) Select ¶ added in v0.0.2
func (sl *DynamicWeighted[T, I]) Select() (ins I)
Select a instance
func (*DynamicWeighted[T, I]) Size ¶ added in v0.0.2
func (sl *DynamicWeighted[T, I]) Size() int
type InstanceList ¶
func (InstanceList[T, I]) Len ¶
func (ig InstanceList[T, I]) Len() int
func (InstanceList[T, I]) Less ¶
func (ig InstanceList[T, I]) Less(i, j int) bool
func (InstanceList[T, I]) Swap ¶
func (ig InstanceList[T, I]) Swap(i, j int)
type RoundRobin ¶
轮询负载均衡
func NewRoundRobin ¶
func NewRoundRobin[T Hashable, I Instance[T]]() *RoundRobin[T, I]
func (*RoundRobin[T, I]) Add ¶
func (rr *RoundRobin[T, I]) Add(instances ...I) int
func (*RoundRobin[T, I]) Del ¶
func (rr *RoundRobin[T, I]) Del(instances ...I) int
func (*RoundRobin[T, I]) ForEach ¶
func (rr *RoundRobin[T, I]) ForEach(callback func(T, I) bool)
func (*RoundRobin[T, I]) Get ¶
func (rr *RoundRobin[T, I]) Get(key T) (I, bool)
func (*RoundRobin[T, I]) Select ¶
func (rr *RoundRobin[T, I]) Select() (ins I)
func (*RoundRobin[T, I]) Size ¶
func (rr *RoundRobin[T, I]) Size() int
type SelectorBy ¶
type SourceAddressHash ¶
type SourceAddressHash[T Hashable] struct { // contains filtered or unexported fields }
func NewSourceAddressHash ¶
func NewSourceAddressHash[T Hashable]() *SourceAddressHash[T]
func (*SourceAddressHash[T]) Add ¶
func (kh *SourceAddressHash[T]) Add(instances ...Instance[T]) int
func (*SourceAddressHash[T]) Del ¶
func (kh *SourceAddressHash[T]) Del(instances ...Instance[T]) int
func (*SourceAddressHash[T]) ForEach ¶
func (kh *SourceAddressHash[T]) ForEach(callback func(T, Instance[T]) bool)
func (*SourceAddressHash[T]) Get ¶
func (kh *SourceAddressHash[T]) Get(key T) (Instance[T], bool)
func (*SourceAddressHash[T]) SelectBy ¶
func (kh *SourceAddressHash[T]) SelectBy(key string) Instance[T]
func (*SourceAddressHash[T]) Size ¶
func (kh *SourceAddressHash[T]) Size() int
type WeightNode ¶
type WeightedRandom ¶
func NewWeightedRandom ¶
func NewWeightedRandom[T Hashable, I Instance[T]]() *WeightedRandom[T, I]
func (*WeightedRandom[T, I]) Add ¶
func (wr *WeightedRandom[T, I]) Add(instances ...I) int
func (*WeightedRandom[T, I]) Del ¶
func (wr *WeightedRandom[T, I]) Del(instances ...I) int
func (*WeightedRandom[T, I]) ForEach ¶
func (wr *WeightedRandom[T, I]) ForEach(callback func(T, I) bool)
func (*WeightedRandom[T, I]) Get ¶
func (wr *WeightedRandom[T, I]) Get(key T) (I, bool)
func (*WeightedRandom[T, I]) Select ¶
func (wr *WeightedRandom[T, I]) Select() (ins I)
func (*WeightedRandom[T, I]) Size ¶
func (wr *WeightedRandom[T, I]) Size() int
type WeightedRoundRobin ¶
type WeightedRoundRobin[T Hashable, I Instance[T]] struct { // contains filtered or unexported fields }
func NewWeightRoundRobin ¶
func NewWeightRoundRobin[T Hashable, I Instance[T]]() *WeightedRoundRobin[T, I]
func (*WeightedRoundRobin[T, I]) Add ¶
func (wrr *WeightedRoundRobin[T, I]) Add(instances ...I) int
func (*WeightedRoundRobin[T, I]) Del ¶
func (wrr *WeightedRoundRobin[T, I]) Del(instances ...I) int
func (*WeightedRoundRobin[T, I]) ForEach ¶
func (wrr *WeightedRoundRobin[T, I]) ForEach(callback func(T, I) bool)
func (*WeightedRoundRobin[T, I]) Get ¶
func (wrr *WeightedRoundRobin[T, I]) Get(key T) (I, bool)
func (*WeightedRoundRobin[T, I]) Select ¶
func (wrr *WeightedRoundRobin[T, I]) Select() (ins I)
func (*WeightedRoundRobin[T, I]) Size ¶
func (wrr *WeightedRoundRobin[T, I]) Size() int
type XorShift64 ¶
type XorShift64 struct {
// contains filtered or unexported fields
}
func NewXorShift64 ¶
func NewXorShift64(seed uint64) XorShift64
func (*XorShift64) Int31 ¶
func (xs *XorShift64) Int31() int32
func (*XorShift64) Int63 ¶
func (xs *XorShift64) Int63() int64
func (*XorShift64) Seed ¶
func (xs *XorShift64) Seed(seed uint64)
func (*XorShift64) Uint64 ¶
func (xs *XorShift64) Uint64() uint64