A lightweight and efficient multithreaded task executor built in Rust that evaluates Lua scripts using worker threads. This project is ideal for concurrent execution of Lua code using a load-balanced worker pool architecture.
- Lua Execution: Executes Lua code using the rlua library.
- Asynchronous & Concurrent: Built on top of
tokio
for async task handling. - Worker Pool: Load-balancer distributes Lua tasks across a configurable pool of worker threads.
- JSON Input Support: Lua scripts are read from a JSON file (
lua.json
) and dispatched to workers. - Safe Message Passing: Uses Tokio channels for inter-thread communication.
src/
├── load_balancer.rs # Distributes tasks randomly to worker threads
├── main.rs # Entry point: loads scripts and triggers task assignment
├── types.rs # Data structures for Lua scripts, results, and system components
└── worker.rs # Worker implementation that executes Lua code
lua.json # Input Lua scripts in JSON format
Cargo.toml # Rust project configuration
rlua
– Safe high-level bindings to Lua.tokio
– Asynchronous runtime for Rust.serde
– Serialization/deserialization framework.serde_json
– JSON parser/serializer.rand
– Random number generator for load balancing.
Install dependencies with:
cargo build
[
{
"code": "print(\"hello world!\")"
},
{
"code": "function add(a, b) return a + b end print(add(3, 5))"
}
]
Each object in the array contains a code
string with the Lua code to be executed.
- Startup: The system reads
lua.json
and parses it into Lua code tasks. - Load Balancer: Each task is sent to a randomly selected worker.
- Workers: Each worker runs an infinite async loop, waiting for Lua scripts to execute.
- Execution: Lua scripts are run in isolated contexts to ensure thread safety.
- Result Handling: A basic result structure is sent back through channels (can be extended).
cargo run
You can configure the number of workers by modifying this line in main.rs
:
let mut lb: LoadBalancer = LoadBalancer::new(5).unwrap(); // 5 workers
- Better load-balancing strategies (round-robin, task queue length awareness).
- Enhanced result reporting with actual output capturing.
- Fault-tolerance and worker recovery.
- Lua sandboxing and timeout support for untrusted code.
- Web interface or CLI for task submission.
- Unsafe Code: None used.
- Thread Safety: Lua contexts are isolated per worker to avoid race conditions.
- Lua Output: Currently printed to stdout; consider capturing for logging or UI display.
This project is licensed under the MIT License.