Cache the client connector too
Otherwise, we negotiate for a fresh token on each request
This commit is contained in:
parent
c0f724f200
commit
6cce1a6e62
|
@ -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) {
|
||||||
|
let new_client = Github::new(
|
||||||
useragent,
|
useragent,
|
||||||
Client::with_connector(HttpsConnector::new(NativeTlsClient::new().unwrap())),
|
Client::with_connector(HttpsConnector::new(NativeTlsClient::new().unwrap())),
|
||||||
Credentials::InstallationToken(InstallationTokenGenerator::new(
|
Credentials::InstallationToken(InstallationTokenGenerator::new(
|
||||||
install_id,
|
install_id,
|
||||||
jwt
|
jwt
|
||||||
)),
|
)),
|
||||||
))
|
);
|
||||||
|
self.client_cache.insert(install_id, new_client);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(self.client_cache.get(&install_id).unwrap())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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};
|
||||||
|
|
Loading…
Reference in a new issue