rate

package module
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: May 7, 2024 License: MIT Imports: 3 Imported by: 1

README

build coverage goreport Docs

An efficient rate limiter for Go

Because too much of the CPU consumed was golang.org/x/time/rate.Limiter.Wait() calling time.Now().

Documentation

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
}

Limiter provides efficient rate limiting. The zero value is immediately usable.

A Limiter is not safe to use from multiple goroutines simultaneously.

func (*Limiter) Wait

func (rl *Limiter) Wait(maxrate *int32) (slept time.Duration)

Wait sleeps at least long enough to ensure that Wait cannot be called more than `*maxrate` times per second.

Returns the duration slept.

A nil `maxrate` or a `*maxrate` of zero or less doesn't wait at all.

type Ticker added in v1.3.0

type Ticker struct {
	C <-chan struct{} // sends a struct{}{} at most maxrate times per second
	// contains filtered or unexported fields
}

func NewSubTicker added in v1.1.0

func NewSubTicker(parent <-chan struct{}, maxrate *int32, counter *uint64) *Ticker

NewSubTicker returns a channel that reads from another struct{}{} channel and then sends a `struct{}{}` at most `*maxrate` times per second, but that cannot exceed the parent tick rate.

If counter is not nil, it is incremented every time a send is successful.

Use this to make "background" tickers that are less prioritized.

The Ticker is closed when the parent channel closes.

func NewTicker

func NewTicker(maxrate *int32, counter *uint64) *Ticker

NewTicker returns a Ticker that sends a `struct{}{}` at most `*maxrate` times per second on it's C channel.

If counter is not nil, it is incremented every time a send is successful.

A nil `maxrate` or a `*maxrate` of zero or less sends as quickly as possible.

func (*Ticker) Close added in v1.3.0

func (ticker *Ticker) Close()

Close stops the Ticker and frees resources.

It is safe to call multiple times or concurrently.

func (*Ticker) Closed added in v1.6.0

func (ticker *Ticker) Closed() (yes bool)

Closed returns true if the Ticker is closed.

func (*Ticker) Wait added in v1.5.0

func (ticker *Ticker) Wait()

Wait delays until the next tick is available.

Jump to

Keyboard shortcuts

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