rule

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2026 License: MIT Imports: 15 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package rule provides the rule matching and routing engine for CoreC data points.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EvalArith

func EvalArith(exprStr string, value float64) (float64, error)

EvalArith parses and evaluates an arithmetic expression against the given value. The expression must reference the variable as `value`. Compiled programs are cached for reuse across calls with the same expression.

Types

type Engine

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

Engine is the rule matching engine. It evaluates DataPoints against a priority-sorted list of rules. Rules with a detectable driver filter are indexed by driver name so that a data point from driver X only evaluates rules scoped to X (plus universal rules that can match any driver). Rules scoped to other drivers have their miss statistics updated without evaluating their match expressions. This preserves the priority-order and statistics invariants documented in the Match method while reducing the per-data-point cost from O(all rules) to O(relevant rules).

func NewEngine

func NewEngine() *Engine

NewEngine creates a new rule engine.

func (*Engine) AddProvider

func (e *Engine) AddProvider(p core.RuleProvider)

AddProvider registers a rule provider for RULE-SET references.

func (*Engine) CloseProviders added in v0.0.2

func (e *Engine) CloseProviders()

CloseProviders closes and removes all registered rule providers, stopping any background goroutines they started (e.g. reload loops). This should be called during engine Stop/Reload to prevent goroutine leaks (problem 4).

func (*Engine) Match

func (e *Engine) Match(point core.DataPoint) *MatchResult

Match finds the first matching rule for a DataPoint. Rules are evaluated in priority order (lower priority = higher precedence). Returns nil if no rule matches.

Match uses a driver index to reduce the per-data-point scan cost. Rules with a detectable driver == 'X' filter are only evaluated for data points from driver X; for data points from other drivers, their miss statistics are updated directly without evaluating the match expression. Universal rules (ALL, complex expressions, rule-set/sub-rule refs) are always evaluated. Both groups are priority-sorted, and the two sorted lists are merged on the fly to preserve global priority order — the first match across both groups is returned, exactly as a full linear scan would produce. Miss statistics for rules scoped to other drivers are recorded via RecordMiss, preserving the invariant that every non-matching, non-disabled rule records a miss.

func (*Engine) ProviderCount added in v0.0.2

func (e *Engine) ProviderCount() int

ProviderCount returns the number of registered rule providers.

func (*Engine) RuleStats

func (e *Engine) RuleStats() []core.RuleStat

RuleStats returns runtime statistics for all rules.

func (*Engine) Rules

func (e *Engine) Rules() []core.Rule

Rules returns the active rules.

func (*Engine) SetRuleDisabled

func (e *Engine) SetRuleDisabled(index int, disabled bool) error

SetRuleDisabled enables or disables a rule by index.

func (*Engine) SetRules

func (e *Engine) SetRules(configs []core.RuleConfig) error

SetRules parses rule configs and sets the active rule set.

func (*Engine) SetSubRules

func (e *Engine) SetSubRules(groups map[string][]core.RuleConfig) error

SetSubRules parses named sub-rule groups for SUB-RULE references.

type FileProvider

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

FileProvider loads match-only rules from a YAML file and supports hot-reload at a configurable interval.

func NewFileProvider

func NewFileProvider(name, path, intervalStr string) (*FileProvider, error)

NewFileProvider creates a file-based rule provider. interval is the hot-reload interval; 0 means no auto-reload.

func (*FileProvider) Close

func (p *FileProvider) Close()

Close stops the reload goroutine and waits for it to exit.

func (*FileProvider) Count

func (p *FileProvider) Count() int

func (*FileProvider) Initial

func (p *FileProvider) Initial() error

Initial loads the provider data (already done in constructor, kept for interface compat).

func (*FileProvider) Match

func (p *FileProvider) Match(point core.DataPoint) bool

Match returns true if any rule in the provider matches the DataPoint.

func (*FileProvider) Name

func (p *FileProvider) Name() string

func (*FileProvider) Update

func (p *FileProvider) Update() error

Update forces a reload of the provider file.

type MatchResult

type MatchResult = core.RuleMatchResult

MatchResult contains the matched rule and resolved targets. It is an alias for core.RuleMatchResult so the concrete rule.Engine satisfies the core.RuleEngine port (whose Match returns *core.RuleMatchResult) without any conversion at the call site.

Jump to

Keyboard shortcuts

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