ratelimitml

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package ratelimitml provides machine learning extensions for advanced rate limiting

Index

Constants

View Source
const (
	PluginName    = "ratelimit-ml"
	PluginVersion = "1.0.0"
)

Plugin constants

Variables

This section is empty.

Functions

func GetPlugin

func GetPlugin() plugin.Plugin

GetPlugin returns a new instance of the plugin for dynamic loading

Types

type AnomalyDetector

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

AnomalyDetector implements advanced anomaly detection

func NewAnomalyDetector

func NewAnomalyDetector(modelPath string, threshold float64) *AnomalyDetector

NewAnomalyDetector creates a new anomaly detector

func (*AnomalyDetector) DetectAnomaly

func (ad *AnomalyDetector) DetectAnomaly(pattern *TrafficPattern) float64

DetectAnomaly detects anomalies in traffic patterns

func (*AnomalyDetector) LoadModel

func (ad *AnomalyDetector) LoadModel() error

LoadModel loads the ML model (placeholder implementation)

func (*AnomalyDetector) SaveModel

func (ad *AnomalyDetector) SaveModel() error

SaveModel saves the ML model (placeholder implementation)

type AnomalyEvent

type AnomalyEvent struct {
	Timestamp   time.Time
	Score       float64
	Type        string
	Description string
	Action      string
}

AnomalyEvent represents a detected anomaly

type BehaviorPrediction

type BehaviorPrediction struct {
	PredictedRate  float64
	PredictedSpike float64
	Confidence     float64
	TimeHorizon    time.Duration
}

type CoordinatedAttack

type CoordinatedAttack struct {
	IPs       []string
	StartTime time.Time
	Type      string
}

type Decision

type Decision struct {
	Block      bool
	Reason     string
	Confidence float64
	Duration   time.Duration
	Escalation int
}

Decision represents an ML-based rate limiting decision

type HistoricalData

type HistoricalData struct {
	Timestamp time.Time
	Rate      float64
	Pattern   string
}

type IsolationForest

type IsolationForest struct {
	Trees         []*IsolationTree
	NumTrees      int
	SampleSize    int
	MaxDepth      int
	FeatureNames  []string
	AnomalyScores map[string]float64 // Cache for recent scores
}

IsolationForest represents the ensemble of isolation trees

type IsolationNode

type IsolationNode struct {
	IsLeaf       bool
	SplitFeature int
	SplitValue   float64
	Left         *IsolationNode
	Right        *IsolationNode
	Size         int // Number of samples at this node
}

IsolationNode represents a node in the isolation tree

type IsolationTree

type IsolationTree struct {
	Root       *IsolationNode
	PathLength map[string]float64
}

IsolationTree represents a single tree in the forest

type MLBlock

type MLBlock struct {
	ClientIP   string
	Reason     string
	Duration   time.Duration
	Escalation int
	AppliedAt  time.Time
}

type MLMetrics

type MLMetrics struct {
	AnomaliesDetected int64
	PredictionsMade   int64
	ModelUpdates      int64
	TruePositives     int64
	FalsePositives    int64
	// contains filtered or unexported fields
}

MLMetrics collects and records ML-related metrics

func NewMLMetrics

func NewMLMetrics() *MLMetrics

func (*MLMetrics) GetCurrentMetrics

func (m *MLMetrics) GetCurrentMetrics() map[string]interface{}

GetCurrentMetrics returns current metrics

func (*MLMetrics) RecordAnomaly

func (m *MLMetrics) RecordAnomaly(clientIP string, score float64)

RecordAnomaly records an anomaly detection

func (*MLMetrics) RecordLearningData

func (m *MLMetrics) RecordLearningData(clientIP string)

RecordLearningData records new learning data

func (*MLMetrics) RecordModelUpdate

func (m *MLMetrics) RecordModelUpdate()

RecordModelUpdate records a model update

func (*MLMetrics) RecordPatternAnalysis

func (m *MLMetrics) RecordPatternAnalysis()

RecordPatternAnalysis records pattern analysis completion

func (*MLMetrics) RecordRequest

func (m *MLMetrics) RecordRequest(clientIP string, anomalyScore float64, decision Decision)

type MLRateLimitConfig

type MLRateLimitConfig struct {
	Enabled              bool    `json:"enabled"`
	ModelPath            string  `json:"modelPath"`
	AnomalyThreshold     float64 `json:"anomalyThreshold"`
	PredictionWindow     string  `json:"predictionWindow"`
	LearningEnabled      bool    `json:"learningEnabled"`
	CoordinationEnabled  bool    `json:"coordinationEnabled"`
	BlockDuration        string  `json:"blockDuration"`
	EscalationMultiplier float64 `json:"escalationMultiplier"`
}

MLRateLimitConfig holds configuration

type NormalProfile

type NormalProfile struct {
	ClientIP         string
	AverageRate      float64
	StdDeviation     float64
	PeakHours        []int
	TypicalEndpoints map[string]float64
	UserAgent        string
	LastSeen         time.Time
}

NormalProfile represents normal traffic behavior

type PatternAnalyzer

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

PatternAnalyzer analyzes request patterns

func NewPatternAnalyzer

func NewPatternAnalyzer() *PatternAnalyzer

func (*PatternAnalyzer) AnalyzeRequest

func (pa *PatternAnalyzer) AnalyzeRequest(clientIP string, r *http.Request) *TrafficPattern

func (*PatternAnalyzer) GetActivePatterns

func (pa *PatternAnalyzer) GetActivePatterns() map[string]*TrafficPattern

type RateLimitCoordinator

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

RateLimitCoordinator coordinates with base rate limiter

func NewRateLimitCoordinator

func NewRateLimitCoordinator() *RateLimitCoordinator

func (*RateLimitCoordinator) ApplyMLBlock

func (rlc *RateLimitCoordinator) ApplyMLBlock(clientIP string, decision Decision)

type RateLimitMLPlugin

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

RateLimitMLPlugin extends rate limiting with ML-based anomaly detection

func (*RateLimitMLPlugin) CreateMiddleware

func (p *RateLimitMLPlugin) CreateMiddleware() (func(http.Handler) http.Handler, error)

CreateMiddleware creates the ML rate limiting middleware

func (*RateLimitMLPlugin) Dependencies

func (p *RateLimitMLPlugin) Dependencies() []plugin.PluginDependency

Dependencies returns required dependencies

func (*RateLimitMLPlugin) Description

func (p *RateLimitMLPlugin) Description() string

Description returns the plugin description

func (*RateLimitMLPlugin) Initialize

func (p *RateLimitMLPlugin) Initialize(ctx context.Context, host plugin.PluginHost, config map[string]interface{}) error

Initialize implements the Plugin interface

func (*RateLimitMLPlugin) Name

func (p *RateLimitMLPlugin) Name() string

Name returns the plugin name

func (*RateLimitMLPlugin) Priority

func (p *RateLimitMLPlugin) Priority() int

Priority returns the plugin priority (higher numbers run later)

func (*RateLimitMLPlugin) Shutdown

func (p *RateLimitMLPlugin) Shutdown(ctx context.Context) error

Shutdown gracefully stops the plugin

func (*RateLimitMLPlugin) Version

func (p *RateLimitMLPlugin) Version() string

Version returns the plugin version

type TimeWindow

type TimeWindow struct {
	Start    time.Time
	End      time.Time
	Requests int
	Unique   int
}

type TrafficAnomaly

type TrafficAnomaly struct {
	Type        string
	Severity    string
	Description string
}

type TrafficPattern

type TrafficPattern struct {
	ClientIP       string
	RequestRate    float64
	BurstPattern   []int
	Periodicity    float64
	Entropy        float64
	EntropyScore   float64 // Alias for Entropy
	Predictability float64
	AnomalyScore   float64
	IsAnomalous    bool
	LastUpdated    time.Time
}

TrafficPattern represents analyzed traffic patterns

type TrafficPredictor

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

TrafficPredictor predicts future traffic behavior

func NewTrafficPredictor

func NewTrafficPredictor(window string) *TrafficPredictor

func (*TrafficPredictor) PredictBehavior

func (tp *TrafficPredictor) PredictBehavior(pattern *TrafficPattern) *BehaviorPrediction

Jump to

Keyboard shortcuts

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