Skip to content

Commit

Permalink
use shared string match for rate limit
Browse files Browse the repository at this point in the history
Signed-off-by: huabing zhao <zhaohuabing@gmail.com>
  • Loading branch information
zhaohuabing committed Oct 28, 2023
1 parent 476ec69 commit 51596e1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 41 deletions.
44 changes: 10 additions & 34 deletions api/v1alpha1/ratelimit_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,49 +108,25 @@ type SourceMatch struct {
}

// HeaderMatch defines the match attributes within the HTTP Headers of the request.
type HeaderMatch struct { // TODO: zhaohuabing this type could be replaced with a general purpose StringMatch type.
// Type specifies how to match against the value of the header.
//
// +optional
// +kubebuilder:default=Exact
Type *HeaderMatchType `json:"type,omitempty"`

type HeaderMatch struct {
// Name of the HTTP header.
// +kubebuilder:validation:MinLength=1
// +kubebuilder:validation:MaxLength=256
Name string `json:"name"`

// Value within the HTTP header. Due to the
// case-insensitivity of header names, "foo" and "Foo" are considered equivalent.
// Distinct matches any and all possible unique values encountered in the
// specified HTTP Header.
// Note that each unique value will receive its own rate limit bucket.
// Only one of Distinct or Match can be set.
Distinct *bool `json:"distinct,omitempty"`

// Match specifies how to match against the value of the header.
// Do not set this field when Type="Distinct", implying matching on any/all unique
// values within the header.
//
// +optional
// +kubebuilder:validation:MaxLength=1024
Value *string `json:"value,omitempty"`
// Only one of Distinct or Match can be set.
Match *StringMatch `json:"match,omitempty"`
}

// HeaderMatchType specifies the semantics of how HTTP header values should be compared.
// Valid HeaderMatchType values are "Exact", "RegularExpression", and "Distinct".
//
// +kubebuilder:validation:Enum=Exact;RegularExpression;Distinct
type HeaderMatchType string

// HeaderMatchType constants.
const (
// HeaderMatchExact matches the exact value of the Value field against the value of
// the specified HTTP Header.
HeaderMatchExact HeaderMatchType = "Exact"
// HeaderMatchRegularExpression matches a regular expression against the value of the
// specified HTTP Header. The regex string must adhere to the syntax documented in
// https://github.com/google/re2/wiki/Syntax.
HeaderMatchRegularExpression HeaderMatchType = "RegularExpression"
// HeaderMatchDistinct matches any and all possible unique values encountered in the
// specified HTTP Header. Note that each unique value will receive its own rate limit
// bucket.
HeaderMatchDistinct HeaderMatchType = "Distinct"
)

// RateLimitValue defines the limits for rate limiting.
type RateLimitValue struct {
Requests uint `json:"requests"`
Expand Down
14 changes: 7 additions & 7 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 51596e1

Please sign in to comment.