Skip to content

Commit 1186ad0

Browse files
committed
Tentative fix for aggressive GH caching
1 parent 2407f13 commit 1186ad0

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

src/config.rs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
use crate::changelogs::ChangelogFormat;
22
use crate::github::{GithubClient, Repository};
3+
use octocrab::Octocrab;
34
use std::collections::{HashMap, HashSet};
45
use std::fmt;
56
use std::sync::{Arc, LazyLock, RwLock};
67
use std::time::{Duration, Instant};
78
use tracing as log;
89

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

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

434435
pub(crate) async fn get(
435-
gh: &GithubClient,
436+
octo_ctx: &Octocrab,
436437
repo: &Repository,
437438
) -> Result<Arc<Config>, ConfigurationError> {
438439
if let Some(config) = get_cached_config(&repo.full_name) {
439440
log::trace!("returning config for {} from cache", repo.full_name);
440441
config
441442
} else {
442443
log::trace!("fetching fresh config for {}", repo.full_name);
443-
let res = get_fresh_config(gh, repo).await;
444+
let res = get_fresh_config2(&octo_ctx, repo).await;
444445
CONFIG_CACHE
445446
.write()
446447
.unwrap()
@@ -525,14 +526,40 @@ fn default_true() -> bool {
525526
fn get_cached_config(repo: &str) -> Option<Result<Arc<Config>, ConfigurationError>> {
526527
let cache = CONFIG_CACHE.read().unwrap();
527528
cache.get(repo).and_then(|(config, fetch_time)| {
528-
if fetch_time.elapsed() < REFRESH_EVERY {
529+
if fetch_time.elapsed() < REFRESH_EVERY_SECS {
529530
Some(config.clone())
530531
} else {
531532
None
532533
}
533534
})
534535
}
535536

537+
async fn get_fresh_config2(
538+
octo_ctx: &Octocrab,
539+
repo: &Repository,
540+
) -> Result<Arc<Config>, ConfigurationError> {
541+
let mut content_items = octo_ctx
542+
.repos(repo.owner(), repo.name())
543+
.get_content()
544+
.path(CONFIG_FILE_NAME)
545+
.r#ref(&repo.default_branch)
546+
.send()
547+
.await
548+
.map_err(|e| ConfigurationError::Http(Arc::new(e.into())))?;
549+
550+
let contents = content_items.take_items();
551+
let c = &contents[0];
552+
553+
let contents = c
554+
.decoded_content()
555+
.ok_or(ConfigurationError::Missing)
556+
.map_err(|e| e)?;
557+
558+
let config = Arc::new(toml::from_str::<Config>(&contents).map_err(ConfigurationError::Toml)?);
559+
log::debug!("fresh configuration for {}: {:?}", repo.full_name, config);
560+
Ok(config)
561+
}
562+
536563
async fn get_fresh_config(
537564
gh: &GithubClient,
538565
repo: &Repository,

src/handlers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ mod transfer;
5959
pub mod types_planning_updates;
6060

6161
pub async fn handle(ctx: &Context, event: &Event) -> Vec<HandlerError> {
62-
let config = config::get(&ctx.github, event.repo()).await;
62+
let config = config::get(&ctx.octocrab, event.repo()).await;
6363
if let Err(e) = &config {
6464
log::warn!("configuration error {}: {e}", event.repo().full_name);
6565
}

src/handlers/major_change.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ async fn process_seconded(
554554
.await
555555
.context("failed retrieving the repository informations")?;
556556

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

0 commit comments

Comments
 (0)