parsec_client/core/
request_client.rs

1// Copyright 2020 Contributors to the Parsec project.
2// SPDX-License-Identifier: Apache-2.0
3//! Request-level client
4use super::ipc_handler::{self, unix_socket, Connect};
5use crate::error::{ClientErrorKind, Result};
6use derivative::Derivative;
7use parsec_interface::requests::{Request, Response};
8use std::env;
9use std::time::Duration;
10
11const DEFAULT_MAX_BODY_SIZE: usize = usize::max_value();
12
13/// Low level client structure optimised for communicating with the service
14/// at a request level of abstraction.
15///
16/// Usage is recommended when fine control over the request header and IPC handler
17/// is needed.
18#[derive(Derivative)]
19#[derivative(Debug)]
20pub struct RequestClient {
21    /// Max size for response bodies
22    ///
23    /// Defaults to the max value of `usize` on the current platform
24    pub max_body_size: usize,
25    /// Handler for IPC-related functionality
26    ///
27    /// Defaults to using Unix domain sockets
28    #[derivative(Debug = "ignore")]
29    pub ipc_handler: Box<dyn Connect + Send + Sync>,
30}
31
32impl RequestClient {
33    /// Creates a new `RequestClient`, bootstrapping the socket
34    /// location.
35    pub fn new() -> Result<Self> {
36        Ok(RequestClient {
37            ipc_handler: ipc_handler::connector_from_url(url::Url::parse(
38                &env::var("PARSEC_SERVICE_ENDPOINT")
39                    .unwrap_or(format!("unix:{}", unix_socket::DEFAULT_SOCKET_PATH)),
40            )?)?,
41            max_body_size: DEFAULT_MAX_BODY_SIZE,
42        })
43    }
44
45    /// Send a request and get a response.
46    pub fn process_request(&self, request: Request) -> Result<Response> {
47        // Try to connect once, wait for a timeout until trying again.
48        let mut stream = self.ipc_handler.connect()?;
49
50        request
51            .write_to_stream(&mut stream)
52            .map_err(ClientErrorKind::Interface)?;
53        Ok(Response::read_from_stream(&mut stream, self.max_body_size)
54            .map_err(ClientErrorKind::Interface)?)
55    }
56}
57
58impl Default for RequestClient {
59    fn default() -> Self {
60        RequestClient {
61            max_body_size: DEFAULT_MAX_BODY_SIZE,
62            ipc_handler: Box::from(unix_socket::Handler::default()),
63        }
64    }
65}
66
67/// Configuration methods for controlling IPC-level options.
68impl crate::BasicClient {
69    /// Set the maximum body size allowed for requests.
70    ///
71    /// Defaults to the maximum value of `usize`.
72    pub fn set_max_body_size(&mut self, max_body_size: usize) {
73        self.op_client.request_client.max_body_size = max_body_size;
74    }
75
76    /// Set the IPC handler used for communication with the service.
77    ///
78    /// By default the [Unix domain socket client](../ipc_handler/unix_socket/struct.Client.html) is used.
79    pub fn set_ipc_handler(&mut self, ipc_handler: Box<dyn Connect + Send + Sync>) {
80        self.op_client.request_client.ipc_handler = ipc_handler;
81    }
82
83    /// Set the timeout for operations on the IPC stream.
84    ///
85    /// The value defaults to 1 second.
86    pub fn set_timeout(&mut self, timeout: Option<Duration>) {
87        self.op_client
88            .request_client
89            .ipc_handler
90            .set_timeout(timeout);
91    }
92}