1
0
Fork 0
mirror of https://github.com/dani-garcia/vaultwarden.git synced 2025-06-16 15:50:08 +00:00

Implement better retry and use it while saving device

This commit is contained in:
Daniel García 2018-12-12 22:15:54 +01:00
parent f713e2e092
commit 4e827e4f8a
No known key found for this signature in database
GPG key ID: FC8A7D14C3CD543A
3 changed files with 45 additions and 30 deletions

View file

@ -64,33 +64,15 @@ impl Attachment {
}
pub fn delete(self, conn: &DbConn) -> QueryResult<()> {
use crate::util;
use std::{thread, time};
crate::util::retry(
|| {
diesel::delete(attachments::table.filter(attachments::id.eq(&self.id)))
.execute(&**conn)
},
10,
)?;
let mut retries = 10;
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());
crate::util::delete_file(&self.get_file_path());
Ok(())
}

View file

@ -101,7 +101,6 @@ impl Device {
amr: vec!["Application".into()],
};
(encode_jwt(&claims), DEFAULT_VALIDITY.num_seconds())
}
}
@ -116,8 +115,15 @@ impl Device {
pub fn save(&mut self, conn: &DbConn) -> QueryResult<()> {
self.updated_at = Utc::now().naive_utc();
diesel::replace_into(devices::table)
.values(&*self).execute(&**conn).and(Ok(()))
crate::util::retry(
|| {
diesel::replace_into(devices::table)
.values(&*self)
.execute(&**conn)
},
10,
)
.and(Ok(()))
}
pub fn delete(self, conn: &DbConn) -> QueryResult<()> {