项目作者: liangwt

项目描述 :
implementation of weighted round-robin and smooth weighted round-robin
高级语言: Go
项目地址: git://github.com/liangwt/wrr.git
创建时间: 2019-05-29T02:55:04Z
项目社区:https://github.com/liangwt/wrr

开源协议:Apache License 2.0

下载


Weighted Round-Robin

Implementation of weighted round-robin and smooth weighted round-robin

Normal weighted round-robin was referenced by LVS weighted round-robin

Smooth weighted round-robin was referenced by nginx smooth weighted round-robin balancing

As the name implies, smooth weighted round-robin algorithm avoids the choice of the same point at a certain moment.

Well known, time complexity of both is O(n) when looking for the next element. In this implementation, cache will set while first looping, O(1) time complexity after that

USAGE

  1. go get github.com/liangwt/wrr
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/liangwt/wrr"
  5. )
  6. func main() {
  7. points := []*wrr.Point{
  8. {Entry: "A", Weight: 5},
  9. {Entry: "B", Weight: 2},
  10. {Entry: "C", Weight: 3},
  11. }
  12. iter := wrr.NewWrr(points)
  13. for i := 0; i < 12; i++ {
  14. fmt.Printf("%s ", iter.Next().Entry)
  15. }
  16. fmt.Println()
  17. smIter := wrr.NewSmoothWrr(points)
  18. for i := 0; i < 12; i++ {
  19. fmt.Printf("%s ", smIter.Next().Entry)
  20. }
  21. }

FEATURE

  • O(1) time complexity, benefit from cache
  • test cover