public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Max Carrara <m.carrara@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: Re: [pbs-devel] [PATCH proxmox-backup 4/4] pxar: use anyhow::Error in PxarBackupStream
Date: Fri, 16 Feb 2024 18:47:21 +0100	[thread overview]
Message-ID: <e3fee3e7-697a-496f-93a7-02d0e3121ebd@proxmox.com> (raw)
In-Reply-To: <20240216153317.323260-5-g.goller@proxmox.com>

On 2/16/24 16:33, Gabriel Goller wrote:
> Instead of storing the error as a string in the PxarBackupStream, we
> store it as an anyhow::Error. As we can't clone an anyhow::Error, we take
> it out from the mutex and return it. This won't change anything as
> the consumation of the stream will stop if it gets a Some(Err(..)).
> 
> Signed-off-by: Gabriel Goller <g.goller@proxmox.com>
> ---
>  pbs-client/src/pxar_backup_stream.rs | 20 +++++++++++---------
>  1 file changed, 11 insertions(+), 9 deletions(-)
> 
> diff --git a/pbs-client/src/pxar_backup_stream.rs b/pbs-client/src/pxar_backup_stream.rs
> index 22a6ffdc..4dbfd472 100644
> --- a/pbs-client/src/pxar_backup_stream.rs
> +++ b/pbs-client/src/pxar_backup_stream.rs
> @@ -5,7 +5,7 @@ use std::pin::Pin;
>  use std::sync::{Arc, Mutex};
>  use std::task::{Context, Poll};
>  
> -use anyhow::{format_err, Error};
> +use anyhow::Error;
>  use futures::future::{AbortHandle, Abortable};
>  use futures::stream::Stream;
>  use nix::dir::Dir;
> @@ -25,7 +25,7 @@ use pbs_datastore::catalog::CatalogWriter;
>  pub struct PxarBackupStream {
>      rx: Option<std::sync::mpsc::Receiver<Result<Vec<u8>, Error>>>,
>      handle: Option<AbortHandle>,
> -    error: Arc<Mutex<Option<String>>>,
> +    error: Arc<Mutex<Option<Error>>>,
>  }
>  
>  impl Drop for PxarBackupStream {
> @@ -68,7 +68,7 @@ impl PxarBackupStream {
>              .await
>              {
>                  let mut error = error2.lock().unwrap();
> -                *error = Some(err.to_string());
> +                *error = Some(err);
>              }
>          };
>  
> @@ -100,18 +100,20 @@ impl Stream for PxarBackupStream {
>      fn poll_next(self: Pin<&mut Self>, _cx: &mut Context) -> Poll<Option<Self::Item>> {
>          {
>              // limit lock scope
> -            let error = self.error.lock().unwrap();
> -            if let Some(ref msg) = *error {
> -                return Poll::Ready(Some(Err(format_err!("{}", msg))));

This here:

> +            let mut error = self.error.lock().unwrap();
> +            if error.is_some() {
> +                let err = error.take().unwrap();
> +                return Poll::Ready(Some(Err(err)));
>              }

... could be replaced with the following:

    let mut error = self.error.lock().unwrap();
    if let Some(err) = error.take() {
        return Poll::Ready(Some(Err(err)));
    }

`Option::take()` also returns an `Option`, allowing you to omit the `unwrap()`.


>          }
>  
>          match proxmox_async::runtime::block_in_place(|| self.rx.as_ref().unwrap().recv()) {
>              Ok(data) => Poll::Ready(Some(data)),
>              Err(_) => {
> -                let error = self.error.lock().unwrap();
> -                if let Some(ref msg) = *error {
> -                    return Poll::Ready(Some(Err(format_err!("{}", msg))));
> +                let mut error = self.error.lock().unwrap();
> +                if error.is_some() {
> +                    let err = error.take().unwrap();
> +                    return Poll::Ready(Some(Err(err)));
>                  }

Same as above here.

>                  Poll::Ready(None) // channel closed, no error
>              }





  reply	other threads:[~2024-02-16 17:47 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-16 15:33 [pbs-devel] [PATCH proxmox{, -backup} 0/4] output full anyhow context in client Gabriel Goller
2024-02-16 15:33 ` [pbs-devel] [PATCH proxmox 1/4] CLI: print fatal errors including causes Gabriel Goller
2024-02-16 15:33 ` [pbs-devel] [PATCH proxmox-backup 2/4] pxar: remove ArchiveError Gabriel Goller
2024-02-16 15:33 ` [pbs-devel] [PATCH proxmox-backup 3/4] pxar: add UniqueContext helper Gabriel Goller
2024-02-16 17:44   ` Max Carrara
2024-02-16 15:33 ` [pbs-devel] [PATCH proxmox-backup 4/4] pxar: use anyhow::Error in PxarBackupStream Gabriel Goller
2024-02-16 17:47   ` Max Carrara [this message]
2024-02-19 10:53     ` Gabriel Goller
2024-02-16 17:49 ` [pbs-devel] [PATCH proxmox{, -backup} 0/4] output full anyhow context in client Max Carrara
2024-02-16 17:55   ` Max Carrara
2024-02-19 10:54     ` Gabriel Goller
2024-02-20 10:30 ` Gabriel Goller

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=e3fee3e7-697a-496f-93a7-02d0e3121ebd@proxmox.com \
    --to=m.carrara@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal