type IGruleEngine interface {
Execute(ctx context.Context, rule string, fact any) error
FetchMatching(ctx context.Context, rule string, fact any) ([]*ast.RuleEntry, error)
AddRule(rule, statement string, duration int64) error
BuildRule(rule, statement string, duration int64) error
ContainsRule(rule string) bool
Debug() map[string]any
Close()
}The main interface for rule engine operations.
type Config struct {
Type CacheType // Cache type: lru, lfu, arc, twoq, random
Size int // Cache size, 0 means unlimited
CleanupInterval int // Cleanup interval in seconds, 0 means no cleanup
TTL int // Time-to-live in seconds, 0 means no expiration
Partition int // Number of partitions for the engine
FactName string // Name of the fact to be used in rules
}Configuration structure for the rule engine.
type CacheType string
const (
LRU CacheType = "lru"
LFU CacheType = "lfu"
ARC CacheType = "arc"
TWOQ CacheType = "twoq"
RANDOM CacheType = "random"
)Supported cache types.
func NewPartitionEngine(cfg Config, hashFunc HashFunc) *partitionEngineCreates a new partitioned rule engine instance.
Parameters:
cfg: Engine configurationhashFunc: Optional hash function for partitioning (nil uses default)
Returns: Pointer to partitionEngine instance
func (c Config) GetCacheType() intConverts CacheType string to internal cache type constant.
func (c Config) GetFactName() stringReturns the configured fact name or default "Fact".
type ICache interface {
Set(key any, value any, duration time.Duration)
Get(key any) (value any, ok bool)
Has(key any) bool
Keys() []any
Len() int
Clear()
Close()
SetEvictedFunc(f common.EvictedFunc) error
}Cache interface for all cache implementations.
func New(config Config) ICacheCreates a new cache instance based on configuration.
type ConsistentHash struct {
// Contains filtered or unexported fields
}Consistent hashing implementation for key distribution.
type HashFunc func(data []byte) uint32Function signature for hash functions.
func New(replicas int, hashFunc HashFunc) *ConsistentHashCreates a new consistent hash instance.
func (c *ConsistentHash) AddNode(node string)Adds a node to the hash ring.
func (c *ConsistentHash) RemoveNode(node string)Removes a node from the hash ring.
func (c *ConsistentHash) GetNode(key string) stringReturns the node responsible for the given key.
func (c *ConsistentHash) GetNodes() []stringReturns all nodes in the ring.
All functions return appropriate errors that should be checked:
err := grule.Execute(ctx, rule, fact)
if err != nil {
log.Printf("Rule execution failed: %v", err)
}All grule-plus components are thread-safe and can be used concurrently from multiple goroutines.