Retry in-process, and rate-limit self

This commit is contained in:
asonix 2023-01-04 17:26:05 -06:00
parent ad2c5e5027
commit 28f7a139a0
3 changed files with 33 additions and 3 deletions

2
Cargo.lock generated
View file

@ -1591,7 +1591,7 @@ dependencies = [
[[package]]
name = "pict-rs"
version = "0.4.0-beta.12"
version = "0.4.0-beta.13"
dependencies = [
"actix-form-data",
"actix-rt",

View file

@ -1,7 +1,7 @@
[package]
name = "pict-rs"
description = "A simple image hosting service"
version = "0.4.0-beta.12"
version = "0.4.0-beta.13"
authors = ["asonix <asonix@asonix.dog>"]
license = "AGPL-3.0"
readme = "README.md"

View file

@ -42,7 +42,7 @@ use std::{
path::Path,
path::PathBuf,
sync::atomic::{AtomicU64, Ordering},
time::{Duration, SystemTime},
time::{Duration, Instant, SystemTime},
};
use tokio::sync::Semaphore;
use tracing_actix_web::TracingLogger;
@ -1315,6 +1315,28 @@ const STORE_MIGRATION_MOTION: &str = "store-migration-motion";
const STORE_MIGRATION_VARIANT: &str = "store-migration-variant";
async fn migrate_store<R, S1, S2>(repo: &R, from: S1, to: S2) -> Result<(), Error>
where
S1: Store + Clone,
S2: Store + Clone,
R: IdentifierRepo + HashRepo + SettingsRepo,
{
let mut failure_count = 0;
while let Err(e) = do_migrate_store(repo, from.clone(), to.clone()).await {
tracing::error!("Failed with {}", e.to_string());
failure_count += 1;
tokio::time::sleep(Duration::from_secs(5)).await;
if failure_count >= 50 {
tracing::error!("Exceeded 50 errors");
return Err(e);
}
}
Ok(())
}
async fn do_migrate_store<R, S1, S2>(repo: &R, from: S1, to: S2) -> Result<(), Error>
where
S1: Store,
S2: Store,
@ -1395,10 +1417,18 @@ where
S1: Store,
S2: Store,
{
const CONST_TIME: Duration = Duration::from_millis(250);
let start = Instant::now();
let stream = from.to_stream(identifier, None, None).await?;
let new_identifier = to.save_stream(stream).await?;
let elapsed = start.elapsed();
if elapsed < CONST_TIME {
tokio::time::sleep(CONST_TIME - elapsed).await;
}
Ok(new_identifier)
}