/round-robin-go

round-robin-go is an implementation of round-robin algorithm that allows you to use your resources in a shared rational order. This project uses go generics that enables you to use any data type in your round robin implementation.

Primary LanguageGoMIT LicenseMIT

round-robin-go

round-robin-go is a round robin balancing algorithm written in golang.

The project uses go-generics, that allows you to use round-robin for any data type. With help of go generics, it has become easier for you to plugin roundrobin to your project.

Installation

go get github.com/thegeekyasian/round-robin-go

Version:

1.18

Since Generics were introduced in go with version 1.18, the project requires go 1.18 to work.

Usage

any type

type resource struct {
    id   int
    name string
}

...

rr, _ := roundrobin.New(
    &resource{1, "resource-1"},
    &resource{2, "resource-2"},
    &resource{3, "resource-3"},
)

rr.Next() // resource-1
rr.Next() // resource-2
rr.Next() // resource-3
rr.Next() // resource-1

string

one := "One"
two := "Two"
three := "Three"

rr, _ := roundrobin.New(&one, &two, &three)

rr.Next()	// One
rr.Next()	// Two
rr.Next()	// Three
rr.Next()	// One

URLs

rr, _ := roundrobin.New(
    &url.URL{Host: "192.168.0.1"},
    &url.URL{Host: "192.168.0.2"},
    &url.URL{Host: "192.168.0.3"},
    &url.URL{Host: "192.168.0.4"},
)

rr.Next() // 192.168.0.1
rr.Next() // 192.168.0.2
rr.Next() // 192.168.0.3
rr.Next() // 192.168.0.4
rr.Next() // 192.168.0.1
rr.Next() // 192.168.0.2

Author

License

round-robin-go is released under MIT license (2023).