Skip to content

Commit

Permalink
Feat: max_retries as env config var
Browse files Browse the repository at this point in the history
  • Loading branch information
SHAcollision committed Oct 14, 2024
1 parent 2e2655d commit e56e313
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .env-sample
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ HOMESERVER_URL=http://localhost:6287
EVENTS_LIMIT=1000
# Sleep between checks to homeserver
WATCHER_SLEEP=5000
# Max amount of event retries
MAX_RETRIES=1

# Directory where static files are stored
STATIC_PATH=./static
Expand Down
5 changes: 5 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub struct Config {
pub homeserver_url: String,
pub events_limit: u32,
pub watcher_sleep: u64,
pub max_retries: u64,
}

impl Config {
Expand Down Expand Up @@ -57,6 +58,10 @@ impl Config {
.unwrap_or("5000".to_string())
.parse()
.unwrap_or(5000),
max_retries: env::var("MAX_RETRIES")
.unwrap_or("1".to_string())
.parse()
.unwrap_or(1),
neo4j_username: env::var("NEO4J_DB_USERNAME").expect("NEO4J_DB_USERNAME not set"),
neo4j_password: env::var("NEO4J_PASSWORD").expect("NEO4J_PASSWORD not set"),
}
Expand Down
14 changes: 9 additions & 5 deletions src/events/processor.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::time::Duration;

use super::Event;
use crate::{
models::{homeserver::Homeserver, user::PubkyId},
Expand All @@ -8,13 +10,12 @@ use pkarr::mainline::dht::Testnet;
use pubky::PubkyClient;
use reqwest::Client;

const MAX_RETRIES: usize = 3;

pub struct EventProcessor {
pubky_client: PubkyClient,
http_client: Client,
homeserver: Homeserver,
limit: u32,
max_retries: u64,
}

impl EventProcessor {
Expand All @@ -24,6 +25,7 @@ impl EventProcessor {
let pubky_client = Self::init_pubky_client(config);
let homeserver = Homeserver::from_config(config).await?;
let limit = config.events_limit;
let max_retries = config.max_retries;

info!(
"Initialized Event Processor for homeserver: {:?}",
Expand All @@ -35,6 +37,7 @@ impl EventProcessor {
http_client: Client::new(),
homeserver,
limit,
max_retries,
})
}

Expand All @@ -58,6 +61,7 @@ impl EventProcessor {
http_client: Client::new(),
homeserver,
limit: 100,
max_retries: 3,
}
}

Expand Down Expand Up @@ -124,7 +128,7 @@ impl EventProcessor {
Ok(_) => break Ok(()),
Err(e) => {
attempts += 1;
if attempts >= MAX_RETRIES {
if attempts >= self.max_retries {
error!(
"Error while handling event after {} attempts: {}",
attempts, e
Expand All @@ -133,10 +137,10 @@ impl EventProcessor {
} else {
error!(
"Error while handling event: {}. Retrying attempt {}/{}",
e, attempts, MAX_RETRIES
e, attempts, self.max_retries
);
// Optionally, add a delay between retries
// tokio::time::sleep(Duration::from_millis(100)).await;
tokio::time::sleep(Duration::from_millis(100)).await;
}
}
}
Expand Down

0 comments on commit e56e313

Please sign in to comment.