[pdm-devel] [PATCH datacenter-manager 1/7] server: generic multi-client wrapper

Lukas Wagner l.wagner at proxmox.com
Tue Feb 11 15:50:24 CET 2025



On  2025-02-04 10:55, Wolfgang Bumiller wrote:
> +
> +// doing this via a generic method is currently tedious as it requires an extra helper trait to
> +// declare the flow of the lifetime in the `self.request` vs `self.streaming_request` function from
> +// its input to its generic output future... and then you run into borrow-checker limitations...
> +macro_rules! try_request {
> +    ($self:expr, $method:expr, $path_and_query:expr, $params:expr, $how:ident) => {
> +        let params = $params.map(serde_json::to_value);
> +        Box::pin(async move {
> +            let params = params
> +                .transpose()
> +                .map_err(|err| proxmox_client::Error::Anyhow(err.into()))?;
> +
> +            let mut last_err = None;
> +            let mut timed_out = false;
> +            // The iterator in use here will automatically mark a client as faulty if we move on to
> +            // the `next()` one.
> +            for client in $self.try_clients() {
> +                if let Some(err) = last_err.take() {
> +                    log::error!("API client error, trying another remote - {err:?}");
> +                }
> +                if timed_out {
> +                    timed_out = false;
> +                    log::error!("API client timed out, trying another remote");
> +                }
> +
> +                let request = client.$how($method.clone(), $path_and_query, params.as_ref());
> +                match tokio::time::timeout($self.timeout, request).await {
> +                    Ok(Err(proxmox_client::Error::Client(err))) => {
> +                        last_err = Some(err);
> +                    }
> +                    Ok(result) => return result,
> +                    Err(_) => {
> +                        timed_out = true;
> +                    }
> +                }
> +            }

maybe add another 

if let Some(err) = last_err {
    log::error!("... {err} ...");
}

so that the actual `err` from the last client you tried is also logged?
Also if the *last* client to try times out I guess we would like to log that as well?

> +
> +            Err(proxmox_client::Error::Other(
> +                "failed to perform API request",
> +            ))
> +        })
> +    };
> +}

-- 
- Lukas





More information about the pdm-devel mailing list