ratelimitedworker

package module
v0.0.0-...-e9d4d9c Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2023 License: Apache-2.0 Imports: 6 Imported by: 0

README

Ratelimited

Just a simple wrapper over ratelimit. :)

Example

See a working example here. But here's the idea:

func main() {
	const targetRPM int = 100
    queue := make(chan (string), 25)
    /*
    This will create an instance of the ratelimitedworker.
    - id: primary identifier for the ratelimiter Job the work is assosiated to
    - targetRPM: the expected maximum rate limit to perform at
    - hasty: if true maintains a higher RPS good for work() that takes long, false forces rate below the limit.
    - verbose: enable logging
    */
    rlw := ratelimitedworker.Create("sampleId", targetRPM, true, true)
    go queueLoader(queue)
    go doLotsOfWork(rlw,queue)
}

func doLotsOfWork(rlw *ratelimitedworker.RLW, queue chan (string)) {

	for range queue {
		// this will allow the work() to be called only rlw.targetRPM number of times during a minute
		rlw.Track() // if limit is hit for the time period it will block this go routine
		go work()
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RLW

type RLW struct {
	ID        string
	RL        ratelimit.Limiter
	TargetRPM uint32
	// contains filtered or unexported fields
}

func Create

func Create(
	id string,
	targetRPM int,
	hasty bool,
	verbose bool,
) RLW

This will create an instance of the ratelimitedworker.

- id: primary identifier for the ratelimiter Job the work is assosiated to

- targetRPM: the expected maximum rate limit to perform at

- hasty: if true maintains a higher RPS good for work() that takes long, false forces rate below the limit.

- verbose: enable logging

func (*RLW) Track

func (rlw *RLW) Track()

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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