weight

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 6, 2022 License: MIT Imports: 0 Imported by: 0

README

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Backend

type Backend struct {
	Name          string
	Weight        int
	CurrentWeight int
}

Backend hold information about backend server

type Weight

type Weight struct {
	Items []*Backend
}

Weight list of all backend server

func NewWeight

func NewWeight() *Weight

NewWeight returns Weight struct

func (*Weight) AddWeight

func (w *Weight) AddWeight(name string, weight int)

AddWeight add server and weight information

func (*Weight) Next

func (w *Weight) Next() string

Next returns next selected server

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL