From 3467cd91e99fdf69d6359e8798b58c4e9e006f5d Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Wed, 3 Jul 2019 14:26:07 +0200 Subject: [PATCH] src/client/http_client.rs: return Arc Avoid to call canceller for each clone on drop ... --- src/client/http_client.rs | 41 +++++++++++++++------------------------ 1 file changed, 16 insertions(+), 25 deletions(-) diff --git a/src/client/http_client.rs b/src/client/http_client.rs index 57bd8b19..679741d8 100644 --- a/src/client/http_client.rs +++ b/src/client/http_client.rs @@ -269,7 +269,7 @@ impl HttpClient { backup_type: &str, backup_id: &str, debug: bool, - ) -> impl Future { + ) -> impl Future, Error=Error> { let param = json!({"backup-type": backup_type, "backup-id": backup_id, "store": datastore, "debug": debug}); let req = Self::request_builder(&self.server, "GET", "/api2/json/backup", Some(param)).unwrap(); @@ -285,7 +285,7 @@ impl HttpClient { backup_id: &str, backup_time: DateTime, debug: bool, - ) -> impl Future { + ) -> impl Future, Error=Error> { let param = json!({ "backup-type": backup_type, @@ -461,25 +461,22 @@ impl HttpClient { } -#[derive(Clone)] pub struct BackupReader { h2: H2Client, - canceller: Option, + canceller: Canceller, } impl Drop for BackupReader { fn drop(&mut self) { - if let Some(canceller) = self.canceller.take() { - canceller.cancel(); - } + self.canceller.cancel(); } } impl BackupReader { - pub fn new(h2: H2Client, canceller: Canceller) -> Self { - Self { h2, canceller: Some(canceller) } + pub fn new(h2: H2Client, canceller: Canceller) -> Arc { + Arc::new(Self { h2, canceller: canceller }) } pub fn get(&self, path: &str, param: Option) -> impl Future { @@ -522,30 +519,26 @@ impl BackupReader { } pub fn force_close(mut self) { - if let Some(canceller) = self.canceller.take() { - canceller.cancel(); - } + self.canceller.cancel(); } } pub struct BackupClient { h2: H2Client, - canceller: Option, + canceller: Canceller, } impl Drop for BackupClient { fn drop(&mut self) { - if let Some(canceller) = self.canceller.take() { - canceller.cancel(); - } + self.canceller.cancel(); } } impl BackupClient { - pub fn new(h2: H2Client, canceller: Canceller) -> Self { - Self { h2, canceller: Some(canceller) } + pub fn new(h2: H2Client, canceller: Canceller) -> Arc { + Arc::new(Self { h2, canceller }) } pub fn get(&self, path: &str, param: Option) -> impl Future { @@ -560,18 +553,16 @@ impl BackupClient { self.h2.post(path, param) } - pub fn finish(mut self) -> impl Future { - let canceler = self.canceller.take().unwrap(); + pub fn finish(self: Arc) -> impl Future { + let canceller = self.canceller.clone(); self.h2.clone().post("finish", None).map(move |_| { - canceler.cancel(); + canceller.cancel(); () }) } - pub fn force_close(mut self) { - if let Some(canceller) = self.canceller.take() { - canceller.cancel(); - } + pub fn force_close(self) { + self.canceller.cancel(); } pub fn upload_blob_from_data(