Make max cache age adjustable in env, and change default to 1 hour

Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
This commit is contained in:
Daniel D’Aquino
2024-09-02 15:51:32 -07:00
parent 9ec713b807
commit 11568aa628
4 changed files with 17 additions and 4 deletions

View File

@ -40,6 +40,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
env.apns_team_id.clone(),
env.apns_environment.clone(),
env.apns_topic.clone(),
env.nostr_event_cache_max_age,
)
.await
.expect("Failed to create notification manager"),

View File

@ -6,6 +6,7 @@ const DEFAULT_DB_PATH: &str = "./apns_notifications.db";
const DEFAULT_HOST: &str = "0.0.0.0";
const DEFAULT_PORT: &str = "8000";
const DEFAULT_RELAY_URL: &str = "wss://relay.damus.io";
const DEFAULT_NOSTR_EVENT_CACHE_MAX_AGE: u64 = 60 * 60; // 1 hour
pub struct NotePushEnv {
// The path to the Apple private key .p8 file
@ -26,6 +27,8 @@ pub struct NotePushEnv {
pub api_base_url: String, // The base URL of where the API server is hosted for NIP-98 auth checks
// The URL of the Nostr relay server to connect to for getting mutelists
pub relay_url: String,
// The max age of the Nostr event cache, in seconds
pub nostr_event_cache_max_age: std::time::Duration,
}
impl NotePushEnv {
@ -47,6 +50,11 @@ impl NotePushEnv {
_ => a2::client::Endpoint::Sandbox,
};
let apns_topic = env::var("APNS_TOPIC")?;
let nostr_event_cache_max_age = env::var("NOSTR_EVENT_CACHE_MAX_AGE")
.unwrap_or(DEFAULT_NOSTR_EVENT_CACHE_MAX_AGE.to_string())
.parse::<u64>()
.map(|s| std::time::Duration::from_secs(s))
.unwrap_or(std::time::Duration::from_secs(DEFAULT_NOSTR_EVENT_CACHE_MAX_AGE));
Ok(NotePushEnv {
apns_private_key_path,
@ -59,6 +67,7 @@ impl NotePushEnv {
port,
api_base_url,
relay_url,
nostr_event_cache_max_age
})
}

View File

@ -6,7 +6,6 @@ use super::nostr_event_cache::Cache;
use tokio::time::{timeout, Duration};
const NOTE_FETCH_TIMEOUT: Duration = Duration::from_secs(5);
const CACHE_MAX_AGE: Duration = Duration::from_secs(60);
pub struct NostrNetworkHelper {
client: Client,
@ -16,12 +15,15 @@ pub struct NostrNetworkHelper {
impl NostrNetworkHelper {
// MARK: - Initialization
pub async fn new(relay_url: String) -> Result<Self, Box<dyn std::error::Error>> {
pub async fn new(relay_url: String, cache_max_age: Duration) -> Result<Self, Box<dyn std::error::Error>> {
let client = Client::new(&Keys::generate());
client.add_relay(relay_url.clone()).await?;
client.connect().await;
Ok(NostrNetworkHelper { client, cache: Mutex::new(Cache::new(CACHE_MAX_AGE)) })
Ok(NostrNetworkHelper {
client,
cache: Mutex::new(Cache::new(cache_max_age)),
})
}
// MARK: - Answering questions about a user

View File

@ -41,6 +41,7 @@ impl NotificationManager {
apns_team_id: String,
apns_environment: a2::client::Endpoint,
apns_topic: String,
cache_max_age: std::time::Duration,
) -> Result<Self, Box<dyn std::error::Error>> {
let connection = db.get()?;
Self::setup_database(&connection)?;
@ -58,7 +59,7 @@ impl NotificationManager {
apns_topic,
apns_client: Mutex::new(client),
db: Mutex::new(db),
nostr_network_helper: NostrNetworkHelper::new(relay_url.clone()).await?,
nostr_network_helper: NostrNetworkHelper::new(relay_url.clone(), cache_max_age).await?,
})
}