utils

package
v0.40.0 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2024 License: Apache-2.0, Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetJSON

func GetJSON(url string, out interface{}) error

GetJSON makes an HTTP call to the specified URL and parses the returned JSON into `out`.

func ReadJSON

func ReadJSON(resp *http.Response, out interface{}) error

ReadJSON reads JSON from http.Response and parses it into `out`.

Types

type RateLimiter

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

RateLimiter is a rate limiter based on leaky bucket algorithm, formulated in terms of a credits balance that is replenished every time CheckCredit() method is called (tick) by the amount proportional to the time elapsed since the last tick, up to max of creditsPerSecond. A call to CheckCredit() takes a cost of an item we want to pay with the balance. If the balance exceeds the cost of the item, the item is "purchased" and the balance reduced, indicated by returned value of true. Otherwise the balance is unchanged and return false.

This can be used to limit a rate of messages emitted by a service by instantiating the Rate Limiter with the max number of messages a service is allowed to emit per second, and calling CheckCredit(1.0) for each message to determine if the message is within the rate limit.

It can also be used to limit the rate of traffic in bytes, by setting creditsPerSecond to desired throughput as bytes/second, and calling CheckCredit() with the actual message size.

func NewRateLimiter

func NewRateLimiter(creditsPerSecond, maxBalance float64) *RateLimiter

NewRateLimiter creates a new RateLimiter.

func (*RateLimiter) CheckCredit

func (rl *RateLimiter) CheckCredit(itemCost float64) bool

CheckCredit tries to reduce the current balance by itemCost provided that the current balance is not lest than itemCost.

func (*RateLimiter) Update

func (rl *RateLimiter) Update(creditsPerSecond, maxBalance float64)

Update changes the main parameters of the rate limiter in-place, while retaining the current accumulated balance (pro-rated to the new maxBalance value). Using this method instead of creating a new rate limiter helps to avoid thundering herd when sampling strategies are updated.

Jump to

Keyboard shortcuts

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