Skip to content

Commit

Permalink
Allow usage of external HttpClient.
Browse files Browse the repository at this point in the history
  • Loading branch information
matejonnet committed Sep 15, 2020
1 parent 8bcd720 commit e706cce
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.io.Closeable;
import java.io.IOException;
import java.net.URI;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
Expand All @@ -19,7 +20,8 @@ public abstract class BuildAgentClientBase implements Closeable {

private final Logger log = Logger.getLogger(BuildAgentClientBase.class);

protected final HttpClient httpClient;
private final Optional<HttpClient> internalHttpClient;
private final Optional<HttpClient> httpClient;
protected final URI livenessProbeLocation;
private final long livenessResponseTimeout;

Expand All @@ -28,27 +30,55 @@ public BuildAgentClientBase(String termBaseUrl, long livenessResponseTimeout) th
termBaseUrl = StringUtils.stripEndingSlash(termBaseUrl);
this.livenessProbeLocation = URI.create(termBaseUrl + "/servlet/is-alive");
try {
httpClient = new HttpClient();
internalHttpClient = Optional.of(new HttpClient());
httpClient = Optional.empty();
} catch (IOException e) {
throw new BuildAgentClientException("Cannot initialize http client.", e);
}
}

/**
* It is preferable to use a single instance of a HttpClient for all the BuildAgentClients because of the HttpClient's
* internal thread pool.
*
* @param httpClient
* @param termBaseUrl
* @param livenessResponseTimeout
*/
public BuildAgentClientBase(HttpClient httpClient, String termBaseUrl, long livenessResponseTimeout) {
this.livenessResponseTimeout = livenessResponseTimeout;
termBaseUrl = StringUtils.stripEndingSlash(termBaseUrl);
this.livenessProbeLocation = URI.create(termBaseUrl + "/servlet/is-alive");
this.internalHttpClient = Optional.empty();
this.httpClient = Optional.of(httpClient);
}

public boolean isServerAlive() {
CompletableFuture<HttpClient.Response> responseFuture = new CompletableFuture<>();
httpClient.invoke(livenessProbeLocation, "HEAD", "", responseFuture);
getHttpClient().invoke(livenessProbeLocation, "HEAD", "", responseFuture);
try {
HttpClient.Response response = responseFuture.get(livenessResponseTimeout, TimeUnit.MILLISECONDS);
return response.getCode() == 200;
boolean isSuccess = response.getCode() == 200;
return isSuccess;
} catch (InterruptedException | TimeoutException | ExecutionException e) {
log.warn("Did not receive liveness probe response.", e);
responseFuture.cancel(true);
return false;
}
}

protected HttpClient getHttpClient() {
if (internalHttpClient.isPresent()) {
return internalHttpClient.get();
} else {
return httpClient.get();
}
}

@Override
public void close() throws IOException {
httpClient.close();
if (internalHttpClient.isPresent()) {
internalHttpClient.get().close();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,22 @@ public BuildAgentHttpClient(HttpClientConfiguration configuration)
}
}

/**
* It is preferable to use a single instance of a HttpClient for all the BuildAgentClients because of the HttpClient's
* internal thread pool.
*/
public BuildAgentHttpClient(HttpClient httpClient, HttpClientConfiguration configuration)
throws BuildAgentClientException {
super(httpClient, configuration.getTermBaseUrl(), configuration.getLivenessResponseTimeout());
this.callbackUrl = configuration.getCallbackUrl();
this.callbackMethod = configuration.getCallbackMethod();
try {
invokerUrl = new URL(configuration.getTermBaseUrl() + Constants.HTTP_INVOKER_FULL_PATH);
} catch (MalformedURLException e) {
throw new BuildAgentClientException("Invalid term url.", e);
}
}

@Override
public void execute(Object command) throws BuildAgentClientException {
String cmd;
Expand All @@ -75,7 +91,7 @@ public void execute(Object command) throws BuildAgentClientException {
CompletableFuture<HttpClient.Response> responseFuture = new CompletableFuture<>();
try {
String requestJson = objectMapper.writeValueAsString(request);
httpClient.invoke(invokerUrl.toURI(), "POST", requestJson, responseFuture);
getHttpClient().invoke(invokerUrl.toURI(), "POST", requestJson, responseFuture);
} catch (JsonProcessingException e) {
throw new BuildAgentClientException("Cannot serialize request.", e);
} catch (URISyntaxException e) {
Expand Down Expand Up @@ -107,7 +123,7 @@ public void cancel() throws BuildAgentClientException {
CompletableFuture<HttpClient.Response> responseFuture = new CompletableFuture<>();
try {
String requestJson = objectMapper.writeValueAsString(request);
httpClient.invoke(invokerUrl.toURI(), "PUT", requestJson, responseFuture);
getHttpClient().invoke(invokerUrl.toURI(), "PUT", requestJson, responseFuture);
} catch (JsonProcessingException e) {
throw new BuildAgentClientException("Cannot serialize cancel request.", e);
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import org.jboss.pnc.buildagent.api.ResponseMode;
import org.jboss.pnc.buildagent.api.TaskStatusUpdateEvent;
import org.jboss.pnc.buildagent.common.http.HttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -117,6 +118,30 @@ public BuildAgentSocketClient(
commandExecutingEndpoint = connectCommandExecutingClient(termBaseUrl, responseDataConsumer);
}

/**
* It is preferable to use a single instance of a HttpClient for all the BuildAgentClients because of the HttpClient's
* internal thread pool.
*/
public BuildAgentSocketClient(
HttpClient httpClient,
Optional<Consumer<String>> responseDataConsumer,
Consumer<TaskStatusUpdateEvent> onStatusUpdate,
SocketClientConfiguration configuration)
throws TimeoutException, InterruptedException, BuildAgentClientException {
super(httpClient, configuration.getTermBaseUrl(), configuration.getLivenessResponseTimeout());
this.commandContext = formatCommandContext(configuration.getCommandContext());
this.responseMode = configuration.getResponseMode();
this.readOnly = configuration.isReadOnly();

Consumer<TaskStatusUpdateEvent> onStatusUpdateInternal = (event) -> {
onStatusUpdate.accept(event);
};

String termBaseUrl = configuration.getTermBaseUrl();
statusUpdatesEndpoint = connectStatusListenerClient(termBaseUrl, onStatusUpdateInternal);
commandExecutingEndpoint = connectCommandExecutingClient(termBaseUrl, responseDataConsumer);
}

@Deprecated
public void executeCommand(String command) throws BuildAgentClientException {
execute(command);
Expand Down

0 comments on commit e706cce

Please sign in to comment.