Skip to content

⚡️ weighted round-robin load balancing algorithm library for Go

License

Notifications You must be signed in to change notification settings

nothinux/weight

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Weight

Go Report Card test status

This package implement weighted round-robin load balancing algorithm, inspired from nginx.

It's works by increase all current_weight by their respective weight, then choose backend which has the largest current_weight, after that the previously selected backend will reduce its current_weight by total_weight, and this process will repeat every Next method called.

In case you have 3 backend server A, B, C and its weight 2, 3, 2 the selected server will -> { B, A, C, B, A, C, B }

Documentation

see pkg.go.dev

Installation

$ go get github.com/nothinux/weight

Getting Started

package main

import (
    "github.com/nothinux/weight"
    "log"
)

func main() {
    w := weight.NewWeight()
    w.AddWeight("A", 2)
    w.AddWeight("B", 3)
    w.AddWeight("C", 2)
    
    for i := 0; i < 7; i++ {
        log.Println(w.Next())
    }
}

LICENSE

MIT