nothinux / weight

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

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

About

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

License:MIT License


Languages

Language:Go 100.0%