runtime-rs: timeout for shim management client

Let client side support timeout if the timeout value is set.
If timeout not set, execute directly.

Fixes: #5114
Signed-off-by: Ji-Xinyou <jerryji0414@outlook.com>
This commit is contained in:
Ji-Xinyou 2022-09-07 16:06:47 +08:00
parent 9f13496e13
commit 5add50aea2
5 changed files with 37 additions and 46 deletions

View File

@ -12,5 +12,5 @@ logging::logger_with_subsystem!(sl, "runtimes");
pub mod manager;
pub use manager::RuntimeHandlerManager;
mod shim_mgmt;
pub use shim_mgmt::client::MgmtClient;
pub use shim_mgmt::{client::MgmtClient, server::sb_storage_path};
mod static_resource;

View File

@ -8,10 +8,7 @@ use std::sync::Arc;
use anyhow::{anyhow, Context, Result};
use crate::{
shim_mgmt::server::MgmtServer,
static_resource::StaticResourceManager,
};
use crate::{shim_mgmt::server::MgmtServer, static_resource::StaticResourceManager};
use common::{
message::Message,
types::{Request, Response},

View File

@ -4,8 +4,6 @@
// SPDX-License-Identifier: Apache-2.0
//
#![allow(dead_code)]
// Defines the general client functions used by other components acting like
// clients. To be specific, a client first connect to the socket, then send
// request to destined URL, and finally handle the request(or not)
@ -13,19 +11,20 @@
use std::{path::Path, path::PathBuf, time::Duration};
use super::server::mgmt_socket_addr;
use anyhow::{Context, Result};
use hyper::{Body, Client, Method, Request, Response};
use anyhow::{anyhow, Context, Result};
use hyper::{Body, Client, Response};
use hyperlocal::{UnixClientExt, UnixConnector, Uri};
/// Shim management client with timeout
pub struct MgmtClient {
/// The socket *file path* on host file system
s_path: PathBuf,
sock_path: PathBuf,
/// The http client connect to the long standing shim mgmt server
client: Client<UnixConnector, Body>,
/// timeout value for each dial
/// Timeout value for each dial, usually 200ms will be enough
/// For heavier workload, you may want longer timeout
timeout: Option<Duration>,
}
@ -36,10 +35,10 @@ impl MgmtClient {
let s_addr = unix_socket_path
.strip_prefix("unix:")
.context("failed to strix prefix")?;
let s_path = Path::new("/").join(s_addr).as_path().to_owned();
let sock_path = Path::new("/").join(s_addr).as_path().to_owned();
let client = Client::unix();
Ok(Self {
s_path,
sock_path,
client,
timeout,
})
@ -48,32 +47,15 @@ impl MgmtClient {
/// The http GET method for client, return a raw response. Further handling should be done by caller.
/// Parameter uri should be like "/agent-url" etc.
pub async fn get(&self, uri: &str) -> Result<Response<Body>> {
let url: hyper::Uri = Uri::new(&self.s_path, uri).into();
let response = self.client.get(url).await.context("failed to GET")?;
Ok(response)
}
/// The http PUT method for client
pub async fn put(&self, uri: &str) -> Result<Response<Body>> {
let url: hyper::Uri = Uri::new(&self.s_path, uri).into();
let req = Request::builder()
.method(Method::PUT)
.uri(url)
.body(Body::from(""))
.expect("request builder");
let response = self.client.request(req).await?;
Ok(response)
}
/// The http POST method for client
pub async fn post(&self, uri: &str) -> Result<Response<Body>> {
let url: hyper::Uri = Uri::new(&self.s_path, uri).into();
let req = Request::builder()
.method(Method::POST)
.uri(url)
.body(Body::from(""))
.expect("request builder");
let response = self.client.request(req).await?;
Ok(response)
let url: hyper::Uri = Uri::new(&self.sock_path, uri).into();
let work = self.client.get(url);
match self.timeout {
Some(timeout) => match tokio::time::timeout(timeout, work).await {
Ok(result) => result.map_err(|e| anyhow!(e)),
Err(_) => Err(anyhow!("TIMEOUT")),
},
// if timeout not set, work executes directly
None => work.await.context("failed to GET"),
}
}
}

View File

@ -4,6 +4,6 @@
// SPDX-License-Identifier: Apache-2.0
//
pub mod client;
mod handlers;
pub mod server;
pub mod client;

View File

@ -21,7 +21,7 @@ use tokio::net::UnixListener;
use super::handlers::handler_mux;
pub(crate) const DIRECT_VOLUMN_PATH_KEY: &str = "path";
pub(crate) const DIRECT_VOLUMN_STAT_URL: &str = "/direct-volumn/stats";
pub(crate) const DIRECT_VOLUMN_STATS_URL: &str = "/direct-volumn/stats";
pub(crate) const DIRECT_VOLUMN_RESIZE_URL: &str = "/direct-volumn/resize";
pub(crate) const AGENT_URL: &str = "/agent-url";
pub(crate) const IP_TABLE_URL: &str = "/iptables";
@ -53,10 +53,10 @@ impl MgmtServer {
// TODO(when metrics is supported): register sandbox metrics
// running management http server in an infinite loop, able to serve concurrent requests
pub async fn run(self: Arc<Self>) {
let lsnr = lsnr_from_path(self.s_addr.clone()).await.unwrap();
// start an infinate loop, which serves the incomming uds stream
let listener = listener_from_path(self.s_addr.clone()).await.unwrap();
// start an infinite loop, which serves the incomming uds stream
loop {
let (stream, _) = lsnr.accept().await.unwrap();
let (stream, _) = listener.accept().await.unwrap();
let me = self.clone();
// spawn a light weight thread to multiplex to the handler
tokio::task::spawn(async move {
@ -91,7 +91,7 @@ pub fn mgmt_socket_addr(sid: String) -> String {
// from path, return a unix listener corresponding to that path,
// if the path(socket file) is not created, we create that here
async fn lsnr_from_path(path: String) -> Result<UnixListener> {
async fn listener_from_path(path: String) -> Result<UnixListener> {
// create the socket if not present
let trim_path = path.strip_prefix("unix:").context("trim path")?;
let file_path = Path::new("/").join(trim_path);
@ -103,3 +103,15 @@ async fn lsnr_from_path(path: String) -> Result<UnixListener> {
info!(sl!(), "mgmt-svr: binding to path {}", path);
UnixListener::bind(file_path).context("bind address")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn mgmt_svr_test_sock_addr() {
let sid = String::from("414123");
let addr = mgmt_socket_addr(sid);
assert_eq!(addr, "unix:///run/kata/414123/shim-monitor.sock");
}
}