robin

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 7, 2023 License: MIT Imports: 1 Imported by: 0

README

AtomicGo | robin

Downloads Latest Release Tests Coverage Unit test count Go report


Documentation | Contributing | Code of Conduct


AtomicGo

go get atomicgo.dev/robin

robin

import "atomicgo.dev/robin"

Package robin is a simple, generic and thread-safe 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.

Thread-safety is achieved by using atomic operations amd guarantees that two concurrent calls to Loadbalancer.Next will not return the same item, if the slice contains more than one item.

Benchmark:

BenchmarkLoadbalancer_Next              252151534                4.746 ns/op           0 B/op          0 allocs/op
BenchmarkLoadbalancer_Next-2            254281032                4.758 ns/op           0 B/op          0 allocs/op
BenchmarkLoadbalancer_Next-4            253424396                4.738 ns/op           0 B/op          0 allocs/op
BenchmarkLoadbalancer_Next-8            254842484                4.752 ns/op           0 B/op          0 allocs/op
BenchmarkLoadbalancer_Next-16           247016046                4.785 ns/op           0 B/op          0 allocs/op
BenchmarkLoadbalancer_Next-32           250539441                4.774 ns/op           0 B/op          0 allocs/op

Index

type Loadbalancer

Loadbalancer is a simple, generic and thread-safe round-robin load balancer for Go.

type Loadbalancer[T any] struct {
    Items []T
    // contains filtered or unexported fields
}
func NewLoadbalancer
func NewLoadbalancer[T any](items []T) *Loadbalancer[T]

NewLoadbalancer creates a new Loadbalancer. 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

package main

import (
	"atomicgo.dev/robin"
	"fmt"
)

func main() {
	set := []string{"object1", "object2", "object3"}
	lb := robin.NewLoadbalancer(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

package main

import (
	"atomicgo.dev/robin"
	"fmt"
)

func main() {
	set := []int{1, 2, 3}
	lb := robin.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

package main

import (
	"atomicgo.dev/robin"
	"fmt"
)

func main() {
	set := []int{1, 2, 3}
	lb := robin.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

package main

import (
	"atomicgo.dev/robin"
	"fmt"
)

func main() {
	set := []int{1, 2, 3}
	lb := robin.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

package main

import (
	"atomicgo.dev/robin"
	"fmt"
)

func main() {
	set := []int{1, 2, 3, 4, 5, 6}
	lb := robin.NewLoadbalancer(set)

	lb.Next()
	lb.Next()
	lb.Next()

	lb.Reset()

	fmt.Println(lb.Current())

}
Output
1

Generated by gomarkdoc


AtomicGo.dev  ·  with ❤️ by @MarvinJWendt | MarvinJWendt.com

Documentation

Overview

Package robin is a simple, generic and thread-safe 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.

Thread-safety is achieved by using atomic operations amd guarantees that two concurrent calls to Loadbalancer.Next will not return the same item, if the slice contains more than one item.

Benchmark:

BenchmarkLoadbalancer_Next              252151534                4.746 ns/op           0 B/op          0 allocs/op
BenchmarkLoadbalancer_Next-2            254281032                4.758 ns/op           0 B/op          0 allocs/op
BenchmarkLoadbalancer_Next-4            253424396                4.738 ns/op           0 B/op          0 allocs/op
BenchmarkLoadbalancer_Next-8            254842484                4.752 ns/op           0 B/op          0 allocs/op
BenchmarkLoadbalancer_Next-16           247016046                4.785 ns/op           0 B/op          0 allocs/op
BenchmarkLoadbalancer_Next-32           250539441                4.774 ns/op           0 B/op          0 allocs/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
	// contains filtered or unexported fields
}

Loadbalancer is a simple, generic and thread-safe round-robin load balancer for Go.

func NewLoadbalancer

func NewLoadbalancer[T any](items []T) *Loadbalancer[T]

NewLoadbalancer creates a new Loadbalancer. 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
package main

import (
	"atomicgo.dev/robin"
	"fmt"
)

func main() {
	set := []string{"object1", "object2", "object3"}
	lb := robin.NewLoadbalancer(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
package main

import (
	"atomicgo.dev/robin"
	"fmt"
)

func main() {
	set := []int{1, 2, 3}
	lb := robin.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
package main

import (
	"atomicgo.dev/robin"
	"fmt"
)

func main() {
	set := []int{1, 2, 3}
	lb := robin.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
package main

import (
	"atomicgo.dev/robin"
	"fmt"
)

func main() {
	set := []int{1, 2, 3}
	lb := robin.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
package main

import (
	"atomicgo.dev/robin"
	"fmt"
)

func main() {
	set := []int{1, 2, 3, 4, 5, 6}
	lb := robin.NewLoadbalancer(set)

	lb.Next()
	lb.Next()
	lb.Next()

	lb.Reset()

	fmt.Println(lb.Current())

}
Output:

1

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL