Skip to content

GH caching issue #2088

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use crate::changelogs::ChangelogFormat;
use crate::github::{GithubClient, Repository};
use octocrab::Octocrab;
use std::collections::{HashMap, HashSet};
use std::fmt;
use std::sync::{Arc, LazyLock, RwLock};
use std::time::{Duration, Instant};
use tracing as log;

pub(crate) static CONFIG_FILE_NAME: &str = "triagebot.toml";
const REFRESH_EVERY: Duration = Duration::from_secs(2 * 60); // Every two minutes
const REFRESH_EVERY_SECS: Duration = Duration::from_secs(2 * 60); // Every two minutes

static CONFIG_CACHE: LazyLock<
RwLock<HashMap<String, (Result<Arc<Config>, ConfigurationError>, Instant)>>,
Expand Down Expand Up @@ -432,15 +433,15 @@ pub(crate) struct ReviewRequestedConfig {
}

pub(crate) async fn get(
gh: &GithubClient,
octo_ctx: &Octocrab,
repo: &Repository,
) -> Result<Arc<Config>, ConfigurationError> {
if let Some(config) = get_cached_config(&repo.full_name) {
log::trace!("returning config for {} from cache", repo.full_name);
config
} else {
log::trace!("fetching fresh config for {}", repo.full_name);
let res = get_fresh_config(gh, repo).await;
let res = get_fresh_config2(&octo_ctx, repo).await;
CONFIG_CACHE
.write()
.unwrap()
Expand Down Expand Up @@ -525,14 +526,40 @@ fn default_true() -> bool {
fn get_cached_config(repo: &str) -> Option<Result<Arc<Config>, ConfigurationError>> {
let cache = CONFIG_CACHE.read().unwrap();
cache.get(repo).and_then(|(config, fetch_time)| {
if fetch_time.elapsed() < REFRESH_EVERY {
if fetch_time.elapsed() < REFRESH_EVERY_SECS {
Some(config.clone())
} else {
None
}
})
}

async fn get_fresh_config2(
octo_ctx: &Octocrab,
repo: &Repository,
) -> Result<Arc<Config>, ConfigurationError> {
let mut content_items = octo_ctx
.repos(repo.owner(), repo.name())
.get_content()
.path(CONFIG_FILE_NAME)
.r#ref(&repo.default_branch)
.send()
.await
.map_err(|e| ConfigurationError::Http(Arc::new(e.into())))?;

let contents = content_items.take_items();
let c = &contents[0];

let contents = c
.decoded_content()
.ok_or(ConfigurationError::Missing)
.map_err(|e| e)?;

let config = Arc::new(toml::from_str::<Config>(&contents).map_err(ConfigurationError::Toml)?);
log::debug!("fresh configuration for {}: {:?}", repo.full_name, config);
Ok(config)
}

async fn get_fresh_config(
gh: &GithubClient,
repo: &Repository,
Expand Down
2 changes: 1 addition & 1 deletion src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ mod transfer;
pub mod types_planning_updates;

pub async fn handle(ctx: &Context, event: &Event) -> Vec<HandlerError> {
let config = config::get(&ctx.github, event.repo()).await;
let config = config::get(&ctx.octocrab, event.repo()).await;
if let Err(e) = &config {
log::warn!("configuration error {}: {e}", event.repo().full_name);
}
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/major_change.rs
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ async fn process_seconded(
.await
.context("failed retrieving the repository informations")?;

let config = crate::config::get(&ctx.github, &repo)
let config = crate::config::get(&ctx.octocrab, &repo)
.await
.context("failed to get triagebot configuration")?;

Expand Down