leaky

package module
v1.0.0-alpha Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2021 License: MIT Imports: 4 Imported by: 0

README

Leaky Bucket

Download

go get github.com/twiny/leaky

Example

package main

import (
	"fmt"
	"time"

	"github.com/twiny/leaky"
)

func main() {
	limiter := leaky.NewMutexLimiter(1, 2*time.Second)

	for i := 0; i < 1000; i++ {
		// Take() hold/sleep for the
		// required duration.
		limiter.Take()

		fmt.Println(i)
	}
}
// output
/*
    0
    1 // 2 second
    2 // 2 second
    3 // 2 second
*/

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Clock

type Clock interface {
	Now() time.Time
	Sleep(time.Duration)
}

Clock is the minimum necessary interface to instantiate a rate limiter with a clock or mock clock, compatible with clocks created using github.com/andres-erbsen/clock.

type Limiter

type Limiter interface {
	Take() time.Time // Take should block to make sure that the rate is met.
	Rate() int
	Duration() time.Duration
	String() string
}

Limiter is used to rate-limit some process, possibly across goroutines. The process is expected to call Take() before every iteration, which may block to throttle the goroutine.

func NewMutexLimiter

func NewMutexLimiter(rate int, interval time.Duration) Limiter

New returns a Limiter that will limit to the given RPS.

Jump to

Keyboard shortcuts

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