Skip to content

Commit

Permalink
Switching to ThreadLocal HttpClient for #57.
Browse files Browse the repository at this point in the history
  • Loading branch information
anjackson committed Nov 13, 2020
1 parent 78d77b6 commit cbfe975
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 20 deletions.
22 changes: 9 additions & 13 deletions src/main/java/uk/bl/wap/util/OutbackCDXClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.net.URISyntaxException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -56,9 +57,7 @@ public class OutbackCDXClient {

private PoolingHttpClientConnectionManager pcm = null;

private ThreadLocal<BasicHttpClientConnectionManager> cm = new ThreadLocal<BasicHttpClientConnectionManager>();

private CloseableHttpClient client = null;
private ThreadLocal<CloseableHttpClient> client = new ThreadLocal<CloseableHttpClient>();

private String endpoint = "http://localhost:9090/fc";// ?url=";//
// "http://crawl-index/timeline?url=";
Expand Down Expand Up @@ -191,34 +190,31 @@ private HttpClientConnectionManager getConnectionManager() {
return pcm;

} else {
if (cm.get() == null) {
cm.set(new BasicHttpClientConnectionManager());
}
return cm.get();
return new BasicHttpClientConnectionManager();
}
}

/*
* Return a separate client for every connection:
*/
private CloseableHttpClient getHttpClient() {
if (this.client == null) {
if (this.client.get() == null) {
// Allow client to look up system properties for proxy settings etc.
// Defaults to false as this can lead to thread contention.
if (useSystemProperties) {
client = HttpClients.custom()
client.set(HttpClients.custom()
.setConnectionManager(getConnectionManager())
// .disableConnectionState()
.disableCookieManagement().useSystemProperties()
.build();
.build());
} else {
client = HttpClients.custom()
client.set(HttpClients.custom()
.setConnectionManager(getConnectionManager())
// .disableConnectionState()
.disableCookieManagement().build();
.disableCookieManagement().build());
}
}
return client;
return client.get();
}

private long queryRangeSecs = 6L * 30 * 24 * 3600;
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/uk/bl/wap/util/OutbackCDXClientLoadTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ public class OutbackCDXClientLoadTest {
// 500 3103 6.206 0 50000
// 1000 7581 7.581 0 100000
//
// Using ThreadLocal BasicHttpClientConnectionManager:
// Using ThreadLocal Client w/ BasicHttpClientConnectionManager:
//
// 10 792 79.2 0 1000
// 50 549 10.98 0 5000
// 100 1182 11.82 0 10000
// 250 2244 8.976 0 25000
// 500 3611 7.222 0 50000
// 1000 7862 7.862 0 100000
// 10 1232 123.2 0 1000
// 50 1086 21.72 0 5000
// 100 1713 17.13 0 10000
// 250 2391 9.564 0 25000
// 500 5496 10.992 0 50000
// 1000 8508 8.508 0 100000

/**
* @param args
Expand Down

0 comments on commit cbfe975

Please sign in to comment.