Documentation
¶
Overview ¶
Package limiters provides a pluggable rate limiting layer for HTTP clients.
Implement the RequestsLimiter interface with any rate limiting strategy (token bucket, sliding window, etc.) and wrap an existing net/http.RoundTripper with NewRoundTripper to enforce per-host request limits at the transport level. The resulting RoundTripper is safe for concurrent use and integrates transparently with the resilient client in the parent github.com/altessa-s/go-atlas/transport/http/client package via [client.WithLimiter].
Example:
rt := limiters.NewRoundTripper(http.DefaultTransport, myLimiter)
client := &http.Client{Transport: rt}
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type RequestsLimiter ¶
type RequestsLimiter interface {
// Allow checks if a request for the given key is allowed under the current rate limit.
// The key is typically a hostname, but can be any identifier for grouping requests.
// Returns an error if the request should be denied due to rate limiting.
//
// Example implementation might limit requests to 100 per minute per hostname:
//
// err := limiter.Allow(ctx, "api.example.com")
// if err != nil {
// // Request denied due to rate limit
// }
Allow(ctx context.Context, key string) error
}
RequestsLimiter is the interface for client-side request rate limiting. Implementations must be safe for concurrent use. The key supplied to RequestsLimiter.Allow is typically the target hostname, but can be any string used to partition rate limits.
type RoundTripper ¶
type RoundTripper struct {
// contains filtered or unexported fields
}
RoundTripper implements net/http.RoundTripper by checking a RequestsLimiter before forwarding each request to the next transport. When the limiter denies a request, RoundTrip returns immediately with an error and does not contact the server.
func NewRoundTripper ¶
func NewRoundTripper(next http.RoundTripper, limiter RequestsLimiter) *RoundTripper
NewRoundTripper creates a RoundTripper that calls limiter.Allow with the request hostname before delegating to next. If next is nil, net/http.DefaultTransport is used.
Example:
limiter := &myRateLimiter{limit: 100}
rt := limiters.NewRoundTripper(http.DefaultTransport, limiter)
client := &http.Client{Transport: rt}
func (*RoundTripper) RoundTrip ¶
RoundTrip checks RequestsLimiter.Allow using the request hostname as key. If the limiter returns a non-nil error the request is rejected without reaching the network; otherwise the request is forwarded to the next net/http.RoundTripper.