Skip to content

Commit

Permalink
add TTL
Browse files Browse the repository at this point in the history
  • Loading branch information
EddeCCC committed Dec 11, 2024
1 parent 83b96c9 commit 64892f5
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import lombok.NoArgsConstructor;
import rocks.inspectit.ocelot.config.model.config.RetrySettings;

import javax.validation.Valid;
import javax.validation.constraints.AssertFalse;
import java.net.URL;
import java.time.Duration;

Expand Down Expand Up @@ -42,6 +44,11 @@ public class AgentCommandSettings {
*/
private Duration socketTimeout;

/**
* The TTL - the time to keep an HTTP connection alive
*/
private Duration timeToLive;

/**
* The used interval for polling commands.
*/
Expand All @@ -60,5 +67,14 @@ public class AgentCommandSettings {
/**
* Settings how retries are handled regarding fetching an agent command.
*/
@Valid
private RetrySettings retry;

@AssertFalse(message = "The specified time values should not be negative!")
public boolean isNegativeTimeout() {
boolean negativeLiveReadTimeout = liveSocketTimeout != null && liveSocketTimeout.isNegative();
boolean negativeReadTimeout = socketTimeout != null && socketTimeout.isNegative();
boolean negativeTTL = timeToLive != null && timeToLive.isNegative();
return negativeLiveReadTimeout || negativeReadTimeout || negativeTTL;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
public class HttpConfigSettings {

/**
* Whether a HTTP property source should be used.
* Whether an HTTP property source should be used.
*/
private boolean enabled;

Expand Down Expand Up @@ -68,17 +68,23 @@ public class HttpConfigSettings {
*/
private Duration socketTimeout;

/**
* The TTL - the time to keep an HTTP connection alive
*/
private Duration timeToLive;

/**
* Settings how retries are handled regarding fetching an HTTP property source.
*/
@Valid
private RetrySettings retry;

@AssertFalse(message = "The specified timeout values should not be negative!")
@AssertFalse(message = "The specified time values should not be negative!")
public boolean isNegativeTimeout() {
boolean negativeConnectionTimeout = connectionTimeout != null && connectionTimeout.isNegative();
boolean negativeConnectionRequestTimeout = connectionRequestTimeout != null && connectionRequestTimeout.isNegative();
boolean negativeReadTimeout = socketTimeout != null && socketTimeout.isNegative();
return negativeConnectionTimeout || negativeConnectionRequestTimeout || negativeReadTimeout;
boolean negativeTTL = timeToLive != null && timeToLive.isNegative();
return negativeConnectionTimeout || negativeConnectionRequestTimeout || negativeReadTimeout || negativeTTL;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ inspectit:
socket-timeout: 5s
# the used interval for polling commands
polling-interval: 15s
# the time to keep an HTTP connection alive
time-to-live: 4m
# the maximum time to run one agent command polling task
task-timeout: 32m
# how long the agent will stay in the live mode, before falling back to the normal mode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.lang.management.RuntimeMXBean;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.concurrent.TimeUnit;

@Slf4j
@Component
Expand Down Expand Up @@ -78,13 +79,20 @@ private void updateHttpClients() {
AgentCommandSettings settings = environment.getCurrentConfig().getAgentCommands();
int timeout = (int) settings.getSocketTimeout().toMillis();
int liveTimeout = (int) settings.getLiveSocketTimeout().toMillis();
int timeToLive = (int) settings.getTimeToLive().toMillis();

RequestConfig normalConfig = RequestConfig.custom().setSocketTimeout(timeout).build();

RequestConfig liveConfig = RequestConfig.custom().setSocketTimeout(liveTimeout).build();

normalHttpClient = HttpClientBuilder.create().setDefaultRequestConfig(normalConfig).build();
liveHttpClient = HttpClientBuilder.create().setDefaultRequestConfig(liveConfig).build();
normalHttpClient = HttpClientBuilder.create()
.setDefaultRequestConfig(normalConfig)
.setConnectionTimeToLive(timeToLive, TimeUnit.MILLISECONDS)
.build();
liveHttpClient = HttpClientBuilder.create()
.setDefaultRequestConfig(liveConfig)
.setConnectionTimeToLive(timeToLive, TimeUnit.MILLISECONDS)
.build();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import java.io.IOException;
import java.time.Duration;
import java.util.concurrent.TimeUnit;

/**
* Stores an instance of a {@link CloseableHttpClient} and it's relevant {@link HttpConfigSettings}.
Expand All @@ -24,6 +25,8 @@ public class HttpClientHolder {

private Duration socketTimeout;

private Duration timeToLive;

/**
* Returns a {@link HttpClient}, which is used for fetching the configuration.
* If the HTTP settings changed, a new client will be created and the old one will be closed.
Expand All @@ -32,39 +35,54 @@ public class HttpClientHolder {
*/
public CloseableHttpClient getHttpClient(HttpConfigSettings httpSettings) throws IOException {
if(isUpdated(httpSettings) || httpClient == null) {
RequestConfig.Builder configBuilder = RequestConfig.custom();
RequestConfig config = getRequestConfig(httpSettings);
HttpClientBuilder builder = HttpClientBuilder.create().setDefaultRequestConfig(config);

if (httpSettings.getConnectionTimeout() != null) {
int connectionTimeout = (int) httpSettings.getConnectionTimeout().toMillis();
configBuilder = configBuilder.setConnectTimeout(connectionTimeout);
}
if (httpSettings.getConnectionRequestTimeout() != null) {
int connectionRequestTimeout = (int) httpSettings.getConnectionRequestTimeout().toMillis();
configBuilder = configBuilder.setConnectionRequestTimeout(connectionRequestTimeout);
if (httpSettings.getTimeToLive() != null) {
int timeToLive = (int) httpSettings.getTimeToLive().toMillis();
builder.setConnectionTimeToLive(timeToLive, TimeUnit.MILLISECONDS);
}
if (httpSettings.getSocketTimeout() != null) {
int socketTimeout = (int) httpSettings.getSocketTimeout().toMillis();
configBuilder = configBuilder.setSocketTimeout(socketTimeout);
}

RequestConfig config = configBuilder.build();

if (httpClient != null) httpClient.close();
httpClient = HttpClientBuilder.create().setDefaultRequestConfig(config).build();

httpClient = builder.build();
connectionTimeout = httpSettings.getConnectionTimeout();
connectionRequestTimeout = httpSettings.getConnectionRequestTimeout();
socketTimeout = httpSettings.getSocketTimeout();
timeToLive = httpSettings.getTimeToLive();
}
return httpClient;
}

/**
* @param httpSettings the current HTTP settings
* @return the derived request configuration
*/
private static RequestConfig getRequestConfig(HttpConfigSettings httpSettings) {
RequestConfig.Builder configBuilder = RequestConfig.custom();

if (httpSettings.getConnectionTimeout() != null) {
int connectionTimeout = (int) httpSettings.getConnectionTimeout().toMillis();
configBuilder = configBuilder.setConnectTimeout(connectionTimeout);
}
if (httpSettings.getConnectionRequestTimeout() != null) {
int connectionRequestTimeout = (int) httpSettings.getConnectionRequestTimeout().toMillis();
configBuilder = configBuilder.setConnectionRequestTimeout(connectionRequestTimeout);
}
if (httpSettings.getSocketTimeout() != null) {
int socketTimeout = (int) httpSettings.getSocketTimeout().toMillis();
configBuilder = configBuilder.setSocketTimeout(socketTimeout);
}

return configBuilder.build();
}

/**
* @return true, if the provided settings differ from the active settings
*/
private boolean isUpdated(HttpConfigSettings settings) {
return settings.getConnectionTimeout() != connectionTimeout ||
settings.getConnectionRequestTimeout() != connectionRequestTimeout ||
settings.getSocketTimeout() != socketTimeout;
settings.getSocketTimeout() != socketTimeout ||
settings.getTimeToLive() != timeToLive;
}
}

0 comments on commit 64892f5

Please sign in to comment.