-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
only create new client when config changed
- Loading branch information
Showing
3 changed files
with
146 additions
and
32 deletions.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
...c/main/java/rocks/inspectit/ocelot/core/config/propertysources/http/HttpClientHolder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package rocks.inspectit.ocelot.core.config.propertysources.http; | ||
|
||
import org.apache.http.client.HttpClient; | ||
import org.apache.http.client.config.RequestConfig; | ||
import org.apache.http.impl.client.CloseableHttpClient; | ||
import org.apache.http.impl.client.HttpClientBuilder; | ||
import rocks.inspectit.ocelot.config.model.config.HttpConfigSettings; | ||
|
||
import java.io.IOException; | ||
import java.time.Duration; | ||
|
||
/** | ||
* Stores an instance of a {@link CloseableHttpClient} and it's relevant {@link HttpConfigSettings}. | ||
* Since the HTTP client is a rather expensive object, we create one instance and recreate it only | ||
* if the settings have changed. | ||
*/ | ||
public class HttpClientHolder { | ||
|
||
private CloseableHttpClient httpClient; | ||
|
||
private Duration connectionTimeout; | ||
|
||
private Duration connectionRequestTimeout; | ||
|
||
private Duration socketTimeout; | ||
|
||
/** | ||
* 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. | ||
* | ||
* @return a {@link HttpClient} instance. | ||
*/ | ||
public CloseableHttpClient getHttpClient(HttpConfigSettings httpSettings) throws IOException { | ||
if(isUpdated(httpSettings) || httpClient == null) { | ||
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); | ||
} | ||
|
||
RequestConfig config = configBuilder.build(); | ||
|
||
if (httpClient != null) httpClient.close(); | ||
httpClient = HttpClientBuilder.create().setDefaultRequestConfig(config).build(); | ||
|
||
connectionTimeout = httpSettings.getConnectionTimeout(); | ||
connectionRequestTimeout = httpSettings.getConnectionRequestTimeout(); | ||
socketTimeout = httpSettings.getSocketTimeout(); | ||
} | ||
return httpClient; | ||
} | ||
|
||
/** | ||
* @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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...st/java/rocks/inspectit/ocelot/core/config/propertysources/http/HttpClientHolderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package rocks.inspectit.ocelot.core.config.propertysources.http; | ||
|
||
import org.apache.http.impl.client.CloseableHttpClient; | ||
import org.junit.jupiter.api.Test; | ||
import rocks.inspectit.ocelot.config.model.config.HttpConfigSettings; | ||
|
||
import java.io.IOException; | ||
import java.time.Duration; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class HttpClientHolderTest { | ||
|
||
@Test | ||
void shouldNotCreateNewHttpClientWhenConfigIsSame() throws IOException { | ||
HttpClientHolder holder = new HttpClientHolder(); | ||
HttpConfigSettings settings = new HttpConfigSettings(); | ||
|
||
settings.setConnectionTimeout(Duration.ofSeconds(30)); | ||
CloseableHttpClient client = holder.getHttpClient(settings); | ||
CloseableHttpClient anotherClient = holder.getHttpClient(settings); | ||
|
||
assertThat(client).isSameAs(anotherClient); | ||
} | ||
|
||
@Test | ||
void shouldCreateNewHttpClientWhenConnectionTimeoutChanges() throws IOException { | ||
HttpClientHolder holder = new HttpClientHolder(); | ||
HttpConfigSettings settings = new HttpConfigSettings(); | ||
|
||
settings.setConnectionTimeout(Duration.ofSeconds(30)); | ||
CloseableHttpClient client = holder.getHttpClient(settings); | ||
|
||
settings.setConnectionTimeout(Duration.ofSeconds(10)); | ||
CloseableHttpClient updatedClient = holder.getHttpClient(settings); | ||
|
||
assertThat(updatedClient).isNotSameAs(client); | ||
} | ||
|
||
@Test | ||
void shouldCreateNewHttpClientWhenConnectionRequestTimeoutChanges() throws IOException { | ||
HttpClientHolder holder = new HttpClientHolder(); | ||
HttpConfigSettings settings = new HttpConfigSettings(); | ||
|
||
settings.setConnectionRequestTimeout(Duration.ofSeconds(30)); | ||
CloseableHttpClient client = holder.getHttpClient(settings); | ||
|
||
settings.setConnectionRequestTimeout(Duration.ofSeconds(10)); | ||
CloseableHttpClient updatedClient = holder.getHttpClient(settings); | ||
|
||
assertThat(updatedClient).isNotSameAs(client); | ||
} | ||
|
||
@Test | ||
void shouldCreateNewHttpClientWhenSocketTimeoutChanges() throws IOException { | ||
HttpClientHolder holder = new HttpClientHolder(); | ||
HttpConfigSettings settings = new HttpConfigSettings(); | ||
|
||
settings.setSocketTimeout(Duration.ofSeconds(30)); | ||
CloseableHttpClient client = holder.getHttpClient(settings); | ||
|
||
settings.setSocketTimeout(Duration.ofSeconds(10)); | ||
CloseableHttpClient updatedClient = holder.getHttpClient(settings); | ||
|
||
assertThat(updatedClient).isNotSameAs(client); | ||
} | ||
} |