Rate limiting is a technique used to control the rate of requests made to a system or service. It helps prevent performance degradation and potential system crashes caused by an overwhelming number of requests. The rate_limiter
in the project “jaeger-lib” is implemented in Go and can be found in the file utils/rate_limiter.go
.
The rate_limiter
uses a token bucket algorithm to manage the rate of requests. This algorithm involves a bucket with a fixed size that can hold a certain number of tokens. Each request made to the system consumes one token from the bucket. The bucket is refilled with tokens at a fixed rate, and if there are not enough tokens in the bucket to handle a request, the request is denied or delayed until enough tokens are available.
Here are some possible options for the rate_limiter
and examples for each option:
NewRateLimiter(rate int, burst int) *RateLimiter
: This creates a new rate limiter with the specified rate (in requests per second) and burst (maximum number of requests that can be made at once). For example,NewRateLimiter(10, 5)
creates a rate limiter that allows up to 10 requests per second with a burst limit of 5.Allow() bool
: This method checks if a request can be made based on the current rate limit. It returnstrue
if the request can be made andfalse
if it cannot. For example,limiter.Allow()
checks if a request can be made using the current rate limiter.Wait()
: This method blocks the current goroutine until it is allowed to make a request based on the current rate limit. For example,limiter.Wait()
blocks the current goroutine until it is allowed to make a request.
The rate_limiter
also includes a test file utils/rate_limiter_test.go
with examples of how to use the rate limiter and test its functionality.
Sources:
- https://www.envoyproxy.io/docs/envoy/latest/api-v3/extensions/common/ratelimit/v3/ratelimit.proto
- https://www.envoyproxy.io/docs/envoy/latest/api-v3/service/ratelimit/v3/rls.proto
- https://github.com/jaegertracing/jaeger-lib/blob/master/utils/rate_limiter.go
- https://github.com/jaegertracing/jaeger-lib/blob/master/utils/rate_limiter_test.go