types

package
v1.12.0 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DatabaseStorageConfig

type DatabaseStorageConfig struct {
	CleanupInterval time.Duration `json:"cleanup_interval" toml:"cleanup_interval"`
}

type MemoryStorageConfig

type MemoryStorageConfig struct {
	CleanupInterval time.Duration `json:"cleanup_interval" toml:"cleanup_interval"`
}

type RateLimit

type RateLimit struct {
	bun.BaseModel `bun:"table:rate_limits"`

	Key       string    `json:"key" bun:"column:key,pk"`
	Count     int       `json:"count" bun:"column:count"`
	ExpiresAt time.Time `json:"expires_at" bun:"column:expires_at"`
}

type RateLimitCheckRequest

type RateLimitCheckRequest struct {
	ClientIP   string
	Path       string
	HTTPMethod string
}

RateLimitCheckRequest contains the information needed to check rate limits

type RateLimitCheckResponse

type RateLimitCheckResponse struct {
	// Allowed indicates whether the request should be allowed
	Allowed bool
	// Limit is the maximum number of requests allowed
	Limit int
	// Window is the time window for the rate limit in seconds
	Window int
	// RetryAfter is the number of seconds to wait before retrying (only set if Allowed is false)
	RetryAfter int
}

RateLimitCheckResponse contains the result of a rate limit check

type RateLimitEntry

type RateLimitEntry struct {
	Count     int
	FirstReq  time.Time
	LastReset time.Time
}

RateLimitEntry tracks requests for a specific key

type RateLimitPluginConfig

type RateLimitPluginConfig struct {
	Enabled     bool                     `json:"enabled" toml:"enabled"`
	Window      time.Duration            `json:"window" toml:"window"`
	Max         int                      `json:"max" toml:"max"`
	Prefix      string                   `json:"prefix,omitempty" toml:"prefix"`
	CustomRules map[string]RateLimitRule `json:"custom_rules" toml:"custom_rules"`
	Provider    RateLimitProviderType    `json:"provider" toml:"provider"`
	Memory      *MemoryStorageConfig     `json:"memory,omitempty" toml:"memory"`
	Database    *DatabaseStorageConfig   `json:"database,omitempty" toml:"database"`
}

func (*RateLimitPluginConfig) ApplyDefaults

func (config *RateLimitPluginConfig) ApplyDefaults()

type RateLimitProvider

type RateLimitProvider interface {
	// GetName returns the name of the provider
	GetName() string
	// CheckAndIncrement checks if a request is allowed and increments the counter if so
	// key is the fully-qualified key (with prefix already included)
	// window is the time window for expiration
	// maxRequests is the maximum number of requests allowed in the window
	// Returns: (allowed bool, currentCount int, resetTime time.Time, error)
	CheckAndIncrement(ctx context.Context, key string, window time.Duration, maxRequests int) (bool, int, time.Time, error)
	// SetRule stores a per-key rate-limit rule without consuming quota.
	SetRule(ctx context.Context, key string, window time.Duration, maxRequests int) error
	// GetRule retrieves a stored per-key rule. Returns 0, 0, false, nil when not found.
	GetRule(ctx context.Context, key string) (window time.Duration, maxRequests int, found bool, err error)
	// DeleteRule removes the stored rule for a key.
	DeleteRule(ctx context.Context, key string) error
	// Close closes any resources held by the provider
	Close() error
}

RateLimitProvider defines the interface for rate limit storage backends Implementations can use in-memory storage, Redis, database, or any other backend

type RateLimitProviderType

type RateLimitProviderType string
const (
	RateLimitProviderInMemory RateLimitProviderType = "memory"
	RateLimitProviderRedis    RateLimitProviderType = "redis"
	RateLimitProviderDatabase RateLimitProviderType = "database"
)

func (RateLimitProviderType) String

func (r RateLimitProviderType) String() string

type RateLimitRule

type RateLimitRule struct {
	Disabled bool          `json:"disabled" toml:"disabled"`
	Window   time.Duration `json:"window" toml:"window"`
	Max      int           `json:"max" toml:"max"`
	Prefix   string        `json:"prefix,omitempty" toml:"prefix"`
}

type RateLimitRuleRecord

type RateLimitRuleRecord struct {
	bun.BaseModel `bun:"table:rate_limit_rules"`

	Key           string `json:"key" bun:"column:key,pk"`
	WindowSeconds int    `json:"window_seconds" bun:"column:window_seconds"`
	MaxRequests   int    `json:"max_requests" bun:"column:max_requests"`
}

Jump to

Keyboard shortcuts

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