Documentation ¶
Overview ¶
Package polaris is a rate limiting plugin for tRPC-Gateway based on Polaris.
Index ¶
Constants ¶
const ( // PluginName is the name of the plugin. PluginName = "polaris_limiter" // ErrLimit indicates that the request has been rate limited. ErrLimit = 10003 )
Variables ¶
var New = func(opts ...Opt) (*Limiter, error) { l := &Limiter{} for _, o := range opts { o(l) } cfg := config.NewDefaultConfigurationWithDomain() limitAPI, err := api.NewLimitAPIByConfig(cfg) if err != nil { return nil, fmt.Errorf("failed to create limitAPI from default configuration, err: %w", err) } l.API = limitAPI return l, nil }
New creates a new Limiter.
Functions ¶
This section is empty.
Types ¶
type GetLabelFunc ¶
GetLabelFunc defines the function to get label.
var DefaultGetLabelFunc GetLabelFunc = func(ctx context.Context, label string) string { fctx := http.RequestContext(ctx) if fctx == nil { return "" } if val := http.GetString(fctx, label); val != "" { return val } return "" }
DefaultGetLabelFunc is a function to get parameters, which can be overridden by yourself.
type Limiter ¶
type Limiter struct { // Timeout for individual quota queries, in milliseconds Timeout int `yaml:"timeout"` // Number of retries MaxRetries int `yaml:"max_retries"` // Global rate limiting object API api.LimitAPI }
Limiter is the Polaris rate limiter.
func (*Limiter) InterceptServer ¶
func (l *Limiter) InterceptServer(ctx context.Context, req interface{}, handler filter.ServerHandleFunc) (interface{}, error)
InterceptServer implements the server-side rate limiting interceptor.
type Opt ¶
type Opt func(*Limiter)
Opt allows customization of the Limiter.
func WithMaxRetries ¶
WithMaxRetries sets the maximum number of retries for failed rate limiting requests.
func WithTimeout ¶
WithTimeout sets the timeout for rate limiting requests.
type Options ¶
type Options struct { // Business fields Labels []string `yaml:"labels" json:"labels"` // Timeout for quota query, in milliseconds Timeout int `yaml:"timeout" json:"timeout"` // Number of retries MaxRetries int `yaml:"max_retries" json:"max_retries"` // Polaris rate limiting service name, defaults to the gateway project's rate limiting configuration, can be overridden Service string `yaml:"service" json:"service"` // Polaris rate limiting namespace, defaults to the gateway instance's namespace Namespace string `yaml:"namespace" json:"namespace"` // Response body after rate limiting LimitedRspBody string `yaml:"limited_rsp_body" json:"limited_rsp_body"` // Retrieve rate limiting parameters from JSON request body ParseJSONBody bool `yaml:"parse_json_body" json:"parse_json_body"` }
Options for rate limiting
type PluginFactory ¶
type PluginFactory struct { // Default timeout for rate limiting requests Timeout int `yaml:"timeout"` // Default number of retries for rate limiting requests MaxRetries int `yaml:"max_retries"` }
PluginFactory is the plugin factory
func (*PluginFactory) CheckConfig ¶
func (p *PluginFactory) CheckConfig(name string, decoder plugin.Decoder) error
CheckConfig validates the plugin configuration and returns a parsed configuration object. Used in the ServerFilter method.
func (*PluginFactory) Setup ¶
func (p *PluginFactory) Setup(_ string, decoder plugin.Decoder) error
Setup implements the Setup method of plugin.Factory, initializes and registers the interceptor.
func (*PluginFactory) Type ¶
func (p *PluginFactory) Type() string
Type implements the Type method of plugin.Factory.