Documentation
¶
Overview ¶
Package robin is a simple, generic round-robin load balancer for Go.
It can be used to load balance any type of data. It is not limited to HTTP requests.
Robin takes any slice as an input and returns the next item in the slice. When the end of the slice is reached, it starts again from the beginning.
There are two versions of Robin: a thread-safe version (NewThreadSafeLoadbalancer) and a non-thread-safe (NewLoadbalancer) version. The thread-safe version is slower than the non-thread-safe version, but it is guaranteed that two concurrent calls to Loadbalancer.Next will not return the same item, if the slice contains more than one item.
Benchmark:
BenchmarkLoadbalancer_Next 225866620 5.274 ns/op BenchmarkLoadbalancer_Next-2 227712583 5.285 ns/op BenchmarkLoadbalancer_Next-32 228792201 5.273 ns/op BenchmarkLoadbalancer_Next_ThreadSafe 100000000 10.15 ns/op BenchmarkLoadbalancer_Next_ThreadSafe-2 100000000 10.02 ns/op BenchmarkLoadbalancer_Next_ThreadSafe-32 100000000 10.06 ns/op
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Loadbalancer ¶
type Loadbalancer[T any] struct { Items []T ThreadSafe bool // contains filtered or unexported fields }
Loadbalancer is a simple, generic round-robin load balancer for Go.
func NewLoadbalancer ¶
func NewLoadbalancer[T any](items []T) *Loadbalancer[T]
NewLoadbalancer creates a new Loadbalancer. For maximum speed, this is not thread-safe. Use NewThreadSafeLoadbalancer if you need thread-safety. If two goroutines call Loadbalancer.Next at the exact same time, it can happen that they both return the same item.
Example ¶
set := []string{"object1", "object2", "object3"}
lb := NewLoadbalancer(set)
fmt.Println(lb.Current())
Output: object1
func NewThreadSafeLoadbalancer ¶
func NewThreadSafeLoadbalancer[T any](items []T) *Loadbalancer[T]
NewThreadSafeLoadbalancer creates a new Loadbalancer. This is thread-safe, but slower than NewLoadbalancer. It is guaranteed that two concurrent calls to Loadbalancer.Next will not return the same item, if the slice contains more than one item.
Example ¶
set := []string{"object1", "object2", "object3"}
lb := NewThreadSafeLoadbalancer(set)
fmt.Println(lb.Current())
Output: object1
func (*Loadbalancer[T]) AddItems ¶
func (l *Loadbalancer[T]) AddItems(items ...T)
AddItems adds items to the Loadbalancer.
Example ¶
set := []int{1, 2, 3}
lb := NewLoadbalancer(set)
lb.AddItems(4, 5, 6)
fmt.Println(lb.Items)
Output: [1 2 3 4 5 6]
func (*Loadbalancer[T]) Current ¶
func (l *Loadbalancer[T]) Current() T
Current returns the current item in the slice, without advancing the Loadbalancer.
Example ¶
set := []int{1, 2, 3}
lb := NewLoadbalancer(set)
fmt.Println(lb.Current())
Output: 1
func (*Loadbalancer[T]) Next ¶
func (l *Loadbalancer[T]) Next() T
Next returns the next item in the slice. When the end of the slice is reached, it starts again from the beginning.
Example ¶
set := []int{1, 2, 3}
lb := NewLoadbalancer(set)
for i := 0; i < 10; i++ {
fmt.Println(lb.Next())
}
Output: 1 2 3 1 2 3 1 2 3 1
func (*Loadbalancer[T]) Reset ¶
func (l *Loadbalancer[T]) Reset()
Reset resets the Loadbalancer to its initial state.
Example ¶
set := []int{1, 2, 3, 4, 5, 6}
lb := NewLoadbalancer(set)
lb.Next()
lb.Next()
lb.Next()
lb.Reset()
fmt.Println(lb.Current())
Output: 1