Skip to content

Repository files navigation

LBA

Run TestsGo Report Card

lba (LoadBalancer Algorithm) is a go implementation of the balancing algorithm.

  • Round Robin
  • IP Hash
  • Least Connections
  • ...updating...

Installation

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"

Example

  • Round Robin:
package main
import (
"net/url"
roundrobin "github.com/stsmdt/round-robin"
)
funcmain() {
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 src1src3, 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"}

About

A go implementation of the balancing algorithm

Topics

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages