Help us shape the future of Go! Take the 2026 Go Developer Survey

roundrobin

package module
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2023 License: MIT Imports: 3 Imported by: 1

README

roundrobin

License Go version Release Checks GoDoc

A Golang Implementation of RoundRobin Algorithm with configurable rotating strategies.

Features

  • Configurable Rotating Strategies
  • Track Stats of Each Item
  • Generic Implementation
  • Concurrency Safe

Example

An Example showing usage of roundrobin as a library is specified below:

package main

import (
	"fmt"

	"github.com/projectdiscovery/roundrobin"
)

func main() {

	ips := []string{
		"192.168.29.58",
		"192.168.29.1",
		"192.168.29.92",
	}

	// create new roundrobin iterator
	rb, err := roundrobin.New(ips...)
	if err != nil {
		panic(err)
	}

	// rb.Add adds given item to sequence
	rb.Add("192.168.29.86")

	for i := 0; i < 10; i++ {
		// rb.Next returns next item in roundrobin fashion
		val := rb.Next().String()
		fmt.Println(val)
	}

	/*
		Output:
		192.168.29.58
		192.168.29.1
		192.168.29.92
		192.168.29.86
		192.168.29.58
		192.168.29.1
		192.168.29.92
		192.168.29.86
		192.168.29.58
		192.168.29.1
	*/

}

For more details refer GoDoc .

Acknowledgement

Inspired by https://github.com/hlts2/round-robin

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultOptions = Options{
	RotateAmount: 1,
}
View Source
var ErrNoItems = errors.New("no items")

ErrNoItems specified for the algorithm

Functions

This section is empty.

Types

type Item

type Item struct {
	Stats Stats
	// contains filtered or unexported fields
}

Item stores value & stats of each element in RoundRobin

func (Item) String

func (i Item) String() string

String returns value of item

type Options

type Options struct {
	RotateAmount int32
}

Options for RoundRobin

type RoundRobin

type RoundRobin struct {
	Options Options
	// contains filtered or unexported fields
}

RoundRobin iterates over the items in a round robin fashion

func New

func New(items ...string) (*RoundRobin, error)

New returns a new RoundRobin structure

func NewWithOptions

func NewWithOptions(options Options, items ...string) (*RoundRobin, error)

New returns a new RoundRobin structure with custom options

func (*RoundRobin) Add

func (r *RoundRobin) Add(items ...string)

Next returns next item

func (*RoundRobin) Next

func (r *RoundRobin) Next() Item

Next returns next item

type Stats

type Stats struct {
	Count int32
}

Stats track frequency of item in RoundRobin

func (*Stats) Inc

func (s *Stats) Inc(value int32)

Inc increments Stats counter

func (*Stats) Reset

func (s *Stats) Reset()

Resets Stats counter

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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