doevery

package
v1.20.0 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2023 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

package doevery provides primitives for per-call-site rate-limiting.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func TimePeriod

func TimePeriod(dur time.Duration, f func())

TimePeriod rate limits each call site of this by the duration specified as the first argument. This may be useful in logging scenarios, where you only want to log every few seconds - or every second - instead of tens of hundreds of times per second.

Each unique call site that calls TimePeriod is rate-limited independently. Each invocation of TimePeriod at the same call-site should provide the same duration.

Example usage:

end := time.Now().Add(5 * time.Second)
for end.After(time.Now()) {
	doevery.TimePeriod(1*time.Second, func() {
		fmt.Println("This will only appear once per second.")
	})
}

Please note that each individual thread does not have a distinct rate-limit; the rate-limit is global for the file/line.

TimePeriod is safe for concurrent use.

Types

This section is empty.

Jump to

Keyboard shortcuts

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