From 28f7a139a0565c0dc458c1c2250b5064ece89926 Mon Sep 17 00:00:00 2001 From: asonix Date: Wed, 4 Jan 2023 17:26:05 -0600 Subject: [PATCH] Retry in-process, and rate-limit self --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/lib.rs | 32 +++++++++++++++++++++++++++++++- 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0743910..b7fb773 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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", diff --git a/Cargo.toml b/Cargo.toml index ac38cd6..178a021 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 "] license = "AGPL-3.0" readme = "README.md" diff --git a/src/lib.rs b/src/lib.rs index 1f9f2ae..a7e8d6c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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(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(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) }