Skip to content

ErtugrulKra/gRPC-RuleEngine

Repository files navigation

gRPC Rule Engine

A flexible and scalable rule engine implementation using gRPC for microservices architecture. This project demonstrates how to build a rule-based pricing system that can be easily extended and deployed across different services.

🚀 Features

  • gRPC-based Architecture: High-performance, language-agnostic communication
  • JSON-based Rule Configuration: Easy to modify rules without code changes
  • Flexible Rule Engine: Support for complex conditions and actions
  • Scalable Design: Built for microservices and distributed systems
  • Type Safety: Protocol Buffers ensure type-safe communication
  • Concurrent Processing: Multi-threaded server for handling multiple requests

📋 Project Structure

RuleEngine_gRPC/
├── client.py                 # gRPC client implementation
├── server.py                 # gRPC server implementation
├── rule_engine.proto         # Protocol Buffer definition
├── rule_engine_pb2.py        # Generated Python protobuf classes
├── rule_engine_pb2_grpc.py   # Generated Python gRPC classes
├── rules.json               # Rule configuration file
├── README.md                # This file
└── LICENSE                  # MIT License

🛠️ Installation

Prerequisites

  • Python 3.7+
  • pip (Python package manager)

Dependencies

Install the required dependencies:

pip install grpcio grpcio-tools

Generate Protocol Buffer Files

If you modify the .proto file, regenerate the Python files:

python -m grpc_tools.protoc --python_out=. --grpc_python_out=. rule_engine.proto

🚀 Quick Start

1. Start the Server

python server.py

The server will start on localhost:50051 and display:

RuleEngine gRPC Server is running on port 50051...

2. Run the Client

In a new terminal window:

python client.py

This will send sample user data to the server and display the rule evaluation results.

📖 Usage

Rule Configuration

Rules are defined in rules.json with the following structure:

[
  {
    "condition": "membership == 'gold' and age > 18",
    "action": "base_price * 0.80",
    "message": "Gold adult member"
  }
]
  • condition: Python expression that evaluates to True/False
  • action: Python expression that calculates the final price
  • message: Descriptive message for the applied rule

Example Rules

The current configuration includes:

  1. Gold Adult Members: 20% discount for gold members over 18
  2. Gold Young Members: 15% discount for gold members 18 and under
  3. Silver EU Members: 5% discount for silver members in EU region
  4. Default Rule: Regular pricing for all other cases

Client Usage

The client sends user data with the following fields:

  • age: User's age (integer)
  • membership: Membership type (string: "gold", "silver", "platinum")
  • region: Geographic region (string: "EU", "US", etc.)
  • base_price: Base price before discounts (float)

🔧 API Reference

gRPC Service

Service: RuleEngineService

Method: Evaluate

Request (UserRequest):

message UserRequest {
  int32 age = 1;
  string membership = 2;
  string region = 3;
  double base_price = 4;
}

Response (RuleResponse):

message RuleResponse {
  string message = 1;
  double final_price = 2;
}

🎯 Example Output

User: {'age': 25, 'membership': 'gold', 'region': 'EU', 'base_price': 100}
  → Message: Gold adult member, Final Price: $80.00

User: {'age': 17, 'membership': 'gold', 'region': 'US', 'base_price': 120}
  → Message: Gold young member, Final Price: $102.00

User: {'age': 30, 'membership': 'silver', 'region': 'EU', 'base_price': 80}
  → Message: Silver EU member, Final Price: $76.00

🔒 Security Considerations

⚠️ Important: The current implementation uses eval() for rule evaluation, which can be a security risk in production environments. For production use, consider:

  • Using a safer expression evaluator like ast.literal_eval()
  • Implementing a custom rule parser
  • Using a dedicated rule engine library like durable_rules or business-rules

🚀 Extending the System

Adding New Rules

  1. Edit rules.json to add new rule conditions and actions
  2. Restart the server to load new rules
  3. Test with the client

Adding New Fields

  1. Update rule_engine.proto to add new fields to UserRequest
  2. Regenerate protobuf files: python -m grpc_tools.protoc --python_out=. --grpc_python_out=. rule_engine.proto
  3. Update server and client code to handle new fields
  4. Modify rules in rules.json to use new fields

Multi-language Support

Since this uses gRPC and Protocol Buffers, you can easily create clients in other languages:

  • Go: Use protoc with Go plugins
  • Java: Use protoc with Java plugins
  • C++: Use protoc with C++ plugins
  • Node.js: Use @grpc/proto-loader

🤝 Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

👨‍💻 Author

Ertugrul Kra

🙏 Acknowledgments

  • gRPC team for the excellent framework
  • Protocol Buffers for efficient serialization
  • Python community for the rich ecosystem

⭐ If you found this project helpful, please give it a star!

About

A lightweight Python Rule Engine with JSON-based hot-reload and gRPC support.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages