Cache the client connector too

Otherwise, we negotiate for a fresh token on each request
This commit is contained in:
Graham Christensen 2019-04-11 22:27:28 -04:00
parent c0f724f200
commit 6cce1a6e62
No known key found for this signature in database
GPG key ID: ACA1C1D120C83D5C
2 changed files with 21 additions and 22 deletions

View file

@ -8,7 +8,6 @@ use std::collections::HashMap;
use std::fs::File; use std::fs::File;
use std::io::Read; use std::io::Read;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::cell::RefCell;
use ofborg::acl; use ofborg::acl;
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
@ -119,8 +118,8 @@ impl Config {
{ {
GithubAppVendingMachine { GithubAppVendingMachine {
conf: self.github_app.clone().unwrap(), conf: self.github_app.clone().unwrap(),
id_cache: RefCell::new(HashMap::new()), id_cache: HashMap::new(),
client_cache: RefCell::new(HashMap::new()), client_cache: HashMap::new(),
} }
} }
@ -168,16 +167,12 @@ pub fn load(filename: &Path) -> Config {
pub struct GithubAppVendingMachine { pub struct GithubAppVendingMachine {
conf: GithubAppConfig, conf: GithubAppConfig,
id_cache: RefCell<HashMap<(String, String), i32>>, id_cache: HashMap<(String, String), i32>,
client_cache: RefCell<HashMap<i32, Github>> client_cache: HashMap<i32, Github>,
} }
impl GithubAppVendingMachine { impl GithubAppVendingMachine {
pub fn for_repo(&self, owner: &str, repo: &str) -> Result<Github, hubcaps::Error> { pub fn for_repo<'a>(&'a mut self, owner: &str, repo: &str) -> Result<&'a Github, hubcaps::Error> {
let mut id_cache = self.id_cache.borrow_mut();
// !!! Cache clients so we don't look up new tokens all the time
let mut _client_cache = self.client_cache.borrow_mut();
let useragent = "github.com/grahamc/ofborg (app)"; let useragent = "github.com/grahamc/ofborg (app)";
let jwt = JWTCredentials::new(self.conf.app_id, let jwt = JWTCredentials::new(self.conf.app_id,
self.conf.private_key.clone()); self.conf.private_key.clone());
@ -185,8 +180,8 @@ impl GithubAppVendingMachine {
let install_id: i32; let install_id: i32;
let key = (owner.to_owned(), repo.to_owned()); let key = (owner.to_owned(), repo.to_owned());
if id_cache.contains_key(&key) { if self.id_cache.contains_key(&key) {
install_id = *id_cache.get(&key).unwrap(); install_id = *self.id_cache.get(&key).unwrap();
debug!("Found install ID for {:?} in cache", key); debug!("Found install ID for {:?} in cache", key);
} else { } else {
info!("Looking up install ID for {}/{}", owner, repo); info!("Looking up install ID for {}/{}", owner, repo);
@ -200,17 +195,22 @@ impl GithubAppVendingMachine {
install_id = lookup_gh install_id = lookup_gh
.app() .app()
.find_repo_installation(owner, repo)?.id; .find_repo_installation(owner, repo)?.id;
id_cache.insert(key, install_id); self.id_cache.insert(key, install_id);
debug!("Received install ID {}", install_id); debug!("Received install ID {}", install_id);
} }
Ok(Github::new( if ! self.client_cache.contains_key(&install_id) {
useragent, let new_client = Github::new(
Client::with_connector(HttpsConnector::new(NativeTlsClient::new().unwrap())), useragent,
Credentials::InstallationToken(InstallationTokenGenerator::new( Client::with_connector(HttpsConnector::new(NativeTlsClient::new().unwrap())),
install_id, Credentials::InstallationToken(InstallationTokenGenerator::new(
jwt install_id,
)), jwt
)) )),
);
self.client_cache.insert(install_id, new_client);
}
Ok(self.client_cache.get(&install_id).unwrap())
} }
} }

View file

@ -5,7 +5,6 @@ use serde_json;
use amqp::protocol::basic::{BasicProperties, Deliver}; use amqp::protocol::basic::{BasicProperties, Deliver};
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use hubcaps;
use hubcaps::checks::{CheckRunOptions, CheckRunState, Conclusion, Output}; use hubcaps::checks::{CheckRunOptions, CheckRunState, Conclusion, Output};
use message::buildjob::{BuildJob, QueuedBuildJobs}; use message::buildjob::{BuildJob, QueuedBuildJobs};
use ofborg::message::buildresult::{BuildResult, BuildStatus, LegacyBuildResult}; use ofborg::message::buildresult::{BuildResult, BuildStatus, LegacyBuildResult};