rules

package
v0.92.1 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2026 License: GPL-3.0 Imports: 10 Imported by: 0

Documentation

Overview

Package rules provides the notification rule engine for throttling, aggregation, and mute/DND.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Init

func Init(store *cache.RedisStore) error

Init initializes the global rule engine with configuration and a RedisStore.

func ValidateCondition

func ValidateCondition(condition string) error

ValidateCondition checks whether a condition expression string is syntactically valid. It uses the same parsing logic as evalCondition but without evaluating time values.

Types

type AggregateKey

type AggregateKey struct {
	RuleID    string
	EventType string
	Channel   string
}

AggregateKey holds the parsed components of an aggregate timer key.

type Engine

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

Engine evaluates notification rules to determine whether a message should be sent, throttled, aggregated, or dropped.

func GetEngine

func GetEngine() *Engine

GetEngine returns the global rule engine.

func New

func New(store *cache.RedisStore) *Engine

New creates a new rule Engine.

func (*Engine) CheckThrottle

func (e *Engine) CheckThrottle(ctx context.Context, ruleID, eventType, channel string, window time.Duration, limit int) (bool, error)

CheckThrottle checks whether a notification is within the rate limit for the given key. Returns true if the notification is allowed, false if it should be dropped.

func (*Engine) ClearThrottle

func (e *Engine) ClearThrottle(ctx context.Context, ruleID, eventType, channel string)

ClearThrottle removes the throttle counter for a given key, resetting the rate limiter.

func (*Engine) EnqueueForAggregation

func (e *Engine) EnqueueForAggregation(ctx context.Context, ruleID, eventType, channel string, payload map[string]any) error

EnqueueForAggregation adds a payload to the aggregation buffer for later digest delivery.

func (*Engine) Evaluate

func (e *Engine) Evaluate(_ context.Context, eventType, channel string) *EvalResult

Evaluate checks all rules against an event type and channel, returning the first matching action.

func (*Engine) FlushAggregation

func (e *Engine) FlushAggregation(ctx context.Context, ruleID, eventType, channel string) ([]map[string]any, error)

FlushAggregation retrieves all buffered payloads for a given rule/channel and clears the buffer.

func (*Engine) LoadConfig

func (e *Engine) LoadConfig(rules []config.NotifyRule) error

LoadConfig loads and sorts rules from configuration.

func (*Engine) Reload

func (e *Engine) Reload(ctx context.Context, loader func(context.Context) ([]config.NotifyRule, error)) error

Reload refreshes the rule list from the database. Called after rule CRUD operations to enable hot-reload without restart.

func (*Engine) ScanExpiredAggregates

func (e *Engine) ScanExpiredAggregates(ctx context.Context) ([]AggregateKey, error)

ScanExpiredAggregates finds aggregate timer keys that have expired and returns their rule/channel info.

func (*Engine) SetAggregateTimer

func (e *Engine) SetAggregateTimer(ctx context.Context, ruleID, eventType, channel string, window time.Duration) (bool, error)

SetAggregateTimer sets a timer key for the aggregation window. Returns true if this is the first element (i.e., timer was created).

type EvalResult

type EvalResult struct {
	Action config.NotifyRuleAction
	RuleID string
	Window string
	Limit  int
	Muted  bool
}

EvalResult represents the outcome of rule evaluation.

type Worker

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

Worker periodically scans for expired aggregation windows and flushes them.

func NewWorker

func NewWorker(engine *Engine, interval time.Duration, onFlush func(ctx context.Context, ruleID, eventType, channel string, items []map[string]any)) *Worker

NewWorker creates a new aggregate worker.

func (*Worker) Run

func (w *Worker) Run(ctx context.Context)

Run starts the periodic scan loop. It blocks until the context is cancelled.

Jump to

Keyboard shortcuts

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