Documentation
¶
Index ¶
- func GetResourceProtectionType(err error) string
- func IsResourceProtectionError(err error) bool
- type BatchLimiter
- type MemoryAlert
- type MemoryMonitor
- type ProtectionError
- type ResourceLimits
- type ResourceProtector
- func (rp *ResourceProtector) GetBatchLimiter() *BatchLimiter
- func (rp *ResourceProtector) GetStats() ResourceStats
- func (rp *ResourceProtector) HealthCheck() map[string]any
- func (rp *ResourceProtector) SecureBodyReader(r *http.Request) ([]byte, error)
- func (rp *ResourceProtector) StartMemoryMonitoring(alertCallback func(MemoryAlert))
- func (rp *ResourceProtector) StopMemoryMonitoring()
- type ResourceStats
- type SimpleLimiter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetResourceProtectionType ¶
GetResourceProtectionType returns the type of resource protection error
func IsResourceProtectionError ¶
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 {
Type string `json:"type"`
CurrentMB int64 `json:"current_mb"`
LimitMB int64 `json:"limit_mb"`
UsagePercent float64 `json:"usage_percent"`
Timestamp time.Time `json:"timestamp"`
Severity string `json:"severity"`
}
MemoryAlert represents a memory usage alert
type MemoryMonitor ¶
type MemoryMonitor struct {
// contains filtered or unexported fields
}
MemoryMonitor monitors memory usage
type ProtectionError ¶
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 {
// Request stats
TotalRequests int64 `json:"total_requests"`
RejectedRequests int64 `json:"rejected_requests"`
ConcurrentRequests int64 `json:"concurrent_requests"`
MaxConcurrentReq int64 `json:"max_concurrent_requests"`
// Batch stats
TotalBatchOps int64 `json:"total_batch_operations"`
RejectedBatchOps int64 `json:"rejected_batch_operations"`
ConcurrentBatchOps int64 `json:"concurrent_batch_operations"`
MaxConcurrentBatch int64 `json:"max_concurrent_batch"`
// Memory stats
CurrentMemoryMB int64 `json:"current_memory_mb"`
PeakMemoryMB int64 `json:"peak_memory_mb"`
MemoryAlerts int64 `json:"memory_alerts"`
// Rate limiting stats
RateLimitHits int64 `json:"rate_limit_hits"`
LastStatsUpdate time.Time `json:"last_stats_update"`
}
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