limiter

package
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2020 License: Apache-2.0 Imports: 1 Imported by: 15

Documentation

Overview

Package limiter implements a concurrency limiter with support for contexts.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Limiter

type Limiter struct {
	// contains filtered or unexported fields
}

A Limiter enforces concurrency limits among a set of goroutines. It maintains a bucket of tokens; a number of tokens (e.g., representing the cost of an operation) must be acquired by a goroutine before proceeding. A limiter is not fair: tokens are not granted in FIFO order; rather, waiters are picked randomly to be granted new tokens.

A nil limiter issues an infinite number of tokens.

func New

func New() *Limiter

New creates a new limiter with 0 tokens.

func (*Limiter) Acquire

func (l *Limiter) Acquire(ctx context.Context, need int) error

Acquire blocks until the goroutine is granted the desired number of tokens, or until the context is done.

func (*Limiter) Release

func (l *Limiter) Release(n int)

Release adds a number of tokens back into the limiter.

type LimiterIfc added in v0.0.2

type LimiterIfc interface {
	Release(n int)
	Acquire(ctx context.Context, need int) error
}

Jump to

Keyboard shortcuts

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