Skip to content

Analytics request loader and game version validation#6064

Open
aecsocket wants to merge 3 commits intomainfrom
boris/analytics-checks
Open

Analytics request loader and game version validation#6064
aecsocket wants to merge 3 commits intomainfrom
boris/analytics-checks

Conversation

@aecsocket
Copy link
Copy Markdown
Contributor

No description provided.

@modrinth-bot
Copy link
Copy Markdown
Member

Pull request changelog

App

Added

Changed

Deprecated

Removed

Fixed

Security

Website

Added

Changed

Deprecated

Removed

Fixed

Security

Hosting

Added

Changed

Deprecated

Removed

Fixed

Security

Comment on lines +26 to +64
pub async fn valid_download_tags(
pool: &PgPool,
redis: &RedisPool,
) -> Result<Arc<DownloadTagsCache>, ApiError> {
const DOWNLOAD_TAGS_CACHE_TTL: Duration = Duration::from_secs(60 * 5);

static DOWNLOAD_TAGS_CACHE: ArcSwapOption<DownloadTagsCache> =
ArcSwapOption::const_empty();

let now = Instant::now();
let cached = DOWNLOAD_TAGS_CACHE.load();
if let Some(cached) = &*cached
&& cached.expires > now
{
return Ok(cached.clone());
}

let loaders = Loader::list(pool, redis)
.await
.wrap_internal_err("failed to fetch loaders")?
.into_iter()
.map(|loader| loader.loader)
.collect();
let game_versions = MinecraftGameVersion::list(None, None, pool, redis)
.await
.wrap_internal_err("failed to fetch game versions")?
.into_iter()
.map(|game_version| game_version.version)
.collect();

let cache = Arc::new(DownloadTagsCache {
expires: now + DOWNLOAD_TAGS_CACHE_TTL,
loaders,
game_versions,
});
DOWNLOAD_TAGS_CACHE.store(Some(cache.clone()));

Ok(cache)
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would update the cache asynchronously or at least use cache locking through a read/write lock to avoid cache stampede

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants