Repository navigation
Expand file tree
/
Copy pathhttp.rs
More file actions
155 lines (142 loc) · 6.1 KB
/
Copy pathhttp.rs
File metadata and controls
155 lines (142 loc) · 6.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//! The HTTP service: a pool of routers that answer `POST /v1/{call}` through `respond`.
use crate::{busy, encode, error_body, internal, known, not_found, respond, too_large, Engine, BODY_LIMIT};
use axum::{
body::Bytes,
extract::{rejection::BytesRejection, DefaultBodyLimit, Path, State},
http::{header, StatusCode},
response::{IntoResponse, Response},
routing::{get, post},
Json,
};
use planner_router::{data::RoutingData, Error, Router};
use serde_json::{json, Value};
use std::{
sync::{
atomic::{AtomicBool, Ordering},
Arc, Mutex, PoisonError,
},
time::Duration,
};
use tokio::sync::{OwnedSemaphorePermit, Semaphore};
use tower_http::{compression::CompressionLayer, CompressionLevel};
/// How long a request waits for its permits before it answers `busy`.
const QUEUE_WAIT: Duration = Duration::from_secs(1);
/// The web planner's default profile. It loads before the service accepts requests.
const WARM_PROFILE: &str = "touring";
/// The most routers measured busy at once on the heaviest allowed requests, on the Central
/// Switzerland routing package. Router memory grows with the road count, so a larger package needs
/// a new measurement.
pub const MAX_WORKERS: usize = 12;
struct Workers {
routers: Mutex<Vec<Engine>>,
gate: Gate,
metadata: Value,
}
/// Two classes of calls share the workers. A heavy call, a shape, takes one of
/// `max(1, workers / 2)` heavy permits and then a worker; a light call, a route, takes only a
/// worker. So heavy calls never hold every worker while there are two or more.
struct Gate {
workers: Arc<Semaphore>,
heavy: Arc<Semaphore>,
wait: Duration,
}
/// The permits of one call. Dropping them frees its worker and its heavy permit.
struct Permits {
_worker: OwnedSemaphorePermit,
_heavy: Option<OwnedSemaphorePermit>,
}
impl Gate {
fn new(workers: usize, wait: Duration) -> Self {
let heavy = Arc::new(Semaphore::new((workers / 2).max(1)));
Self { workers: Arc::new(Semaphore::new(workers)), heavy, wait }
}
/// The permits of a call, or `None` when they do not come within `wait` in total.
async fn enter(&self, heavy: bool) -> Option<Permits> {
let permits = async {
let heavy = if heavy { Some(Arc::clone(&self.heavy).acquire_owned().await.ok()?) } else { None };
let worker = Arc::clone(&self.workers).acquire_owned().await.ok()?;
Some(Permits { _worker: worker, _heavy: heavy })
};
tokio::time::timeout(self.wait, permits).await.ok().flatten()
}
}
pub fn app(directory: &std::path::Path, workers: usize) -> Result<axum::Router, Error> {
if !(1..=MAX_WORKERS).contains(&workers) {
return Err(Error::InvalidRequest(format!("Use 1 to {MAX_WORKERS} workers")));
}
// The opened handle has no budget, so it must not outlive the forks' setup.
let routers: Vec<Engine> = {
let source = planner_router::open(directory)?;
let budget = source.default_budget();
(0..workers).map(|_| Router::new(source.fork(), budget)).collect()
};
let package = routers[0].package();
// Forks share prepared profiles, so one load serves every router.
if package.profiles().contains(&WARM_PROFILE) {
package.prepared(WARM_PROFILE)?;
}
let metadata = json!({ "package": package.identity(), "region": package.region(), "bounds": package.bounds(),
"profiles": package.profiles(), "attribution": package.attribution(), "warnings": package.warnings() });
let state = Arc::new(Workers { routers: Mutex::new(routers), gate: Gate::new(workers, QUEUE_WAIT), metadata });
Ok(axum::Router::new()
.route("/health", get(|| async { "ok" }))
.route("/v1/region", get(region))
.route("/v1/{call}", post(call))
.layer(DefaultBodyLimit::max(BODY_LIMIT))
// At brotli's default quality 4, a route answer is larger than with gzip. Quality 6 costs about as much as gzip 6.
.layer(CompressionLayer::new().quality(CompressionLevel::Precise(6)))
.with_state(state))
}
async fn region(State(workers): State<Arc<Workers>>) -> Json<Value> {
Json(workers.metadata.clone())
}
/// Sets the flag when the handler is dropped, so a disconnect cancels the call.
struct Cancel(Arc<AtomicBool>);
impl Drop for Cancel {
fn drop(&mut self) {
self.0.store(true, Ordering::Relaxed);
}
}
async fn call(
State(workers): State<Arc<Workers>>,
Path(call): Path<String>,
body: Result<Bytes, BytesRejection>,
) -> Response {
// Requests that fail without routing never wait for a worker.
if !known(&call) {
return reply(encode(not_found(&call)));
}
let Ok(body) = body else { return reply(encode(error_body(too_large()))) };
let Some(permits) = workers.gate.enter(call == "shape").await else {
return reply(encode(busy()));
};
let cancel = Cancel(Arc::new(AtomicBool::new(false)));
let flag = Arc::clone(&cancel.0);
let result = tokio::task::spawn_blocking(move || {
let _permits = permits;
let routers = &workers.routers;
let mut router = routers.lock().unwrap_or_else(PoisonError::into_inner).pop().expect("a permit holds a router");
let reply = respond(&mut router, &call, &body, &flag);
routers.lock().unwrap_or_else(PoisonError::into_inner).push(router);
reply
})
.await;
reply(result.unwrap_or_else(|error| encode(internal(&format!("A routing task failed: {error}")))))
}
fn reply((status, body): (u16, Vec<u8>)) -> Response {
let status = StatusCode::from_u16(status).expect("a valid status");
(status, [(header::CONTENT_TYPE, "application/json")], body).into_response()
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn a_heavy_call_waits_for_its_share_and_a_route_gets_a_free_worker() {
let gate = Gate::new(2, Duration::from_millis(50));
let _heavy = gate.enter(true).await.unwrap();
assert!(gate.enter(true).await.is_none());
let _light = gate.enter(false).await.unwrap();
// The heavy call holds a worker too, so both workers are busy.
assert!(gate.enter(false).await.is_none());
}
}