reservoir

package module
v0.0.0-...-3b59b64 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2015 License: MIT Imports: 2 Imported by: 0

README

Reservoir

Software License Build Status GoDoc codecov.io

With reservoir you are able to bottle up your function calls in an easy way. Make sure your (or other external services) don't become overloaded by time.

Installation

go get github.com/sbani/reservoir

Usage

The example shows us how to print the numbers with a delay.

import (
    "fmt"
    "github.com/sbani/reservoir"
)

func printInt(i int) {
    fmt.Println(i)
}

func main() {
    // Never more than 1 request running at a time.
    // Wait at least 2s between each request.
    limiter := NewReservoir(1, 2 * time.Second)

    for i := 0; i < 5; i++ {
        limiter.add(printInt, i)
    }

    fmt.Println("This is printed first")

    time.Sleep(7 * 2 * time.Second)
}

See reservoir_test.go for informations about usage as long as there are no docs please.

Roadmap

  • Add http server (API) functionality

Documentation

Index

Constants

View Source
const (
	StrategyLeak     = 1 // Removes the first (oldest) element in queue and adds the new one
	StrategyOverflow = 2 // The added element will be lost.
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Reservoir

type Reservoir struct {
	MaxConcurrent    int           // How many requests can be running at the same time. Default: 0 (unlimited)
	MinTime          time.Duration // How long to wait after launching a request before launching another one. Default: 0ms
	MaxQueueLength   int
	OverflowStrategy int
	Queue            []job
	// contains filtered or unexported fields
}

func NewReservoir

func NewReservoir(maxConcurrent int, minTime time.Duration) *Reservoir

Create a new reservoir struct and start working the queue

func (*Reservoir) Add

func (rv *Reservoir) Add(fn interface{}, args ...interface{})

Add a new call to the queue

func (*Reservoir) LimitQueue

func (rv *Reservoir) LimitQueue(maxQueueLength int, overFlowStrategy int)

Set the queue limit and the strategy whats happens if queue is full

func (*Reservoir) Start

func (rv *Reservoir) Start()

Start working on the queue

func (*Reservoir) Stop

func (rv *Reservoir) Stop()

Stop working no the queue

Jump to

Keyboard shortcuts

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