Documentation ¶
Index ¶
- Variables
- func LimitConcurrencyByRepo(ctx context.Context) string
- func WithConcurrencyLimiters(cfg config.Cfg, middleware *LimiterMiddleware)
- type ConcurrencyLimiter
- type ConcurrencyMonitor
- type GetLockKey
- type LimitedFunc
- type Limiter
- type LimiterMiddleware
- type QueueTickerCreator
- type RateLimiter
- type SetupFunc
Constants ¶
This section is empty.
Variables ¶
var ErrMaxQueueSize = errors.New("maximum queue size reached")
ErrMaxQueueSize indicates the concurrency queue has reached its maximum size
var ErrMaxQueueTime = errors.New("maximum time in concurrency queue reached")
ErrMaxQueueTime indicates a request has reached the maximum time allowed to wait in the concurrency queue.
var ErrRateLimit = errors.New("rate limit reached")
ErrRateLimit is returned when RateLimiter determined a request has breached the rate request limit.
Functions ¶
func LimitConcurrencyByRepo ¶
LimitConcurrencyByRepo implements GetLockKey by using the repository path as lock.
func WithConcurrencyLimiters ¶
func WithConcurrencyLimiters(cfg config.Cfg, middleware *LimiterMiddleware)
WithConcurrencyLimiters sets up middleware to limit the concurrency of requests based on RPC and repository
Types ¶
type ConcurrencyLimiter ¶
type ConcurrencyLimiter struct {
// contains filtered or unexported fields
}
ConcurrencyLimiter contains rate limiter state
func NewConcurrencyLimiter ¶
func NewConcurrencyLimiter(perKeyLimit, globalLimit int, maxWaitTickerGetter QueueTickerCreator, monitor ConcurrencyMonitor) *ConcurrencyLimiter
NewConcurrencyLimiter creates a new concurrency rate limiter
func (*ConcurrencyLimiter) Limit ¶
func (c *ConcurrencyLimiter) Limit(ctx context.Context, lockKey string, f LimitedFunc) (interface{}, error)
Limit will limit the concurrency of f
type ConcurrencyMonitor ¶
type ConcurrencyMonitor interface { Queued(ctx context.Context) Dequeued(ctx context.Context) Enter(ctx context.Context, acquireTime time.Duration) Exit(ctx context.Context) Dropped(ctx context.Context, message string) }
ConcurrencyMonitor allows the concurrency monitor to be observed
type GetLockKey ¶
GetLockKey function defines the lock key of an RPC invocation based on its context
type LimitedFunc ¶
type LimitedFunc func() (resp interface{}, err error)
LimitedFunc represents a function that will be limited
type Limiter ¶
type Limiter interface {
Limit(ctx context.Context, lockKey string, f LimitedFunc) (interface{}, error)
}
Limiter limits incoming requests
type LimiterMiddleware ¶
type LimiterMiddleware struct {
// contains filtered or unexported fields
}
LimiterMiddleware contains rate limiter state
func New ¶
func New(cfg config.Cfg, getLockKey GetLockKey, setupMiddleware SetupFunc) *LimiterMiddleware
New creates a new middleware that limits requests. SetupFunc sets up the middlware with a specific kind of limiter.
func (*LimiterMiddleware) Collect ¶
func (c *LimiterMiddleware) Collect(metrics chan<- prometheus.Metric)
Collect is used to collect Prometheus metrics.
func (*LimiterMiddleware) Describe ¶
func (c *LimiterMiddleware) Describe(descs chan<- *prometheus.Desc)
Describe is used to describe Prometheus metrics.
func (*LimiterMiddleware) StreamInterceptor ¶
func (c *LimiterMiddleware) StreamInterceptor() grpc.StreamServerInterceptor
StreamInterceptor returns a Stream Interceptor
func (*LimiterMiddleware) UnaryInterceptor ¶
func (c *LimiterMiddleware) UnaryInterceptor() grpc.UnaryServerInterceptor
UnaryInterceptor returns a Unary Interceptor
type QueueTickerCreator ¶
QueueTickerCreator is a function that provides a ticker
type RateLimiter ¶
type RateLimiter struct {
// contains filtered or unexported fields
}
RateLimiter is an implementation of Limiter that puts a hard limit on the number of requests per second
func NewRateLimiter ¶
func NewRateLimiter( refillInterval time.Duration, burst int, ticker helper.Ticker, requestsDroppedMetric prometheus.Counter, ) *RateLimiter
NewRateLimiter creates a new instance of RateLimiter
func (*RateLimiter) Limit ¶
func (r *RateLimiter) Limit(ctx context.Context, lockKey string, f LimitedFunc) (interface{}, error)
Limit rejects an incoming reequest if the maximum number of requests per second has been reached
func (*RateLimiter) PruneUnusedLimiters ¶
func (r *RateLimiter) PruneUnusedLimiters(ctx context.Context)
PruneUnusedLimiters enters an infinite loop to periodically check if any limiters can be cleaned up. This is meant to be called in a separate goroutine.
type SetupFunc ¶
type SetupFunc func(cfg config.Cfg, middleware *LimiterMiddleware)
SetupFunc set up a middleware to limiting requests
func WithRateLimiters ¶
WithRateLimiters sets up a middleware with limiters that limit requests based on its rate per second per RPC