Giter VIP home page Giter VIP logo

thegeekyasian / round-robin-go Goto Github PK

View Code? Open in Web Editor NEW
8.0 4.0 1.0 14 KB

round-robin-go is an implementation of round-robin algorithm that allows you to use your resources in a shared rational order. This project uses go generics that enables you to use any data type in your round robin implementation.

License: MIT License

Go 100.00%
go golang load-balancer round-robin load-balancing round-robin-scheduler round-robin-simulator roundrobin

round-robin-go's People

Contributors

superbear avatar thegeekyasian avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

superbear

round-robin-go's Issues

How to assign Array

I have array of workers. Now I want to put these worker in round-robin. How may I pass array in New so that when I do Next task should assign the next worker.

Please help.

Thanks in advance.

Starting from the second round, the first object will be fetched twice

Description

Starting from the second round, the first object will be fetched twice

Code

// go version: 1.18
package main

import (
	"fmt"
	"net/url"

	roundrobin "github.com/thegeekyasian/round-robin-go"
)

func main() {
	rr, _ := roundrobin.New(
		&url.URL{Host: "192.168.0.1"},
		&url.URL{Host: "192.168.0.2"},
		&url.URL{Host: "192.168.0.3"},
		&url.URL{Host: "192.168.0.4"},
	)

	for i := 0; i < 20; i++ {
		fmt.Println(rr.Next().Host)
	}
}

// output
192.168.0.1
192.168.0.2
192.168.0.3
192.168.0.4
192.168.0.1
192.168.0.1

192.168.0.2
192.168.0.3
192.168.0.4
192.168.0.1
192.168.0.1

Why?

when the next field is greater than the length of the object, the first object will be fetched and next will be reset to 0. The next time the Next method is called, next field will be 1, so the first object will be fetched.

// Next returns the next object.
func (r *RoundRobin[O]) Next() *O {
	n := atomic.AddUint32(&r.next, 1)

	if int(n) > len(r.objects) {
		atomic.StoreUint32(&r.next, 0) // reset next field
		n = 1
	}
	return r.objects[(int(n)-1)%len(r.objects)]
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.