mirror of
https://github.com/ViViDboarder/bitwarden_rs.git
synced 2024-11-22 21:26:38 +00:00
Implement better retry and use it while saving device
This commit is contained in:
parent
f713e2e092
commit
4e827e4f8a
@ -64,33 +64,15 @@ impl Attachment {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn delete(self, conn: &DbConn) -> QueryResult<()> {
|
pub fn delete(self, conn: &DbConn) -> QueryResult<()> {
|
||||||
use crate::util;
|
crate::util::retry(
|
||||||
use std::{thread, time};
|
|| {
|
||||||
|
diesel::delete(attachments::table.filter(attachments::id.eq(&self.id)))
|
||||||
|
.execute(&**conn)
|
||||||
|
},
|
||||||
|
10,
|
||||||
|
)?;
|
||||||
|
|
||||||
let mut retries = 10;
|
crate::util::delete_file(&self.get_file_path());
|
||||||
|
|
||||||
loop {
|
|
||||||
match diesel::delete(
|
|
||||||
attachments::table.filter(
|
|
||||||
attachments::id.eq(&self.id)
|
|
||||||
)
|
|
||||||
).execute(&**conn) {
|
|
||||||
Ok(_) => break,
|
|
||||||
Err(err) => {
|
|
||||||
if retries < 1 {
|
|
||||||
error!("Failed with 10 retries");
|
|
||||||
return Err(err)
|
|
||||||
} else {
|
|
||||||
retries -= 1;
|
|
||||||
info!("Had to retry! Retries left: {}", retries);
|
|
||||||
thread::sleep(time::Duration::from_millis(500));
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
util::delete_file(&self.get_file_path());
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -101,7 +101,6 @@ impl Device {
|
|||||||
amr: vec!["Application".into()],
|
amr: vec!["Application".into()],
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
(encode_jwt(&claims), DEFAULT_VALIDITY.num_seconds())
|
(encode_jwt(&claims), DEFAULT_VALIDITY.num_seconds())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -116,8 +115,15 @@ impl Device {
|
|||||||
pub fn save(&mut self, conn: &DbConn) -> QueryResult<()> {
|
pub fn save(&mut self, conn: &DbConn) -> QueryResult<()> {
|
||||||
self.updated_at = Utc::now().naive_utc();
|
self.updated_at = Utc::now().naive_utc();
|
||||||
|
|
||||||
diesel::replace_into(devices::table)
|
crate::util::retry(
|
||||||
.values(&*self).execute(&**conn).and(Ok(()))
|
|| {
|
||||||
|
diesel::replace_into(devices::table)
|
||||||
|
.values(&*self)
|
||||||
|
.execute(&**conn)
|
||||||
|
},
|
||||||
|
10,
|
||||||
|
)
|
||||||
|
.and(Ok(()))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn delete(self, conn: &DbConn) -> QueryResult<()> {
|
pub fn delete(self, conn: &DbConn) -> QueryResult<()> {
|
||||||
|
29
src/util.rs
29
src/util.rs
@ -252,6 +252,33 @@ fn upcase_value(value: &Value) -> Value {
|
|||||||
fn _process_key(key: &str) -> String {
|
fn _process_key(key: &str) -> String {
|
||||||
match key.to_lowercase().as_ref() {
|
match key.to_lowercase().as_ref() {
|
||||||
"ssn" => "SSN".into(),
|
"ssn" => "SSN".into(),
|
||||||
_ => self::upcase_first(key)
|
_ => self::upcase_first(key),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Retry methods
|
||||||
|
//
|
||||||
|
|
||||||
|
pub fn retry<F, T, E>(func: F, max_tries: i32) -> Result<T, E>
|
||||||
|
where
|
||||||
|
F: Fn() -> Result<T, E>,
|
||||||
|
{
|
||||||
|
use std::{thread::sleep, time::Duration};
|
||||||
|
let mut tries = 0;
|
||||||
|
|
||||||
|
loop {
|
||||||
|
match func() {
|
||||||
|
ok @ Ok(_) => return ok,
|
||||||
|
err @ Err(_) => {
|
||||||
|
tries += 1;
|
||||||
|
|
||||||
|
if tries >= max_tries {
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
sleep(Duration::from_millis(500));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user