Skip to content

Commit dd2cb1d

Browse files
committed
docs: add Mohist design and admission control spec
1 parent 399e478 commit dd2cb1d

File tree

2 files changed

+61
-9
lines changed

2 files changed

+61
-9
lines changed

README.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,31 @@
33
Raw is a Rust-native web server framework focused on ergonomic routing, middleware,
44
and extensibility while keeping the core small and explicit.
55

6+
## Why Raw (Mohist Design)
7+
Raw addresses a common infrastructure gap in Rust services: predictable latency
8+
under load for multi-tenant workloads. Instead of pushing admission control into
9+
external proxies, Raw makes capacity explicit and fair at the framework level.
10+
11+
**Mohist principles in practice:**
12+
- Universal benefit through fair admission control.
13+
- Frugality via minimal overhead primitives.
14+
- Clarity by exposing explicit route policies.
15+
- Non-aggression by shedding load early.
16+
617
## Features
718
- Async HTTP server built on Tokio + Hyper
819
- Declarative routing with params and wildcards
920
- Middleware chain (request ID, logging, static files)
1021
- Response helpers for JSON, HTML, and text
22+
- Admission control with route policies and global limits
1123
- CLI scaffolding for new projects
1224

1325
## Terminal Preview
1426
![cargo test output](docs/assets/cargo-test.svg)
1527

1628
## Quickstart
1729
```rust
18-
use raw::{App, Response, Text};
30+
use raw::{App, Text};
1931

2032
#[tokio::main]
2133
async fn main() {
@@ -25,6 +37,27 @@ async fn main() {
2537
}
2638
```
2739

40+
## Route Policies (Mohist Admission Control)
41+
```rust
42+
use raw::{App, RoutePolicy, Text};
43+
44+
#[tokio::main]
45+
async fn main() {
46+
let mut app = App::new();
47+
48+
let policy = RoutePolicy {
49+
max_in_flight: Some(16),
50+
cost: 1,
51+
};
52+
53+
app.get_with("/reports/:id", policy, |_req| async {
54+
Text::new("report")
55+
});
56+
57+
app.listen("127.0.0.1:3000").await.unwrap();
58+
}
59+
```
60+
2861
## CLI
2962
```bash
3063
cargo run -p raw-cli -- new my-raw-app

spec.md

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@
33
## Overview
44
Raw is a Rust-native web server framework focused on ergonomic routing, middleware, and extensibility while retaining strong performance and explicit control. The framework targets building HTTP services and web applications with minimal boilerplate and a clear, composable API.
55

6+
## Unique Infrastructure Need
7+
Rust server teams building multi-tenant services often struggle with predictable latency under load. Existing frameworks tend to leave admission control to external proxies or bespoke middleware. Raw addresses this gap with built-in, per-route admission control designed for fairness, frugality, and explicit resource budgeting.
8+
9+
## Mohist Design Principles
10+
Raw adopts Mohist principles as engineering constraints:
11+
- **Universal benefit**: fairness across routes and tenants by default.
12+
- **Frugality**: minimal overhead, no hidden allocations.
13+
- **Merit and clarity**: explicit policies for cost and concurrency.
14+
- **Non-aggression**: shed load early to protect system health.
15+
- **Plain design**: simple primitives over implicit magic.
16+
617
## Goals
718
- Provide a production-ready HTTP framework with a clean, opinionated core.
819
- Offer fast request routing, middleware composition, and structured responses.
@@ -39,6 +50,11 @@ Raw is a Rust-native web server framework focused on ergonomic routing, middlewa
3950
- Koa-style middleware chain using async handlers.
4051
- Built-in middleware: logging, request ID, CORS, compression, static files.
4152

53+
### Admission Control (Mohist Core)
54+
- Per-route policies for max in-flight requests and cost units.
55+
- Global concurrency limit to protect system capacity.
56+
- Fast rejection with `503 Service Unavailable` when capacity is exhausted.
57+
4258
### Error Handling
4359
- Unified error type with HTTP status mapping.
4460
- Custom error handlers per app or per route.
@@ -72,7 +88,7 @@ Raw is a Rust-native web server framework focused on ergonomic routing, middlewa
7288

7389
### Modules
7490
- `server` - listener, runtime, graceful shutdown
75-
- `router` - route table, matchers
91+
- `router` - route table, matchers, route policies
7692
- `request` - request parsing, body extraction
7793
- `response` - response builders, body types
7894
- `middleware` - middleware traits and chain
@@ -83,19 +99,21 @@ Raw is a Rust-native web server framework focused on ergonomic routing, middlewa
8399

84100
## API Sketch
85101
```rust
86-
use raw::{App, Router, response::Html};
102+
use raw::{App, RoutePolicy, Text};
87103

88104
#[tokio::main]
89105
async fn main() {
90106
let mut app = App::new();
91107

92-
app.get("/", |_req| async {
93-
Html::new("<h1>Hello from Raw</h1>")
94-
});
108+
app.get("/", |_req| async { Text::new("Hello from Raw") });
109+
110+
let policy = RoutePolicy {
111+
max_in_flight: Some(16),
112+
cost: 1,
113+
};
95114

96-
app.get("/users/:id", |req| async move {
97-
let id = req.param("id");
98-
raw::response::Json::new(serde_json::json!({"id": id}))
115+
app.get_with("/reports/:id", policy, |_req| async {
116+
Text::new("report")
99117
});
100118

101119
app.listen("127.0.0.1:3000").await.unwrap();
@@ -108,6 +126,7 @@ async fn main() {
108126
3. Static files and JSON helpers.
109127
4. CLI scaffolding and templates.
110128
5. Observability and configuration polish.
129+
6. Mohist admission control.
111130

112131
## Quality Targets
113132
- 90%+ test coverage on core routing and middleware.

0 commit comments

Comments
 (0)