Documentation
¶
Overview ¶
Package throttle provides configurable gRPC interceptors for rate limiting and in-flight request limiting.
This package implements two main interceptors:
- UnaryInterceptor: For unary gRPC calls
- StreamInterceptor: For streaming gRPC calls
Both interceptors support:
- Rate limiting with configurable algorithms (leaky bucket, sliding window)
- In-flight request limiting
- Service method pattern matching
- Identity-based, header-based, and remote address-based key extraction
- Dry-run mode for testing
- Comprehensive configuration through JSON/YAML
Index ¶
- Constants
- func MapstructureDecodeHook() mapstructure.DecodeHookFunc
- func NewPrometheusMetrics(options ...PrometheusMetricsOption) *throttleconfig.PrometheusMetrics
- func StreamInterceptor(cfg *Config, options ...StreamInterceptorOption) (grpc.StreamServerInterceptor, error)
- func UnaryInterceptor(cfg *Config, options ...UnaryInterceptorOption) (grpc.UnaryServerInterceptor, error)
- type Config
- type ConfigOption
- type InFlightLimitZoneConfig
- type MetricsCollector
- type PrometheusMetrics
- type PrometheusMetricsOption
- type RateLimitRetryAfterValue
- type RateLimitValue
- type RateLimitZoneConfig
- type RuleConfig
- type RuleInFlightLimit
- type RuleRateLimit
- type StreamGetKeyFunc
- type StreamInterceptorOption
- func WithStreamGetKeyIdentity(getKeyIdentity StreamGetKeyFunc) StreamInterceptorOption
- func WithStreamInFlightLimitOnError(onError interceptor.InFlightLimitStreamOnErrorFunc) StreamInterceptorOption
- func WithStreamInFlightLimitOnReject(onReject interceptor.InFlightLimitStreamOnRejectFunc) StreamInterceptorOption
- func WithStreamInFlightLimitOnRejectInDryRun(onReject interceptor.InFlightLimitStreamOnRejectFunc) StreamInterceptorOption
- func WithStreamMetricsCollector(collector MetricsCollector) StreamInterceptorOption
- func WithStreamRateLimitOnError(onError interceptor.RateLimitStreamOnErrorFunc) StreamInterceptorOption
- func WithStreamRateLimitOnReject(onReject interceptor.RateLimitStreamOnRejectFunc) StreamInterceptorOption
- func WithStreamRateLimitOnRejectInDryRun(onReject interceptor.RateLimitStreamOnRejectFunc) StreamInterceptorOption
- func WithStreamTags(tags []string) StreamInterceptorOption
- type TagsList
- type UnaryGetKeyFunc
- type UnaryInterceptorOption
- func WithUnaryGetKeyIdentity(getKeyIdentity UnaryGetKeyFunc) UnaryInterceptorOption
- func WithUnaryInFlightLimitOnError(onError interceptor.InFlightLimitUnaryOnErrorFunc) UnaryInterceptorOption
- func WithUnaryInFlightLimitOnReject(onReject interceptor.InFlightLimitUnaryOnRejectFunc) UnaryInterceptorOption
- func WithUnaryInFlightLimitOnRejectInDryRun(onReject interceptor.InFlightLimitUnaryOnRejectFunc) UnaryInterceptorOption
- func WithUnaryMetricsCollector(collector MetricsCollector) UnaryInterceptorOption
- func WithUnaryRateLimitOnError(onError interceptor.RateLimitUnaryOnErrorFunc) UnaryInterceptorOption
- func WithUnaryRateLimitOnReject(onReject interceptor.RateLimitUnaryOnRejectFunc) UnaryInterceptorOption
- func WithUnaryRateLimitOnRejectInDryRun(onReject interceptor.RateLimitUnaryOnRejectFunc) UnaryInterceptorOption
- func WithUnaryTags(tags []string) UnaryInterceptorOption
- type ZoneConfig
- type ZoneKeyConfig
- type ZoneKeyType
Constants ¶
const ( RateLimitAlgLeakyBucket = throttleconfig.RateLimitAlgLeakyBucket RateLimitAlgSlidingWindow = throttleconfig.RateLimitAlgSlidingWindow )
Rate-limiting algorithms.
const ( ZoneKeyTypeNoKey = throttleconfig.ZoneKeyTypeNoKey ZoneKeyTypeIdentity = throttleconfig.ZoneKeyTypeIdentity ZoneKeyTypeHeader = throttleconfig.ZoneKeyTypeHeader ZoneKeyTypeRemoteAddr = throttleconfig.ZoneKeyTypeRemoteAddr )
Zone key types.
Variables ¶
This section is empty.
Functions ¶
func MapstructureDecodeHook ¶
func MapstructureDecodeHook() mapstructure.DecodeHookFunc
MapstructureDecodeHook returns a DecodeHookFunc for mapstructure to handle custom types.
func NewPrometheusMetrics ¶
func NewPrometheusMetrics(options ...PrometheusMetricsOption) *throttleconfig.PrometheusMetrics
NewPrometheusMetrics creates a new instance of PrometheusMetrics with optional configuration. If no options are provided, defaults are used.
func StreamInterceptor ¶
func StreamInterceptor(cfg *Config, options ...StreamInterceptorOption) (grpc.StreamServerInterceptor, error)
StreamInterceptor returns a gRPC stream interceptor that throttles requests based on the provided configuration.
func UnaryInterceptor ¶
func UnaryInterceptor(cfg *Config, options ...UnaryInterceptorOption) (grpc.UnaryServerInterceptor, error)
UnaryInterceptor returns a gRPC unary interceptor that throttles requests based on the provided configuration.
Types ¶
type Config ¶
type Config struct {
// RateLimitZones contains rate limiting zones.
// Key is a zone's name, and value is a zone's configuration.
RateLimitZones map[string]RateLimitZoneConfig `mapstructure:"rateLimitZones" yaml:"rateLimitZones" json:"rateLimitZones"`
// InFlightLimitZones contains in-flight limiting zones.
// Key is a zone's name, and value is a zone's configuration.
InFlightLimitZones map[string]InFlightLimitZoneConfig `mapstructure:"inFlightLimitZones" yaml:"inFlightLimitZones" json:"inFlightLimitZones"` //nolint:lll // struct tags must be on one line
// Rules contains list of so-called throttling rules.
// Basically, throttling rule represents a gRPC service/method pattern,
// and rate/in-flight limiting zones based on which all matched gRPC requests will be throttled.
Rules []RuleConfig `mapstructure:"rules" yaml:"rules" json:"rules"`
// contains filtered or unexported fields
}
Config represents a configuration for throttling of gRPC requests on the server side. Configuration can be loaded in different formats (YAML, JSON) using config.Loader, viper, or with json.Unmarshal/yaml.Unmarshal functions directly.
func NewConfig ¶
func NewConfig(options ...ConfigOption) *Config
NewConfig creates a new instance of the Config.
func (*Config) KeyPrefix ¶
KeyPrefix returns a key prefix with which all configuration parameters should be presented. Implements config.KeyPrefixProvider interface.
func (*Config) Set ¶
func (c *Config) Set(dp config.DataProvider) error
Set sets throttling configuration values from config.DataProvider. Implements config.Config interface.
func (*Config) SetProviderDefaults ¶
func (c *Config) SetProviderDefaults(_ config.DataProvider)
SetProviderDefaults sets default configuration values for logger in config.DataProvider. Implements config.Config interface.
type ConfigOption ¶
type ConfigOption func(*configOptions)
ConfigOption is a type for functional options for the Config.
func WithKeyPrefix ¶
func WithKeyPrefix(keyPrefix string) ConfigOption
WithKeyPrefix returns a ConfigOption that sets a key prefix for parsing configuration parameters. This prefix will be used by config.Loader.
type InFlightLimitZoneConfig ¶
type InFlightLimitZoneConfig struct {
ZoneConfig `mapstructure:",squash" yaml:",inline"`
InFlightLimit int `mapstructure:"inFlightLimit" yaml:"inFlightLimit" json:"inFlightLimit"`
BacklogLimit int `mapstructure:"backlogLimit" yaml:"backlogLimit" json:"backlogLimit"`
BacklogTimeout config.TimeDuration `mapstructure:"backlogTimeout" yaml:"backlogTimeout" json:"backlogTimeout"`
ResponseRetryAfter config.TimeDuration `mapstructure:"responseRetryAfter" yaml:"responseRetryAfter" json:"responseRetryAfter"`
}
InFlightLimitZoneConfig represents zone configuration for in-flight limiting.
func (*InFlightLimitZoneConfig) Validate ¶
func (c *InFlightLimitZoneConfig) Validate() error
Validate validates zone configuration for in-flight limiting.
type MetricsCollector ¶
type MetricsCollector = throttleconfig.MetricsCollector
MetricsCollector represents a collector of metrics for rate/in-flight limiting rejects.
type PrometheusMetrics ¶
type PrometheusMetrics = throttleconfig.PrometheusMetrics
PrometheusMetrics represents a collector of Prometheus metrics for rate/in-flight limiting rejects.
type PrometheusMetricsOption ¶
type PrometheusMetricsOption func(*throttleconfig.PrometheusMetricsOpts)
PrometheusMetricsOption configures Prometheus metrics construction for gRPC throttle interceptor.
func WithPrometheusConstLabels ¶
func WithPrometheusConstLabels(labels prometheus.Labels) PrometheusMetricsOption
WithPrometheusConstLabels sets constant labels applied to all metrics.
func WithPrometheusCurriedLabelNames ¶
func WithPrometheusCurriedLabelNames(names ...string) PrometheusMetricsOption
WithPrometheusCurriedLabelNames sets label names that will be required to curry later.
func WithPrometheusNamespace ¶
func WithPrometheusNamespace(ns string) PrometheusMetricsOption
WithPrometheusNamespace sets the Prometheus namespace for metrics.
type RateLimitRetryAfterValue ¶
type RateLimitRetryAfterValue = throttleconfig.RateLimitRetryAfterValue
RateLimitRetryAfterValue represents structured retry-after value for rate limiting.
type RateLimitValue ¶
type RateLimitValue = throttleconfig.RateLimitValue
RateLimitValue represents value for rate limiting.
type RateLimitZoneConfig ¶
type RateLimitZoneConfig struct {
ZoneConfig `mapstructure:",squash" yaml:",inline"`
Alg string `mapstructure:"alg" yaml:"alg" json:"alg"`
RateLimit RateLimitValue `mapstructure:"rateLimit" yaml:"rateLimit" json:"rateLimit"`
BurstLimit int `mapstructure:"burstLimit" yaml:"burstLimit" json:"burstLimit"`
BacklogLimit int `mapstructure:"backlogLimit" yaml:"backlogLimit" json:"backlogLimit"`
BacklogTimeout config.TimeDuration `mapstructure:"backlogTimeout" yaml:"backlogTimeout" json:"backlogTimeout"`
ResponseRetryAfter RateLimitRetryAfterValue `mapstructure:"responseRetryAfter" yaml:"responseRetryAfter" json:"responseRetryAfter"`
}
RateLimitZoneConfig represents zone configuration for rate limiting.
func (*RateLimitZoneConfig) Validate ¶
func (c *RateLimitZoneConfig) Validate() error
Validate validates zone configuration for rate limiting.
type RuleConfig ¶
type RuleConfig struct {
// Alias is an alternative name for the rule. It will be used as a label in metrics.
Alias string `mapstructure:"alias" yaml:"alias" json:"alias"`
// ServiceMethods contains a list of gRPC service methods for which the rule will be applied.
// Patterns like "/package.Service/Method" or "/package.Service/*" are supported.
ServiceMethods []string `mapstructure:"serviceMethods" yaml:"serviceMethods" json:"serviceMethods"`
// ExcludedServiceMethods contains list of gRPC service methods to be excluded from throttling limitations.
ExcludedServiceMethods []string `mapstructure:"excludedServiceMethods" yaml:"excludedServiceMethods" json:"excludedServiceMethods"`
// Tags is useful when the different rules of the same config should be used by different interceptors.
Tags TagsList `mapstructure:"tags" yaml:"tags" json:"tags"`
// RateLimits contains a list of the rate limiting zones that are used in the rule.
RateLimits []RuleRateLimit `mapstructure:"rateLimits" yaml:"rateLimits" json:"rateLimits"`
// InFlightLimits contains a list of the in-flight limiting zones that are used in the rule.
InFlightLimits []RuleInFlightLimit `mapstructure:"inFlightLimits" yaml:"inFlightLimits" json:"inFlightLimits"`
}
RuleConfig represents configuration for throttling rule.
func (*RuleConfig) Validate ¶
func (c *RuleConfig) Validate( rateLimitZones map[string]RateLimitZoneConfig, inFlightLimitZones map[string]InFlightLimitZoneConfig, ) error
Validate validates throttling rule configuration.
type RuleInFlightLimit ¶
type RuleInFlightLimit = throttleconfig.RuleInFlightLimit
RuleInFlightLimit represents rule's in-flight limiting parameters.
type RuleRateLimit ¶
type RuleRateLimit = throttleconfig.RuleRateLimit
RuleRateLimit represents rule's rate limiting parameters.
type StreamGetKeyFunc ¶
type StreamGetKeyFunc func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo) (key string, bypass bool, err error)
type StreamInterceptorOption ¶
type StreamInterceptorOption func(*streamInterceptorOptions)
func WithStreamGetKeyIdentity ¶
func WithStreamGetKeyIdentity(getKeyIdentity StreamGetKeyFunc) StreamInterceptorOption
WithStreamGetKeyIdentity sets the function to extract identity string from the request context for stream interceptors.
func WithStreamInFlightLimitOnError ¶
func WithStreamInFlightLimitOnError(onError interceptor.InFlightLimitStreamOnErrorFunc) StreamInterceptorOption
WithStreamInFlightLimitOnError sets the callback for handling in-flight limiting errors in stream requests.
func WithStreamInFlightLimitOnReject ¶
func WithStreamInFlightLimitOnReject(onReject interceptor.InFlightLimitStreamOnRejectFunc) StreamInterceptorOption
WithStreamInFlightLimitOnReject sets the callback for handling rejected stream requests when in-flight limit is exceeded.
func WithStreamInFlightLimitOnRejectInDryRun ¶
func WithStreamInFlightLimitOnRejectInDryRun(onReject interceptor.InFlightLimitStreamOnRejectFunc) StreamInterceptorOption
WithStreamInFlightLimitOnRejectInDryRun sets the callback for handling rejected stream requests in dry run mode when in-flight limit is exceeded.
func WithStreamMetricsCollector ¶
func WithStreamMetricsCollector(collector MetricsCollector) StreamInterceptorOption
WithStreamMetricsCollector sets the metrics collector for collecting throttling metrics.
func WithStreamRateLimitOnError ¶
func WithStreamRateLimitOnError(onError interceptor.RateLimitStreamOnErrorFunc) StreamInterceptorOption
WithStreamRateLimitOnError sets the callback for handling rate limiting errors in stream requests.
func WithStreamRateLimitOnReject ¶
func WithStreamRateLimitOnReject(onReject interceptor.RateLimitStreamOnRejectFunc) StreamInterceptorOption
WithStreamRateLimitOnReject sets the callback for handling rejected stream requests when rate limit is exceeded.
func WithStreamRateLimitOnRejectInDryRun ¶
func WithStreamRateLimitOnRejectInDryRun(onReject interceptor.RateLimitStreamOnRejectFunc) StreamInterceptorOption
WithStreamRateLimitOnRejectInDryRun sets the callback for handling rejected stream requests in dry run mode when rate limit is exceeded.
func WithStreamTags ¶
func WithStreamTags(tags []string) StreamInterceptorOption
WithStreamTags sets the list of tags for filtering throttling rules from the config for stream interceptors.
type UnaryGetKeyFunc ¶
type UnaryInterceptorOption ¶
type UnaryInterceptorOption func(*unaryInterceptorOptions)
func WithUnaryGetKeyIdentity ¶
func WithUnaryGetKeyIdentity(getKeyIdentity UnaryGetKeyFunc) UnaryInterceptorOption
WithUnaryGetKeyIdentity sets the function to extract identity string from the request context for unary interceptors.
func WithUnaryInFlightLimitOnError ¶
func WithUnaryInFlightLimitOnError(onError interceptor.InFlightLimitUnaryOnErrorFunc) UnaryInterceptorOption
WithUnaryInFlightLimitOnError sets the callback for handling in-flight limiting errors in unary requests.
func WithUnaryInFlightLimitOnReject ¶
func WithUnaryInFlightLimitOnReject(onReject interceptor.InFlightLimitUnaryOnRejectFunc) UnaryInterceptorOption
WithUnaryInFlightLimitOnReject sets the callback for handling rejected unary requests when in-flight limit is exceeded.
func WithUnaryInFlightLimitOnRejectInDryRun ¶
func WithUnaryInFlightLimitOnRejectInDryRun(onReject interceptor.InFlightLimitUnaryOnRejectFunc) UnaryInterceptorOption
WithUnaryInFlightLimitOnRejectInDryRun sets the callback for handling rejected unary requests in dry run mode when in-flight limit is exceeded.
func WithUnaryMetricsCollector ¶
func WithUnaryMetricsCollector(collector MetricsCollector) UnaryInterceptorOption
WithUnaryMetricsCollector sets the metrics collector for collecting throttling metrics.
func WithUnaryRateLimitOnError ¶
func WithUnaryRateLimitOnError(onError interceptor.RateLimitUnaryOnErrorFunc) UnaryInterceptorOption
WithUnaryRateLimitOnError sets the callback for handling rate limiting errors in unary requests.
func WithUnaryRateLimitOnReject ¶
func WithUnaryRateLimitOnReject(onReject interceptor.RateLimitUnaryOnRejectFunc) UnaryInterceptorOption
WithUnaryRateLimitOnReject sets the callback for handling rejected unary requests when rate limit is exceeded.
func WithUnaryRateLimitOnRejectInDryRun ¶
func WithUnaryRateLimitOnRejectInDryRun(onReject interceptor.RateLimitUnaryOnRejectFunc) UnaryInterceptorOption
WithUnaryRateLimitOnRejectInDryRun sets the callback for handling rejected unary requests in dry run mode when rate limit is exceeded.
func WithUnaryTags ¶
func WithUnaryTags(tags []string) UnaryInterceptorOption
WithUnaryTags sets the list of tags for filtering throttling rules from the config for unary interceptors.
type ZoneConfig ¶
type ZoneConfig struct {
Key ZoneKeyConfig `mapstructure:"key" yaml:"key" json:"key"`
MaxKeys int `mapstructure:"maxKeys" yaml:"maxKeys" json:"maxKeys"`
DryRun bool `mapstructure:"dryRun" yaml:"dryRun" json:"dryRun"`
IncludedKeys []string `mapstructure:"includedKeys" yaml:"includedKeys" json:"includedKeys"`
ExcludedKeys []string `mapstructure:"excludedKeys" yaml:"excludedKeys" json:"excludedKeys"`
}
ZoneConfig represents a basic zone configuration.
func (*ZoneConfig) Validate ¶
func (c *ZoneConfig) Validate() error
Validate validates zone configuration.
type ZoneKeyConfig ¶
type ZoneKeyConfig = throttleconfig.ZoneKeyConfig
ZoneKeyConfig represents a configuration of zone's key.
type ZoneKeyType ¶
type ZoneKeyType = throttleconfig.ZoneKeyType
ZoneKeyType is a type of keys zone.