Browse Source

Add a cache for client objects

Rather than creating them in the loop for each, make one client
object per api url.
changes/91/636791/1
Monty Taylor 3 months ago
parent
commit
84a247d43e
1 changed files with 22 additions and 2 deletions
  1. 22
    2
      zuul-preview/main.cc

+ 22
- 2
zuul-preview/main.cc View File

@@ -93,11 +93,28 @@ public:
93 93
   }
94 94
 };
95 95
 
96
+class ClientCache {
97
+  unordered_map<string, web::http::client::http_client> clients;
98
+public:
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
 
97 113
 int main(int, char**)
98 114
 {
99 115
   string input;
100 116
   Cache cache{1024};
117
+  ClientCache clients;
101 118
 
102 119
   // For each request apache receieves, it sends us the HTTP host name
103 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 154
     try {
138 155
       // Use the Zuul API to look up the artifact URL.
139
-      web::http::client::http_client client(api_url);
140
-      auto uri = web::uri_builder("/api/tenant/" + tenant + "/build");
156
+
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 161
       uri.append_path(buildid);
142 162
       auto response = client.request(
143 163
         web::http::methods::GET, uri.to_string()).get();

Loading…
Cancel
Save