lba (LoadBalancer Algorithm) is a go implementation of the balancing algorithm.
- Round Robin
- IP Hash
- Least Connections
- ...updating...
First, install Go, and install lba package:
go get -u github.com/ductnn/lba
Then, import package in your code:
import "github.com/ductnn/lba"
- Round Robin:
package main
import (
"net/url"
roundrobin "github.com/stsmdt/round-robin"
)
func main() {
rr, _ := roundrobin.New(
[]url.URL{
{Host: "192.168.1.1"},
{Host: "192.168.1.2"},
{Host: "192.168.1.3"},
{Host: "192.168.1.4"},
{Host: "192.168.1.5"},
},
)
rr.Next() // {Host: "192.168.1.1"}
rr.Next() // {Host: "192.168.1.2"}
rr.Next() // {Host: "192.168.1.3"}
rr.Next() // {Host: "192.168.1.4"}
rr.Next() // {Host: "192.168.1.5"}
rr.Next() // {Host: "192.168.1.1"}
}
- Least Connections:
lc, err := New([]*url.URL{
{Host: "192.168.1.10"},
{Host: "192.168.1.11"},
{Host: "192.168.1.12"},
})
src1, done1 := lc.Next() // {Host: "192.168.1.10"}
src2, done2 := lc.Next() // {Host: "192.168.1.11"}
done1() // Reduce connection of src1
src3, done3 := lc.Next() // {Host: "192.168.1.10"}
- IP Hash:
ip, _ := iphash.New([]*url.URL{
{Host: "192.168.1.10"},
{Host: "192.168.1.11"},
{Host: "192.168.1.12"},
})
ip.Next(&url.URL{Host: "192.168.1.10"}) // {Host: "192.168.1.10"}
ip.Next(&url.URL{Host: "192.168.1.10"}) // {Host: "192.168.1.10"}
ip.Next(&url.URL{Host: "192.168.1.44"}) // {Host: "192.168.1.11"}
ip.Next(&url.URL{Host: "192.168.1.44"}) // {Host: "192.168.1.11"}