Create http client laizily

This commit is contained in:
Nikolay Martynov 2020-02-20 10:11:11 -05:00
parent a4071ca768
commit 2c14aecd5a
1 changed files with 6 additions and 7 deletions

View File

@ -63,15 +63,13 @@ public class DDAgentApi {
private final String host; private final String host;
private final int port; private final int port;
private final String unixDomainSocketPath; private final String unixDomainSocketPath;
private final OkHttpClient httpClient; private OkHttpClient httpClient;
private HttpUrl tracesUrl; private HttpUrl tracesUrl;
public DDAgentApi(final String host, final int port, final String unixDomainSocketPath) { public DDAgentApi(final String host, final int port, final String unixDomainSocketPath) {
this.host = host; this.host = host;
this.port = port; this.port = port;
this.unixDomainSocketPath = unixDomainSocketPath; this.unixDomainSocketPath = unixDomainSocketPath;
httpClient = buildHttpClient(unixDomainSocketPath);
} }
public void addResponseListener(final DDAgentResponseListener listener) { public void addResponseListener(final DDAgentResponseListener listener) {
@ -116,8 +114,8 @@ public class DDAgentApi {
Response sendSerializedTraces( Response sendSerializedTraces(
final int representativeCount, final Integer sizeInBytes, final List<byte[]> traces) { final int representativeCount, final Integer sizeInBytes, final List<byte[]> traces) {
if (tracesUrl == null) { if (httpClient == null) {
detectEndpoint(); detectEndpointAndBuildClient();
} }
try { try {
@ -290,8 +288,8 @@ public class DDAgentApi {
} }
} }
private synchronized void detectEndpoint() { private synchronized void detectEndpointAndBuildClient() {
if (tracesUrl == null) { if (httpClient == null) {
final HttpUrl v4Url = getUrl(host, port, TRACES_ENDPOINT_V4); final HttpUrl v4Url = getUrl(host, port, TRACES_ENDPOINT_V4);
if (endpointAvailable(v4Url, unixDomainSocketPath, true)) { if (endpointAvailable(v4Url, unixDomainSocketPath, true)) {
tracesUrl = v4Url; tracesUrl = v4Url;
@ -299,6 +297,7 @@ public class DDAgentApi {
log.debug("API v0.4 endpoints not available. Downgrading to v0.3"); log.debug("API v0.4 endpoints not available. Downgrading to v0.3");
tracesUrl = getUrl(host, port, TRACES_ENDPOINT_V3); tracesUrl = getUrl(host, port, TRACES_ENDPOINT_V3);
} }
httpClient = buildHttpClient(unixDomainSocketPath);
} }
} }