summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMonty Taylor <mordred@inaugust.com>2019-02-11 21:16:13 +0000
committerMonty Taylor <mordred@inaugust.com>2019-02-11 21:16:13 +0000
commit84a247d43eaab788fe25903c7fd32bd1b37c47ec (patch)
tree8367b4ba39cb89945653648a5321176d66d0b6a7
parenta4c1ce615a280d4cf7fdfba52054cf09e2f39a37 (diff)
Add a cache for client objects
Rather than creating them in the loop for each, make one client object per api url.
-rw-r--r--zuul-preview/main.cc24
1 files changed, 22 insertions, 2 deletions
diff --git a/zuul-preview/main.cc b/zuul-preview/main.cc
index 7b57f08..8493be1 100644
--- a/zuul-preview/main.cc
+++ b/zuul-preview/main.cc
@@ -93,11 +93,28 @@ public:
93 } 93 }
94}; 94};
95 95
96class ClientCache {
97 unordered_map<string, web::http::client::http_client> clients;
98public:
99 ClientCache() : clients{} { }
100
101 web::http::client::http_client get(const string &key)
102 {
103 auto location = clients.find(key);
104 if (location == clients.end()) {
105 auto value = web::http::client::http_client(key);
106 clients.insert(make_pair(key, value));
107 return value;
108 }
109 return location->second;
110 }
111};
96 112
97int main(int, char**) 113int main(int, char**)
98{ 114{
99 string input; 115 string input;
100 Cache cache{1024}; 116 Cache cache{1024};
117 ClientCache clients;
101 118
102 // For each request apache receieves, it sends us the HTTP host name 119 // For each request apache receieves, it sends us the HTTP host name
103 // on standard input. We use that to look up the build URL and emit 120 // on standard input. We use that to look up the build URL and emit
@@ -136,8 +153,11 @@ int main(int, char**)
136 153
137 try { 154 try {
138 // Use the Zuul API to look up the artifact URL. 155 // Use the Zuul API to look up the artifact URL.
139 web::http::client::http_client client(api_url); 156
140 auto uri = web::uri_builder("/api/tenant/" + tenant + "/build"); 157 auto client = clients.get(api_url);
158 auto uri = web::uri_builder("api/tenant");
159 uri.append_path(tenant);
160 uri.append_path("build");
141 uri.append_path(buildid); 161 uri.append_path(buildid);
142 auto response = client.request( 162 auto response = client.request(
143 web::http::methods::GET, uri.to_string()).get(); 163 web::http::methods::GET, uri.to_string()).get();