protection

package
v1.3.0 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetResourceProtectionType

func GetResourceProtectionType(err error) string

GetResourceProtectionType returns the type of resource protection error

func IsResourceProtectionError

func IsResourceProtectionError(err error) bool

IsResourceProtectionError checks if an error is a resource protection error

Types

type BatchLimiter

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

BatchLimiter provides batch operation protection

func (*BatchLimiter) AcquireBatch

func (bl *BatchLimiter) AcquireBatch(ctx context.Context, batchSize int) error

AcquireBatch acquires permission for a batch operation

func (*BatchLimiter) ReleaseBatch

func (bl *BatchLimiter) ReleaseBatch()

ReleaseBatch releases batch operation permission

type MemoryAlert

type MemoryAlert struct {
	Timestamp    time.Time `json:"timestamp"`
	Type         string    `json:"type"`
	Severity     string    `json:"severity"`
	CurrentMB    int64     `json:"current_mb"`
	LimitMB      int64     `json:"limit_mb"`
	UsagePercent float64   `json:"usage_percent"`
}

MemoryAlert represents a memory usage alert

type MemoryMonitor

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

MemoryMonitor monitors memory usage

func (*MemoryMonitor) Start

func (mm *MemoryMonitor) Start()

Start starts the memory monitor

func (*MemoryMonitor) Stop

func (mm *MemoryMonitor) Stop()

Stop stops the memory monitor

type ProtectionError

type ProtectionError struct {
	Type   string `json:"type"`
	Detail string `json:"detail"`
}

ProtectionError represents a resource protection error

func (*ProtectionError) Error

func (e *ProtectionError) Error() string

type ResourceLimits

type ResourceLimits struct {
	// HTTP request limits
	MaxRequestBodySize int64         `json:"max_request_body_size" yaml:"max_request_body_size"`
	MaxRequestTimeout  time.Duration `json:"max_request_timeout" yaml:"max_request_timeout"`
	MaxConcurrentReq   int           `json:"max_concurrent_requests" yaml:"max_concurrent_requests"`

	// Batch operation limits
	MaxBatchSize       int     `json:"max_batch_size" yaml:"max_batch_size"`
	MaxConcurrentBatch int     `json:"max_concurrent_batch" yaml:"max_concurrent_batch"`
	BatchRateLimit     float64 `json:"batch_rate_limit" yaml:"batch_rate_limit"`

	// Memory limits
	MaxMemoryMB          int64         `json:"max_memory_mb" yaml:"max_memory_mb"`
	MemoryCheckInterval  time.Duration `json:"memory_check_interval" yaml:"memory_check_interval"`
	MemoryPanicThreshold float64       `json:"memory_panic_threshold" yaml:"memory_panic_threshold"`

	// Rate limiting
	RequestsPerSecond float64 `json:"requests_per_second" yaml:"requests_per_second"`
	BurstSize         int     `json:"burst_size" yaml:"burst_size"`
}

ResourceLimits defines configurable resource limits

func DefaultResourceLimits

func DefaultResourceLimits() ResourceLimits

DefaultResourceLimits returns secure default limits

type ResourceProtector

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

ResourceProtector provides resource protection and monitoring

func NewResourceProtector

func NewResourceProtector(config ResourceLimits) *ResourceProtector

NewResourceProtector creates a new resource protector

func (*ResourceProtector) GetBatchLimiter

func (rp *ResourceProtector) GetBatchLimiter() *BatchLimiter

GetBatchLimiter returns a batch limiter

func (*ResourceProtector) GetStats

func (rp *ResourceProtector) GetStats() ResourceStats

GetStats returns current resource usage statistics

func (*ResourceProtector) HealthCheck

func (rp *ResourceProtector) HealthCheck() map[string]any

HealthCheck performs a resource protection health check

func (*ResourceProtector) SecureBodyReader

func (rp *ResourceProtector) SecureBodyReader(r *http.Request) ([]byte, error)

SecureBodyReader provides secure HTTP body reading with size limits

func (*ResourceProtector) StartMemoryMonitoring

func (rp *ResourceProtector) StartMemoryMonitoring(alertCallback func(MemoryAlert))

StartMemoryMonitoring starts memory monitoring

func (*ResourceProtector) StopMemoryMonitoring

func (rp *ResourceProtector) StopMemoryMonitoring()

StopMemoryMonitoring stops memory monitoring

type ResourceStats

type ResourceStats struct {
	LastStatsUpdate    time.Time `json:"last_stats_update"`
	ConcurrentBatchOps int64     `json:"concurrent_batch_operations"`
	ConcurrentRequests int64     `json:"concurrent_requests"`
	MaxConcurrentReq   int64     `json:"max_concurrent_requests"`
	TotalBatchOps      int64     `json:"total_batch_operations"`
	RejectedBatchOps   int64     `json:"rejected_batch_operations"`
	TotalRequests      int64     `json:"total_requests"`
	MaxConcurrentBatch int64     `json:"max_concurrent_batch"`
	CurrentMemoryMB    int64     `json:"current_memory_mb"`
	PeakMemoryMB       int64     `json:"peak_memory_mb"`
	MemoryAlerts       int64     `json:"memory_alerts"`
	RateLimitHits      int64     `json:"rate_limit_hits"`
	RejectedRequests   int64     `json:"rejected_requests"`
}

ResourceStats tracks resource usage statistics

type SimpleLimiter

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

SimpleLimiter is a basic rate limiter implementation

func NewSimpleLimiter

func NewSimpleLimiter(rps float64, burst int) *SimpleLimiter

NewSimpleLimiter creates a new simple rate limiter

func (*SimpleLimiter) Allow

func (l *SimpleLimiter) Allow() bool

Allow checks if a request is allowed

Jump to

Keyboard shortcuts

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