Memlink Runtime
Dynamic module loading and execution framework for Rust
Overview
Memlink Runtime is a high-performance dynamic module loading system that enables plugin architectures, hot-reloadable code, and safe FFI execution. Load shared libraries at runtime, call methods on them, and unload them without restarting your application.
Key Features
- ๐ Dynamic Loading - Load
.so,.dll,.dylibfiles at runtime - ๐ก๏ธ Panic Isolation - Module panics don't crash your application
- ๐ฅ Hot Reload - Replace modules with zero downtime
- ๐ Prometheus Metrics - Built-in observability
- ๐งต Thread-Safe - Concurrent module calls from multiple threads
- ๐ฆ Multi-Module - Load and manage multiple modules simultaneously
- โก Circuit Breaker - Prevent cascading failures with automatic recovery
- ๐๏ธ Request Caching - Cache responses for improved performance
- ๐ Module Pooling - Multiple instances with load balancing
- โค๏ธ Health Checks - Liveness and readiness probes
- ๐ Dependency Management - Track module dependencies and load order
- ๐ฆ Version Management - Side-by-side versions with gradual migration
- ๐ญ Request Batching - Combine requests for better throughput
- ๐ Sandboxing - Resource limits and permission control
- ๐ก Event System - Lifecycle hooks and middleware
- ๐ Auto-Discovery - Watch directories and auto-load modules
Quick Start
Basic Example
use ;
use ModuleRef;
Creating Modules
Modules are shared libraries that export three required functions:
Minimal Module (C)
int
int
int
Build Commands
| Platform | Command |
|---|---|
| Linux | cc -shared -fPIC -O2 -o my_module.so my_module.c |
| Windows | cl /LD my_module.c /Fe:my_module.dll |
| macOS | cc -shared -fPIC -O2 -o my_module.dylib my_module.c |
See ABI Documentation for full specification.
Advanced Usage
Loading Multiple Modules
use ;
use ModuleRef;
use Arc;
use thread;
let runtime = new;
// Load multiple modules
let math = runtime.load?;
let string = runtime.load?;
let crypto = runtime.load?;
// Call concurrently from different threads
let mut handles = vec!;
for in
for h in handles
Hot Reload
use ReloadConfig;
use Duration;
// Reload a module with new version
let config = default
.with_drain_timeout
.with_state_preservation;
let reload_state = runtime.reload_with_config?;
// Old module drains in-flight calls before unloading
Metrics and Monitoring
// Get usage statistics
let usage = runtime.get_usage.unwrap;
println!;
println!;
// Export Prometheus metrics
let metrics = new;
// ... after operations ...
println!;
Circuit Breaker Pattern
use ;
let registry = new;
// Configure circuit breaker
let config = CircuitConfig ;
registry.register;
// Check before calling
if registry.can_execute else
Request Caching
use ;
let config = CacheConfig ;
let cache = new;
let key = generate_key;
// Check cache first
if let Some = cache.get
// Cache miss - compute and cache
let result = runtime.call?;
cache.set;
Ok
Module Pooling & Load Balancing
use ;
let config = PoolConfig ;
let pool = new;
// Add multiple instances
for path in &
// Select instance based on load balancing strategy
if let Some = pool.select_instance
Health Checks
use ;
let config = HealthConfig ;
let registry = new;
let tracker = registry.get_or_create;
// Record health check results
tracker.record_success.await;
// or
tracker.record_failure.await;
// Check health status
match tracker.status.await
Dependency Management
use ;
let graph = new;
// Define dependencies
graph.add_module;
graph.add_module;
graph.add_module;
// Get correct load order
let order = graph.load_order?; // ["database", "cache", "api"]
// Load in order
for module_name in order
Version Management
use ;
let manager = new;
// Register multiple versions
manager.register_version;
manager.register_version;
// Activate v1
manager.activate_version;
// Configure gradual migration (10% to v2)
let routing = TrafficRouting ;
manager.set_routing;
// Select version for each request
if let Some = manager.select_version
Event System
use ;
;
let registry = new;
registry.register;
// Events are automatically emitted during module operations
Architecture
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Your Application โ
โ (Memlink Runtime) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Runtime Components โ
โ โโ Resolver (locate modules) โ
โ โโ Loader (load shared libs) โ
โ โโ Instance Manager (track loaded modules) โ
โ โโ Panic Handler (catch module panics) โ
โ โโ Circuit Breaker (fault tolerance) โ
โ โโ Request Cache (performance) โ
โ โโ Module Pool (load balancing) โ
โ โโ Health Tracker (liveness/readiness) โ
โ โโ Dependency Graph (load order) โ
โ โโ Version Manager (gradual migration) โ
โ โโ Request Batcher (throughput) โ
โ โโ Sandbox (resource limits) โ
โ โโ Event Registry (lifecycle hooks) โ
โ โโ Discovery (auto-loading) โ
โ โโ Metrics (Prometheus-compatible) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโ [module_a.so] โโ memlink_init/call/shutdown
โโโ [module_b.dll] โโ memlink_init/call/shutdown
โโโ [module_c.dylib] โโ memlink_init/call/shutdown
Components
| Component | Description |
|---|---|
| Runtime | High-level API for module management |
| Resolver | Locates and validates module files |
| Loader | Loads shared libraries and resolves symbols |
| Instance | Represents a loaded module |
| Arena | Fast bump allocator for module memory |
| Metrics | Collects and exports runtime statistics |
| CircuitBreaker | Fault tolerance with automatic recovery |
| RequestCache | Response caching with TTL |
| ModulePool | Instance pooling with load balancing |
| HealthTracker | Liveness and readiness probes |
| DependencyGraph | Module dependency tracking |
| VersionManager | Side-by-side version management |
| RequestBatcher | Request batching for throughput |
| ResourceTracker | Resource usage monitoring |
| EventRegistry | Lifecycle event emission |
| ModuleDiscovery | Directory watching and auto-loading |
Performance
| Operation | Latency | Throughput |
|---|---|---|
| Module load | 92 ฮผs | - |
| Method call (64 bytes) | 210 ns | 4.7M calls/sec (single thread) |
| Module unload | 52 ฮผs | - |
| Hot reload | 237 ฮผs | - |
| Concurrent calls (8 threads) | - | 2.5M calls/sec |
| Memory overhead | 0.7 MB/module | - |
Key Highlights:
- โ Sub-microsecond call latency
- โ Linear scalability up to 8 threads
- โ Fast hot reload with zero downtime
- โ Low memory footprint
See Performance Benchmarks for detailed methodology, full results, and optimization recommendations.
Use Cases
Plugin Systems
Load user-created plugins without recompiling your application:
// Load all plugins from a directory
for entry in read_dir?
Hot-Reloadable Business Logic
Update logic in production without downtime:
// Watch for file changes
new?;
Sandboxed Execution
Isolate risky code that might panic:
// Module panics are caught and converted to errors
match runtime.call
API Reference
Core Traits
| Trait | Purpose |
|---|---|
ModuleRuntime |
Main interface for module operations |
ModuleResolver |
Resolve module references to artifacts |
Key Types
| Type | Description |
|---|---|
Runtime |
Default runtime implementation |
ModuleHandle |
Opaque handle to loaded module |
ModuleRef |
Module reference (path or registry) |
ModuleUsage |
Usage statistics per module |
ReloadState |
Tracks hot-reload operations |
CircuitBreaker |
Circuit breaker for fault tolerance |
CircuitRegistry |
Registry of circuit breakers |
RequestCache |
Response cache with TTL |
ModulePool |
Pool of module instances |
HealthTracker |
Health check tracker |
HealthRegistry |
Registry of health trackers |
DependencyGraph |
Module dependency graph |
VersionManager |
Version management |
TrafficRouting |
Gradual migration config |
RequestBatcher |
Request batching |
ResourceTracker |
Resource usage tracking |
EventRegistry |
Event emission |
ModuleDiscovery |
Module auto-discovery |
Examples
Run the included examples:
# Multi-module concurrent calls
# Build test modules first (Linux/WSL)
See examples/modules/README.md for module documentation.
License
Memlink Runtime is licensed under the Apache License 2.0 (LICENSE.
Contributing
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch
- Run
cargo testandcargo clippy - Submit a pull request
Development
# Build
# Test
# Lint
# Format
# Build docs
# Run integration tests
Testing
The runtime includes comprehensive test coverage:
- 41 unit tests - Testing individual components
- 13 integration tests - Testing all 10 enterprise features
- 6 platform-specific tests - Linux-only module tests
# Run all tests
# Run only integration tests
# Run with output
Related Crates
- memlink-shm - Shared memory IPC
- libloading - Underlying library loading
- dashmap - Concurrent hash map
Support
- Issues: GitHub Issues
- Documentation: docs.rs
- ABI Spec: docs/abi.md