package utils

import "github.com/uber/jaeger-lib/utils"

Index

Types

type RateLimiter

type RateLimiter interface {
	CheckCredit(itemCost float64) bool
}

RateLimiter is a filter used to check if a message that is worth itemCost units is within the rate limits.

func NewRateLimiter

func NewRateLimiter(creditsPerSecond, maxBalance float64) RateLimiter

NewRateLimiter creates a new 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.

Source Files

rate_limiter.go

Version
v2.4.1+incompatible (latest)
Published
Mar 29, 2021
Platform
linux/amd64
Imports
2 packages
Last checked
3 hours ago

Tools for package owners.