An open API service indexing awesome lists of open source software.

https://github.com/hungpdn/grule-plus

Fast, scalable and flexible rule engine for Go.
https://github.com/hungpdn/grule-plus

golang rule-based rule-engine scalability

Last synced: about 1 month ago
JSON representation

Fast, scalable and flexible rule engine for Go.

Awesome Lists containing this project

README

          

# grule-plus

[![Go Version](https://img.shields.io/badge/go-%3E%3D1.21-blue.svg)](https://golang.org/)
[![CI](https://github.com/hungpdn/grule-plus/workflows/CI/badge.svg)](https://github.com/hungpdn/grule-plus/actions)
[![codecov](https://codecov.io/gh/hungpdn/grule-plus/branch/main/graph/badge.svg)](https://codecov.io/gh/hungpdn/grule-plus)
[![Go Report Card](https://goreportcard.com/badge/github.com/hungpdn/grule-plus)](https://goreportcard.com/report/github.com/hungpdn/grule-plus)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

**grule-plus** is a high-performance, extensible rule engine built on top of [Grule Rule Engine](https://github.com/hyperjumptech/grule-rule-engine). It provides advanced caching, partitioning, and flexible configuration for scalable rule evaluation in Go applications.

---

## Features

- **Pluggable Cache Engines:** Supports LRU (Least Recently Used), LFU (Least Frequently Used), ARC (Adaptive Replacement Cache), and RANDOM cache strategies.
- **Partitioned Rule Engine:** Scale horizontally with partitioned engines for concurrent rule evaluation.
- **Consistent Hashing:** Efficient key distribution across partitions with minimal remapping on node changes.
- **Flexible TTL & Cleanup:** Per-rule time-to-live and periodic cleanup for cache entries.
- **Structured Logging:** Integrated with Go's `slog` for context-aware, structured logs.
- **Runtime Stats:** Built-in runtime statistics for monitoring and debugging.
- **Thread-Safe:** Safe for concurrent use in multi-goroutine environments.

---

## Benchmarks

Comprehensive benchmarks are available in the `benchmark/` directory, testing cache performance, engine throughput, and memory usage across different configurations. See [benchmark/README.md](benchmark/README.md) for detailed results and usage instructions.

---

## Getting Started

### Installation

```sh
go get github.com/hungpdn/grule-plus
```

### Example Usage

```go
import (
"context"
"github.com/hungpdn/grule-plus/engine"
)

func main() {
cfg := engine.Config{
Type: engine.LRU,
Size: 1000,
CleanupInterval: 10,
TTL: 60,
Partition: 1,
FactName: "DiscountFact",
}
grule := engine.NewPartitionEngine(cfg, nil)

rule := "DiscountRule"
statement := `rule DiscountRule "Apply discount" salience 10 {
when
DiscountFact.Amount > 100
then
DiscountFact.Discount = 10;
Retract("DiscountRule");
}`
_ = grule.AddRule(rule, statement, 60)

fact := struct {
Amount int
Discount int
}{Amount: 150}

_ = grule.Execute(context.Background(), rule, &fact)

fmt.Printf("fact.Discount = 10: %v", fact.Discount)
}
```

---

## Configuration

See `engine.Config` for all available options:

- `Type`: Cache type (LRU, LFU, ARC, TWOQ, RANDOM)
- `Size`: Maximum cache size
- `CleanupInterval`: Cache cleanup interval (seconds)
- `TTL`: Default time-to-live for rules (seconds)
- `Partition`: Number of partitions for parallelism

---

## Documentation

Comprehensive documentation is available in the `docs/` directory:

- **[API Reference](docs/api.md)** - Complete API documentation
- **[Architecture](docs/architecture.md)** - System design and components
- **[Cache Types](docs/cache-types.md)** - Cache algorithm explanations
- **[Configuration](docs/configuration.md)** - Setup and tuning guide
- **[Benchmarks](docs/benchmarks.md)** - Performance testing and results
- **[Examples](docs/examples.md)** - Usage patterns and code samples

### Building Documentation

```bash
# Install MkDocs (optional)
pip install mkdocs mkdocs-material

# Serve docs locally
cd docs && mkdocs serve

# Or use godoc for API docs
godoc -http=:6060
```

---

## Security

Please report security vulnerabilities by LinkedIn [**hungpdn**](https://www.linkedin.com/in/hungpdn/) (not through public issues). See [SECURITY.md](SECURITY.md) for details.

---

## TODO

- Metrics & Monitoring System (execution time, success/failure rates, cache hit/miss ratios, ...)

---

## Contributing

We welcome contributions! Please see our [Contributing Guide](CONTRIBUTING.md) for details.

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.