src/client/http_client.rs: return Arc<Self>

Avoid to call canceller for each clone on drop ...
This commit is contained in:
Dietmar Maurer 2019-07-03 14:26:07 +02:00
parent 1e0784841c
commit 3467cd91e9

View File

@ -269,7 +269,7 @@ impl HttpClient {
backup_type: &str, backup_type: &str,
backup_id: &str, backup_id: &str,
debug: bool, debug: bool,
) -> impl Future<Item=BackupClient, Error=Error> { ) -> impl Future<Item=Arc<BackupClient>, Error=Error> {
let param = json!({"backup-type": backup_type, "backup-id": backup_id, "store": datastore, "debug": debug}); 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(); let req = Self::request_builder(&self.server, "GET", "/api2/json/backup", Some(param)).unwrap();
@ -285,7 +285,7 @@ impl HttpClient {
backup_id: &str, backup_id: &str,
backup_time: DateTime<Local>, backup_time: DateTime<Local>,
debug: bool, debug: bool,
) -> impl Future<Item=BackupReader, Error=Error> { ) -> impl Future<Item=Arc<BackupReader>, Error=Error> {
let param = json!({ let param = json!({
"backup-type": backup_type, "backup-type": backup_type,
@ -461,25 +461,22 @@ impl HttpClient {
} }
#[derive(Clone)]
pub struct BackupReader { pub struct BackupReader {
h2: H2Client, h2: H2Client,
canceller: Option<Canceller>, canceller: Canceller,
} }
impl Drop for BackupReader { impl Drop for BackupReader {
fn drop(&mut self) { fn drop(&mut self) {
if let Some(canceller) = self.canceller.take() { self.canceller.cancel();
canceller.cancel();
}
} }
} }
impl BackupReader { impl BackupReader {
pub fn new(h2: H2Client, canceller: Canceller) -> Self { pub fn new(h2: H2Client, canceller: Canceller) -> Arc<Self> {
Self { h2, canceller: Some(canceller) } Arc::new(Self { h2, canceller: canceller })
} }
pub fn get(&self, path: &str, param: Option<Value>) -> impl Future<Item=Value, Error=Error> { pub fn get(&self, path: &str, param: Option<Value>) -> impl Future<Item=Value, Error=Error> {
@ -522,30 +519,26 @@ impl BackupReader {
} }
pub fn force_close(mut self) { pub fn force_close(mut self) {
if let Some(canceller) = self.canceller.take() { self.canceller.cancel();
canceller.cancel();
}
} }
} }
pub struct BackupClient { pub struct BackupClient {
h2: H2Client, h2: H2Client,
canceller: Option<Canceller>, canceller: Canceller,
} }
impl Drop for BackupClient { impl Drop for BackupClient {
fn drop(&mut self) { fn drop(&mut self) {
if let Some(canceller) = self.canceller.take() { self.canceller.cancel();
canceller.cancel();
}
} }
} }
impl BackupClient { impl BackupClient {
pub fn new(h2: H2Client, canceller: Canceller) -> Self { pub fn new(h2: H2Client, canceller: Canceller) -> Arc<Self> {
Self { h2, canceller: Some(canceller) } Arc::new(Self { h2, canceller })
} }
pub fn get(&self, path: &str, param: Option<Value>) -> impl Future<Item=Value, Error=Error> { pub fn get(&self, path: &str, param: Option<Value>) -> impl Future<Item=Value, Error=Error> {
@ -560,18 +553,16 @@ impl BackupClient {
self.h2.post(path, param) self.h2.post(path, param)
} }
pub fn finish(mut self) -> impl Future<Item=(), Error=Error> { pub fn finish(self: Arc<Self>) -> impl Future<Item=(), Error=Error> {
let canceler = self.canceller.take().unwrap(); let canceller = self.canceller.clone();
self.h2.clone().post("finish", None).map(move |_| { self.h2.clone().post("finish", None).map(move |_| {
canceler.cancel(); canceller.cancel();
() ()
}) })
} }
pub fn force_close(mut self) { pub fn force_close(self) {
if let Some(canceller) = self.canceller.take() { self.canceller.cancel();
canceller.cancel();
}
} }
pub fn upload_blob_from_data( pub fn upload_blob_from_data(