garage-unix-socket/src/rpc_server.rs

99 lines
2.9 KiB
Rust
Raw Normal View History

2020-04-06 17:55:39 +00:00
use std::net::SocketAddr;
use std::sync::Arc;
2020-04-09 21:45:07 +00:00
use serde::Serialize;
use bytes::IntoBuf;
use hyper::service::{make_service_fn, service_fn};
2020-04-06 17:55:39 +00:00
use hyper::server::conn::AddrStream;
use hyper::{Body, Method, Request, Response, Server, StatusCode};
use futures::future::Future;
use crate::error::Error;
2020-04-09 16:43:53 +00:00
use crate::data::rmp_to_vec_all_named;
use crate::proto::Message;
2020-04-08 20:00:41 +00:00
use crate::server::Garage;
2020-04-09 21:45:07 +00:00
use crate::block::*;
fn debug_serialize<T: Serialize>(x: T) -> Result<String, Error> {
let ss = serde_json::to_string(&x)?;
if ss.len() > 100 {
Ok(ss[..100].to_string())
} else {
Ok(ss)
}
}
2020-04-06 17:55:39 +00:00
fn err_to_msg(x: Result<Message, Error>) -> Message {
match x {
Err(e) => Message::Error(format!("{}", e)),
Ok(msg) => msg,
}
}
2020-04-08 20:00:41 +00:00
async fn handler(garage: Arc<Garage>, req: Request<Body>, addr: SocketAddr) -> Result<Response<Body>, Error> {
if req.method() != &Method::POST {
let mut bad_request = Response::default();
*bad_request.status_mut() = StatusCode::BAD_REQUEST;
return Ok(bad_request);
}
let whole_body = hyper::body::to_bytes(req.into_body()).await?;
2020-04-06 17:55:39 +00:00
let msg = rmp_serde::decode::from_read::<_, Message>(whole_body.into_buf())?;
2020-04-09 21:45:07 +00:00
eprintln!("RPC from {}: {} ({} bytes)", addr, debug_serialize(&msg)?, whole_body.len());
2020-04-06 17:55:39 +00:00
2020-04-08 20:00:41 +00:00
let sys = garage.system.clone();
2020-04-06 17:55:39 +00:00
let resp = err_to_msg(match &msg {
2020-04-06 19:02:15 +00:00
Message::Ping(ping) => sys.handle_ping(&addr, ping).await,
2020-04-06 20:27:51 +00:00
Message::PullStatus => sys.handle_pull_status().await,
Message::PullConfig => sys.handle_pull_config().await,
Message::AdvertiseNodesUp(adv) => sys.handle_advertise_nodes_up(adv).await,
Message::AdvertiseConfig(adv) => sys.handle_advertise_config(adv).await,
2020-04-09 21:45:07 +00:00
Message::PutBlock(m) => {
write_block(garage, &m.hash, &m.data).await
}
Message::GetBlock(h) => {
read_block(garage, &h).await
}
2020-04-08 20:00:41 +00:00
Message::TableRPC(table, msg) => {
if let Some(rpc_handler) = garage.table_rpc_handlers.get(table) {
rpc_handler.handle(&msg[..]).await
.map(|rep| Message::TableRPC(table.to_string(), rep))
} else {
Ok(Message::Error(format!("Unknown table: {}", table)))
}
}
2020-04-06 20:27:51 +00:00
2020-04-06 17:55:39 +00:00
_ => Ok(Message::Error(format!("Unexpected message: {:?}", msg))),
});
2020-04-09 21:45:07 +00:00
eprintln!("reply to {}: {}", addr, debug_serialize(&resp)?);
2020-04-09 16:43:53 +00:00
Ok(Response::new(Body::from(
2020-04-09 16:43:53 +00:00
rmp_to_vec_all_named(&resp)?
)))
}
2020-04-08 20:00:41 +00:00
pub async fn run_rpc_server(garage: Arc<Garage>, shutdown_signal: impl Future<Output=()>) -> Result<(), hyper::Error> {
let bind_addr = ([0, 0, 0, 0], garage.system.config.rpc_port).into();
2020-04-06 17:55:39 +00:00
let service = make_service_fn(|conn: &AddrStream| {
let client_addr = conn.remote_addr();
2020-04-08 20:00:41 +00:00
let garage = garage.clone();
2020-04-06 17:55:39 +00:00
async move {
Ok::<_, Error>(service_fn(move |req: Request<Body>| {
2020-04-08 20:00:41 +00:00
let garage = garage.clone();
handler(garage, req, client_addr)
2020-04-06 17:55:39 +00:00
}))
}
});
2020-04-06 17:55:39 +00:00
let server = Server::bind(&bind_addr).serve(service) ;
let graceful = server.with_graceful_shutdown(shutdown_signal);
2020-04-06 17:55:39 +00:00
println!("RPC server listening on http://{}", bind_addr);
graceful.await
}