Skip to content

Latest commit

 

History

History
113 lines (77 loc) · 3.24 KB

README.md

File metadata and controls

113 lines (77 loc) · 3.24 KB

Rust Lua Multithreaded Executor

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.


🚀 Features

  • 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.

📁 Project Structure

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

📦 Dependencies

  • 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

📄 Example lua.json

[
  {
    "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.


🧠 How It Works

  1. Startup: The system reads lua.json and parses it into Lua code tasks.
  2. Load Balancer: Each task is sent to a randomly selected worker.
  3. Workers: Each worker runs an infinite async loop, waiting for Lua scripts to execute.
  4. Execution: Lua scripts are run in isolated contexts to ensure thread safety.
  5. Result Handling: A basic result structure is sent back through channels (can be extended).

✅ Running the Project

cargo run

🔧 Configuration

You can configure the number of workers by modifying this line in main.rs:

let mut lb: LoadBalancer = LoadBalancer::new(5).unwrap(); // 5 workers

📈 Future Improvements

  • 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.

🛡️ Safety & Limitations

  • 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.

📄 License

This project is licensed under the MIT License.