ratelimit

package module
v0.1.24 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 30, 2026 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetPolicy

func GetPolicy(
	metadata policy.PolicyMetadata,
	params map[string]interface{},
) (policy.Policy, error)

GetPolicy creates and initializes a rate limit policy instance

Types

type CELEvaluator

type CELEvaluator struct {
	// contains filtered or unexported fields
}

CELEvaluator provides CEL expression evaluation for rate limit key and cost extraction

func GetCELEvaluator

func GetCELEvaluator() (*CELEvaluator, error)

GetCELEvaluator returns the singleton CEL evaluator instance

func (*CELEvaluator) EvaluateKeyExpression

func (e *CELEvaluator) EvaluateKeyExpression(expression string, ctx *policy.RequestContext, routeName string) (string, error)

EvaluateKeyExpression evaluates a CEL expression for key extraction from request context Returns the extracted key string or an error

func (*CELEvaluator) EvaluateRequestCostExpression

func (e *CELEvaluator) EvaluateRequestCostExpression(expression string, ctx *policy.RequestContext) (float64, error)

EvaluateRequestCostExpression evaluates a CEL expression for cost extraction from request context Returns the extracted cost value or an error

func (*CELEvaluator) EvaluateResponseCostExpression

func (e *CELEvaluator) EvaluateResponseCostExpression(expression string, ctx *policy.ResponseContext) (float64, error)

EvaluateResponseCostExpression evaluates a CEL expression for cost extraction from response context Returns the extracted cost value or an error

type CostExtractionConfig

type CostExtractionConfig struct {
	Enabled bool
	Sources []CostSource
	Default float64 // Default cost if all sources fail
}

CostExtractionConfig holds the configuration for cost extraction

type CostExtractor

type CostExtractor struct {
	// contains filtered or unexported fields
}

CostExtractor handles extracting cost from request/response data

func NewCostExtractor

func NewCostExtractor(config CostExtractionConfig) *CostExtractor

NewCostExtractor creates a new CostExtractor with the given configuration

func (*CostExtractor) ExtractRequestCost

func (e *CostExtractor) ExtractRequestCost(ctx *policy.RequestContext) (float64, bool)

ExtractRequestCost extracts cost from request-phase sources only. Returns (cost, extracted) where extracted indicates if any value was found. When multiple sources succeed, their values are summed (with multipliers applied).

func (*CostExtractor) ExtractResponseCost

func (e *CostExtractor) ExtractResponseCost(ctx *policy.ResponseContext) (float64, bool)

ExtractResponseCost extracts cost from response-phase sources only. Returns (cost, extracted) where extracted indicates if any value was found. When multiple sources succeed, their values are summed (with multipliers applied).

func (*CostExtractor) GetConfig

func (e *CostExtractor) GetConfig() CostExtractionConfig

GetConfig returns the cost extraction configuration

func (*CostExtractor) HasRequestPhaseSources

func (e *CostExtractor) HasRequestPhaseSources() bool

HasRequestPhaseSources returns true if any source is available during request phase

func (*CostExtractor) HasResponsePhaseSources

func (e *CostExtractor) HasResponsePhaseSources() bool

HasResponsePhaseSources returns true if any source is available during response phase

func (*CostExtractor) RequiresRequestBody

func (e *CostExtractor) RequiresRequestBody() bool

RequiresRequestBody returns true if any source requires request body access

func (*CostExtractor) RequiresResponseBody

func (e *CostExtractor) RequiresResponseBody() bool

RequiresResponseBody returns true if any source requires response body access

type CostSource

type CostSource struct {
	Type       CostSourceType // source type
	Key        string         // Header name or metadata key
	JSONPath   string         // For body types: JSONPath expression
	Expression string         // For CEL types: CEL expression
	Multiplier float64        // Multiplier for extracted value (default: 1.0)
}

CostSource represents a single source for extracting cost

type CostSourceType

type CostSourceType string

CostSourceType defines the type of source for cost extraction

const (
	// Request phase sources
	CostSourceRequestHeader   CostSourceType = "request_header"
	CostSourceRequestMetadata CostSourceType = "request_metadata"
	CostSourceRequestBody     CostSourceType = "request_body"
	CostSourceRequestCEL      CostSourceType = "request_cel"

	// Response phase sources
	CostSourceResponseHeader   CostSourceType = "response_header"
	CostSourceResponseMetadata CostSourceType = "response_metadata"
	CostSourceResponseBody     CostSourceType = "response_body"
	CostSourceResponseCEL      CostSourceType = "response_cel"
)

type KeyComponent

type KeyComponent struct {
	Type       string // "header", "metadata", "ip", "apiname", "apiversion", "routename", "cel"
	Key        string // header name or metadata key (required for header/metadata)
	Expression string // CEL expression (required for cel type)
}

KeyComponent represents a single component for building rate limit keys

type LimitConfig

type LimitConfig struct {
	Limit    int64
	Duration time.Duration
	Burst    int64
}

LimitConfig holds parsed rate limit configuration

type QuotaRuntime

type QuotaRuntime struct {
	Name                  string          // Optional name for logging/headers
	Limits                []LimitConfig   // Rate limits for this quota
	KeyExtraction         []KeyComponent  // Per-quota key extraction
	Limiter               limiter.Limiter // Limiter instance for this quota
	CostExtractor         *CostExtractor  // Per-quota cost extractor
	CostExtractionEnabled bool            // Whether cost extraction is enabled
}

QuotaRuntime holds per-quota runtime configuration and limiter instance. Each quota is a self-contained rate limit dimension with its own key extraction, cost extraction, and limiter.

type RateLimitPolicy

type RateLimitPolicy struct {
	// contains filtered or unexported fields
}

RateLimitPolicy defines the policy for rate limiting

func (*RateLimitPolicy) Mode

Mode returns the processing mode for this policy

func (*RateLimitPolicy) OnRequest

func (p *RateLimitPolicy) OnRequest(
	ctx *policy.RequestContext,
	params map[string]interface{},
) policy.RequestAction

OnRequest performs rate limit check across all quotas

func (*RateLimitPolicy) OnResponse

func (p *RateLimitPolicy) OnResponse(
	ctx *policy.ResponseContext,
	params map[string]interface{},
) policy.ResponseAction

OnResponse adds rate limit headers to the response sent to the client

Directories

Path Synopsis
algorithms

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL