Skip to content

Commit

Permalink
feat : adds pooling Connection Pool
Browse files Browse the repository at this point in the history
  • Loading branch information
rajadilipkolli committed Jul 20, 2024
1 parent fad0688 commit 7d3a7b9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/***
<p>
Licensed under MIT License Copyright (c) 2022-2023 Raja Kolli.
Licensed under MIT License Copyright (c) 2022-2024 Raja Kolli.
</p>
***/

Expand All @@ -11,9 +11,11 @@ Licensed under MIT License Copyright (c) 2022-2023 Raja Kolli.

import io.netty.handler.timeout.ReadTimeoutHandler;
import io.netty.handler.timeout.WriteTimeoutHandler;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.reactive.function.client.WebClientCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
Expand All @@ -23,6 +25,8 @@ Licensed under MIT License Copyright (c) 2022-2023 Raja Kolli.
import org.springframework.web.reactive.function.client.ExchangeFilterFunction;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.netty.http.client.HttpClient;
import reactor.netty.resources.ConnectionProvider;
import reactor.util.retry.Retry;

@Configuration(proxyBeanMethods = false)
public class WebClientConfiguration {
Expand All @@ -37,14 +41,30 @@ public WebClientConfiguration(ApplicationProperties applicationProperties) {

@Bean
WebClient webClient(WebClient.Builder webClientBuilder) {
return webClientBuilder
.defaultHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.filter(logRequestDetails())
.filter(logResponseDetails())
.baseUrl(applicationProperties.getInventoryServiceUrl())
.clientConnector(new ReactorClientHttpConnector(clientConnectorConfig()))
.build();
return webClientBuilder.baseUrl(applicationProperties.getInventoryServiceUrl()).build();
}

@Bean
WebClientCustomizer webClientCustomizer() {
return webClientBuilder ->
webClientBuilder
.defaultHeaders(
httpHeaders -> {
httpHeaders.add(
HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE);
httpHeaders.add(
HttpHeaders.CONTENT_TYPE,
MediaType.APPLICATION_JSON_VALUE);
httpHeaders.add(HttpHeaders.ACCEPT_ENCODING, "gzip");
})
.filter(logRequestDetails())
.filter(logResponseDetails())
.filter(
(request, next) ->
next.exchange(request)
.retryWhen(
Retry.backoff(3, Duration.ofMillis(100))))
.clientConnector(new ReactorClientHttpConnector(clientConnectorConfig()));
}

private ExchangeFilterFunction logRequestDetails() {
Expand Down Expand Up @@ -85,8 +105,17 @@ private ExchangeFilterFunction logResponseDetails() {
}

private HttpClient clientConnectorConfig() {
return HttpClient.create()
ConnectionProvider connectionProvider =
ConnectionProvider.builder("custom")
.maxConnections(10)
.pendingAcquireMaxCount(50)
.maxIdleTime(Duration.ofSeconds(20))
.maxLifeTime(Duration.ofSeconds(60))
.build();

return HttpClient.create(connectionProvider)
.option(CONNECT_TIMEOUT_MILLIS, 10_000)
.responseTimeout(Duration.ofSeconds(10))
.doOnConnected(
conn -> {
conn.addHandlerLast(new ReadTimeoutHandler(15, TimeUnit.SECONDS));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ PostgreSQLContainer<?> postgreSqlContainer() {
@ServiceConnection
KafkaContainer kafkaContainer(DynamicPropertyRegistry dynamicPropertyRegistry) {
KafkaContainer kafkaContainer =
new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka").withTag("7.6.0"))
new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka").withTag("7.6.2"))
.withKraft()
.withReuse(true);
dynamicPropertyRegistry.add(
Expand Down

0 comments on commit 7d3a7b9

Please sign in to comment.