@sinkaroid/rust-shapeshift 0.0.0-stage → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +145 -2
- package/bin/rust-shapeshift.js +29 -0
- package/package.json +45 -4
- package/skills/rust-shapeshift/SKILL.md +282 -0
- package/skills/rust-shapeshift/resources/adaptive_concurrency_and_load_shedding.md +122 -0
- package/skills/rust-shapeshift/resources/anti_bot_and_cookie_challenges.md +139 -0
- package/skills/rust-shapeshift/resources/arena_allocation_and_bump_memory.md +83 -0
- package/skills/rust-shapeshift/resources/async_cancellation_safety_and_task_drainage.md +158 -0
- package/skills/rust-shapeshift/resources/axum_and_tower_architecture.md +145 -0
- package/skills/rust-shapeshift/resources/bounded_caching_and_pooling.md +105 -0
- package/skills/rust-shapeshift/resources/bounded_streaming_and_payload_guards.md +147 -0
- package/skills/rust-shapeshift/resources/bounds_check_elimination_and_zero_cost_memory.md +122 -0
- package/skills/rust-shapeshift/resources/cache_line_alignment_and_struct_layout.md +121 -0
- package/skills/rust-shapeshift/resources/compiler_niche_optimizations_and_size_assertions.md +126 -0
- package/skills/rust-shapeshift/resources/concurrency_model_checking_and_resource_budgets.md +170 -0
- package/skills/rust-shapeshift/resources/concurrency_primitives_and_lock_contention.md +158 -0
- package/skills/rust-shapeshift/resources/docker_slimming_and_build_hygiene.md +115 -0
- package/skills/rust-shapeshift/resources/dom_and_scraper_pruning.md +157 -0
- package/skills/rust-shapeshift/resources/framework_and_stack_selection_matrix.md +143 -0
- package/skills/rust-shapeshift/resources/graphql_memory_and_query_containment.md +192 -0
- package/skills/rust-shapeshift/resources/grpc_tonic_and_http2_memory_tuning.md +117 -0
- package/skills/rust-shapeshift/resources/hidden_drop_offload_and_wcet_contracts.md +255 -0
- package/skills/rust-shapeshift/resources/hot_path_string_and_zero_cost_helpers.md +113 -0
- package/skills/rust-shapeshift/resources/instruction_cache_and_cold_path_outlining.md +135 -0
- package/skills/rust-shapeshift/resources/io_uring_and_zero_syscall_networking.md +146 -0
- package/skills/rust-shapeshift/resources/jemalloc_profiling_and_allocator_switch.md +145 -0
- package/skills/rust-shapeshift/resources/kubernetes_runtime_and_resource_containment.md +207 -0
- package/skills/rust-shapeshift/resources/memory_allocator_tuning.md +125 -0
- package/skills/rust-shapeshift/resources/message_queue_and_stream_backpressure.md +125 -0
- package/skills/rust-shapeshift/resources/numa_architecture_and_hugepages.md +169 -0
- package/skills/rust-shapeshift/resources/ownership_borrowing_and_memory_decisions.md +253 -0
- package/skills/rust-shapeshift/resources/profile_guided_optimization_and_fast_hashing.md +94 -0
- package/skills/rust-shapeshift/resources/rate_limiting_and_state.md +140 -0
- package/skills/rust-shapeshift/resources/realtime_websocket_and_connection_scaling.md +251 -0
- package/skills/rust-shapeshift/resources/redis_connection_and_caching_pitfalls.md +230 -0
- package/skills/rust-shapeshift/resources/reqwest_client_and_connection_pooling.md +130 -0
- package/skills/rust-shapeshift/resources/simd_vectorization_and_hardware_acceleration.md +108 -0
- package/skills/rust-shapeshift/resources/sqlx_and_database_pool_memory.md +156 -0
- package/skills/rust-shapeshift/resources/stack_inlining_and_allocation_reduction.md +134 -0
- package/skills/rust-shapeshift/resources/streaming_file_io_and_compression.md +97 -0
- package/skills/rust-shapeshift/resources/telemetry_and_metrics.md +170 -0
- package/skills/rust-shapeshift/resources/thread_per_core_and_shared_nothing.md +149 -0
- package/skills/rust-shapeshift/resources/tokio_thread_starvation_and_cpu_offload.md +124 -0
- package/skills/rust-shapeshift/resources/typestate_domain_proofs_and_buffer_recycling.md +254 -0
- package/skills/rust-shapeshift/resources/wasm_runtime_and_memory_sandboxing.md +114 -0
- package/skills/rust-shapeshift/resources/zero_allocation_error_architecture.md +130 -0
- package/skills/rust-shapeshift/resources/zero_copy_ipc_and_shared_memory.md +99 -0
- package/skills/rust-shapeshift/resources/zero_copy_serde_and_streaming_serialization.md +159 -0
- package/skills/rust-shapeshift/resources/zero_overhead_distributed_tracing.md +109 -0
- package/skills/rust-shapeshift/resources/zero_panic_and_router_architecture.md +104 -0
- package/skills/rust-shapeshift/resources/zero_runtime_and_minimalist_daemons.md +156 -0
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
# Zero-Runtime Minimalist Daemons: The Sub-Megabyte RSS Pattern (The Koburi Model)
|
|
2
|
+
|
|
3
|
+
A common anti-pattern in modern Rust engineering is blindly introducing `tokio`, `axum`, and multi-threaded async executors into lightweight daemons, monitoring sidecars, CLI utilities, or low-QPS internal services. A multi-threaded async runtime pays a non-trivial memory tax upfront: worker thread stacks, scheduling queues, timer wheels, and glibc arenas consume 10MB to 40MB of RSS just sitting completely idle.
|
|
4
|
+
|
|
5
|
+
When the target environment is a memory-constrained host (e.g. a 512MB VPS or an edge container with dozens of co-located services), the tool itself must never become part of the memory problem it monitors. Battle-tested in production (e.g. `koburi`), an optimized zero-runtime Rust daemon can run 24/7 with continuous minute-by-minute scraping at **sub-megabyte memory usage (down to ~50KB RSS)**.
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## 1. The Decision Matrix: Async Runtime vs Plain Synchronous
|
|
10
|
+
|
|
11
|
+
Before adding a single async dependency, classify the service workload:
|
|
12
|
+
|
|
13
|
+
| Workload Property | High-Throughput Service (Archetype A) | Minimalist Daemon / Sidecar (Archetype B) |
|
|
14
|
+
| :--- | :--- | :--- |
|
|
15
|
+
| **Concurrency Target** | 1,000 to 500,000 concurrent sockets | 1 to 5 concurrent connections (polling / low QPS) |
|
|
16
|
+
| **Request Cadence** | Continuous stream (>1,000 QPS) | Sporadic (e.g. health checks every 10–30s) |
|
|
17
|
+
| **Runtime Selection** | **Tokio multi-thread** / Axum | **Zero Async Runtime** (`std::net` / `std::thread`) |
|
|
18
|
+
| **Idle Memory Budget** | 20MB – 50MB RSS | **Sub-megabyte (<1MB, down to ~50KB RSS)** |
|
|
19
|
+
| **Connection Strategy** | Persistent connection pools | On-demand ephemeral sockets |
|
|
20
|
+
| **Linking Target** | `glibc` with jemalloc/malloc_trim | `x86_64-unknown-linux-musl` static linking |
|
|
21
|
+
|
|
22
|
+
---
|
|
23
|
+
|
|
24
|
+
## 2. Rule 1: No Async Runtime for Low-QPS Daemons
|
|
25
|
+
|
|
26
|
+
If a daemon serves one or two endpoints hit on a polling interval (e.g., container metrics, node health probes, local proxying), use `std::net::TcpListener` with a synchronous accept loop.
|
|
27
|
+
|
|
28
|
+
A single-threaded or sequential accept loop pays **0 bytes** of executor overhead at idle:
|
|
29
|
+
|
|
30
|
+
```rust
|
|
31
|
+
use std::io::{Read, Write};
|
|
32
|
+
use std::net::{TcpListener, TcpStream};
|
|
33
|
+
|
|
34
|
+
pub fn run_sub_5mb_server(bind_addr: &str) -> std::io::Result<()> {
|
|
35
|
+
let listener = TcpListener::bind(bind_addr)?;
|
|
36
|
+
println!("Minimalist daemon listening on {bind_addr} (Zero Async Runtime)");
|
|
37
|
+
|
|
38
|
+
for stream in listener.incoming() {
|
|
39
|
+
match stream {
|
|
40
|
+
Ok(stream) => {
|
|
41
|
+
// Handled sequentially with 0 thread stack allocation
|
|
42
|
+
if let Err(e) = handle_connection(stream) {
|
|
43
|
+
eprintln!("Connection error: {e}");
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
Err(e) => eprintln!("Accept error: {e}"),
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
Ok(())
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
fn handle_connection(mut stream: TcpStream) -> std::io::Result<()> {
|
|
54
|
+
// Fixed 1KB stack buffer: 0 heap allocations
|
|
55
|
+
let mut buffer = [0u8; 1024];
|
|
56
|
+
let bytes_read = stream.read(&mut buffer)?;
|
|
57
|
+
|
|
58
|
+
if bytes_read > 0 && buffer.starts_with(b"GET /stats") {
|
|
59
|
+
let stats_json = collect_metrics_snapshot();
|
|
60
|
+
let response = format!(
|
|
61
|
+
"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}",
|
|
62
|
+
stats_json.len(),
|
|
63
|
+
stats_json
|
|
64
|
+
);
|
|
65
|
+
stream.write_all(response.as_bytes())?;
|
|
66
|
+
} else {
|
|
67
|
+
stream.write_all(b"HTTP/1.1 404 NOT FOUND\r\nConnection: close\r\n\r\n")?;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Socket automatically dropped and closed here: 0 lingering socket RAM
|
|
71
|
+
Ok(())
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
fn collect_metrics_snapshot() -> &'static str {
|
|
75
|
+
"{\"status\":\"healthy\",\"uptime\":3600}"
|
|
76
|
+
}
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
---
|
|
80
|
+
|
|
81
|
+
## 3. Rule 2: On-Demand Ephemeral Connections Over Pooling
|
|
82
|
+
|
|
83
|
+
In high-QPS systems, connection pooling saves handshake latency. But in low-cadence daemons (hitting a local Docker socket `/var/run/docker.sock` once every 15 seconds), holding an open connection pool is an anti-pattern:
|
|
84
|
+
- A pooled connection reserves kernel TCP/socket buffers.
|
|
85
|
+
- It can go stale or break mid-request.
|
|
86
|
+
|
|
87
|
+
**Best Practice**: Connect on demand, execute request, close immediately:
|
|
88
|
+
|
|
89
|
+
```rust
|
|
90
|
+
#[cfg(unix)]
|
|
91
|
+
use std::os::unix::net::UnixStream;
|
|
92
|
+
use std::io::{Read, Write};
|
|
93
|
+
|
|
94
|
+
#[cfg(unix)]
|
|
95
|
+
pub fn query_docker_stats_on_demand() -> std::io::Result<String> {
|
|
96
|
+
// 1. Open on-demand socket
|
|
97
|
+
let mut socket = UnixStream::connect("/var/run/docker.sock")?;
|
|
98
|
+
|
|
99
|
+
// 2. Write HTTP request to daemon
|
|
100
|
+
socket.write_all(b"GET /containers/json HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n")?;
|
|
101
|
+
|
|
102
|
+
// 3. Read response into bounded buffer
|
|
103
|
+
let mut response = String::with_capacity(4096);
|
|
104
|
+
socket.read_to_string(&mut response)?;
|
|
105
|
+
|
|
106
|
+
// 4. Socket closed on exit: 0 idle connection RAM held
|
|
107
|
+
Ok(response)
|
|
108
|
+
}
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
---
|
|
112
|
+
|
|
113
|
+
## 4. Rule 3: `musl` Static Linking to Eliminate Allocator Arenas
|
|
114
|
+
|
|
115
|
+
Dynamic linking against Linux `glibc` pulls in glibc's multi-threaded arena allocator by default, adding baseline memory overhead. Compiling against `musl` statically bundles a lean, simple allocator with zero dynamic linker resolution cost:
|
|
116
|
+
|
|
117
|
+
```bash
|
|
118
|
+
# Build fully static Linux binary
|
|
119
|
+
cargo build --release --target x86_64-unknown-linux-musl
|
|
120
|
+
strip target/x86_64-unknown-linux-musl/release/my-daemon
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
Verify compliance:
|
|
124
|
+
```bash
|
|
125
|
+
ldd target/x86_64-unknown-linux-musl/release/my-daemon
|
|
126
|
+
# Output must be: "not a dynamic executable"
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
Configure `Cargo.toml` for size and dead-code elimination:
|
|
130
|
+
```toml
|
|
131
|
+
[profile.release]
|
|
132
|
+
opt-level = "z"
|
|
133
|
+
lto = "fat"
|
|
134
|
+
codegen-units = 1
|
|
135
|
+
panic = "abort"
|
|
136
|
+
strip = true
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
---
|
|
140
|
+
|
|
141
|
+
## 5. Rule 4: Zero Dependencies Discipline (Stdlib-First)
|
|
142
|
+
|
|
143
|
+
Every external crate introduced can bring hidden threads, global statics, or background memory allocation. For minimalist daemons:
|
|
144
|
+
- Use `std::net`, `std::os::unix::net`, `std::io`, `std::thread`.
|
|
145
|
+
- Hand-roll minimal HTTP response headers and JSON serialization if endpoints are simple.
|
|
146
|
+
- Keep `Cargo.toml` `[dependencies]` strictly empty or limited to zero-dependency core crates.
|
|
147
|
+
|
|
148
|
+
---
|
|
149
|
+
|
|
150
|
+
## 6. Verification Checklist
|
|
151
|
+
|
|
152
|
+
- [ ] Evaluated project archetype: Is async concurrency actually needed, or is sequential/blocking I/O sufficient?
|
|
153
|
+
- [ ] Confirmed 0 Tokio dependencies in `Cargo.toml` for low-QPS daemons.
|
|
154
|
+
- [ ] Idle RSS verified with `ps -o rss -p $(pgrep <bin>)` to be **sub-megabyte (down to ~50KB in continuous production)**.
|
|
155
|
+
- [ ] Static `musl` binary produced with `x86_64-unknown-linux-musl`.
|
|
156
|
+
- [ ] Ephemeral sockets closed immediately after processing (`Connection: close`).
|