limiter

package
v0.0.0-...-9228ce8 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package limiter provides concurrency limiters.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LimitListener

func LimitListener(l net.Listener, n int) net.Listener

LimitListener returns a Listener that accepts at most n simultaneous connections from the provided Listener and will drop extra connections.

Types

type Fixed

type Fixed chan struct{}

Fixed is a simple channel-based concurrency limiter. It uses a fixed size channel to limit callers from proceeding until there is a value available in the channel. If all are in-use, the caller blocks until one is freed.

func NewFixed

func NewFixed(limit int) Fixed

func (Fixed) Available

func (t Fixed) Available() int

Available returns the number of available tokens that may be taken.

func (Fixed) Capacity

func (t Fixed) Capacity() int

Capacity returns the number of tokens can be taken.

func (Fixed) Idle

func (t Fixed) Idle() bool

Idle returns true if the limiter has all its capacity is available.

func (Fixed) Release

func (t Fixed) Release()

Release releases a token back to the limiter.

func (Fixed) Take

func (t Fixed) Take(ctx context.Context) error

Take attempts to take a token and blocks until one is available OR until the given context is cancelled.

func (Fixed) TryTake

func (t Fixed) TryTake() bool

TryTake attempts to take a token and return true if successful, otherwise returns false.

Jump to

Keyboard shortcuts

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