Skip to content

Commit 1710b01

Browse files
committed
tmp bžilion bžilionu
Signed-off-by: David Kral <[email protected]>
1 parent de665fa commit 1710b01

File tree

17 files changed

+628
-196
lines changed

17 files changed

+628
-196
lines changed

docs/src/main/asciidoc/se/webclient.adoc

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,55 @@ include::{sourcedir}/se/WebClientSnippets.java[tag=snippet_8,indent=0]
519519
<1> `application.yaml` is a default configuration source loaded when YAML support is on classpath, so we can just use `Config.create()`
520520
<2> Passing the client configuration node
521521
522+
=== Setting connection limits
523+
It is possible to limit connection numbers for specific hosts, proxies or even the total number of connections the client is allowed to create. None of these limits is mandatory to set.
524+
525+
In the examples below we are setting fixed limit implementations, but it is possible to use any implementation of the interface `io.helidon.common.concurrency.limits.Limit`.
526+
527+
Note: Connection limiting is currently supported only for the HTTP1 client connections.
528+
529+
==== Setting connection limit in your code
530+
Below is an example of how to set connection limit to your client programmatically.
531+
532+
[source,java]
533+
----
534+
include::{sourcedir}/se/WebClientSnippets.java[tag=snippet_13,indent=0]
535+
----
536+
537+
<1> Overall connection limit set to 100. The client will not create more active connections than that.
538+
<2> Setting a per-host limit. Every host will have their connection count limited to 5.
539+
<3> This is how we can set limit only to the specific host. This overrides the per-host limit set above. This client will not create more than 2 connections to the host with the name `some-host`.
540+
<4> Disable shared cache so your cache configuration can take effect.
541+
542+
==== Setting connection limit via configuration
543+
It is also possible to set different limits via configuration.
544+
545+
[source,yaml]
546+
.Setting up Http1Client configuration.
547+
----
548+
client:
549+
share-connection-cache: false
550+
connection-cache-config:
551+
connection-limit:
552+
fixed:
553+
permits: 100
554+
connection-per-host-limit:
555+
fixed:
556+
permits: 5
557+
host-limits:
558+
- host: "some-host"
559+
limit:
560+
fixed:
561+
permits: 2
562+
----
563+
Then, in your application code, load the configuration from that file.
564+
565+
[source,java]
566+
.Http1Client initialization using the `application.yaml` file located on the classpath
567+
----
568+
include::{sourcedir}/se/WebClientSnippets.java[tag=snippet_14,indent=0]
569+
----
570+
522571
== Reference
523572
524573
* link:{webclient-javadoc-base-url}.api/module-summary.html[Helidon Webclient API]

docs/src/main/java/io/helidon/docs/se/WebClientSnippets.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package io.helidon.docs.se;
1717

18+
import io.helidon.common.concurrency.limits.FixedLimit;
1819
import io.helidon.common.media.type.MediaTypes;
1920
import io.helidon.config.Config;
2021
import io.helidon.http.Method;
@@ -24,7 +25,9 @@
2425
import io.helidon.webclient.api.HttpClientResponse;
2526
import io.helidon.webclient.api.Proxy;
2627
import io.helidon.webclient.api.WebClient;
28+
import io.helidon.webclient.http1.Http1Client;
2729
import io.helidon.webclient.http1.Http1ClientProtocolConfig;
30+
import io.helidon.webclient.http1.Http1ConnectionCacheConfig;
2831
import io.helidon.webclient.metrics.WebClientMetrics;
2932
import io.helidon.webclient.spi.WebClientService;
3033

@@ -176,4 +179,28 @@ void snippet_12() {
176179
// end::snippet_12[]
177180
}
178181

182+
void snippet_13() {
183+
// tag::snippet_13[]
184+
Http1ConnectionCacheConfig cacheConfig = Http1ConnectionCacheConfig.builder()
185+
.connectionLimit(FixedLimit.builder().permits(100).build()) // <1>
186+
.connectionPerHostLimit(FixedLimit.builder().permits(5).build()) // <2>
187+
.addHostLimit(builder -> builder.host("some-host") // <3>
188+
.limit(FixedLimit.builder().permits(2).build()))
189+
.build();
190+
191+
Http1Client client = Http1Client.builder()
192+
.shareConnectionCache(false) // <4>
193+
.connectionCacheConfig(cacheConfig)
194+
.build();
195+
// end::snippet_13[]
196+
}
197+
198+
void snippet_14() {
199+
// tag::snippet_14[]
200+
Config config = Config.create();
201+
202+
Http1Client client = Http1Client.create(config.get("client"));
203+
// end::snippet_14[]
204+
}
205+
179206
}

webclient/api/src/main/java/io/helidon/webclient/api/TcpClientConnection.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,12 @@ public void closeResource() {
192192
if (closed) {
193193
return;
194194
}
195-
try {
196-
this.socket.close();
197-
} catch (IOException e) {
198-
LOGGER.log(TRACE, "Failed to close a client socket", e);
195+
if (this.socket != null) {
196+
try {
197+
this.socket.close();
198+
} catch (IOException e) {
199+
LOGGER.log(TRACE, "Failed to close a client socket", e);
200+
}
199201
}
200202
this.closed = true;
201203
closeConsumer.accept(this);

webclient/http1/pom.xml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@
6868
<groupId>io.helidon.common</groupId>
6969
<artifactId>helidon-common-context</artifactId>
7070
</dependency>
71+
<dependency>
72+
<groupId>io.helidon.common.concurrency</groupId>
73+
<artifactId>helidon-common-concurrency-limits</artifactId>
74+
</dependency>
7175
<dependency>
7276
<groupId>io.helidon.common.features</groupId>
7377
<artifactId>helidon-common-features-api</artifactId>
@@ -103,8 +107,14 @@
103107
<scope>test</scope>
104108
</dependency>
105109
<dependency>
106-
<groupId>io.helidon.common.concurrency</groupId>
107-
<artifactId>helidon-common-concurrency-limits</artifactId>
110+
<groupId>io.helidon.config</groupId>
111+
<artifactId>helidon-config</artifactId>
112+
<scope>test</scope>
113+
</dependency>
114+
<dependency>
115+
<groupId>io.helidon.config</groupId>
116+
<artifactId>helidon-config-yaml</artifactId>
117+
<scope>test</scope>
108118
</dependency>
109119
</dependencies>
110120

webclient/http1/src/main/java/io/helidon/webclient/http1/HostLimitConfigBlueprint.java

Lines changed: 0 additions & 21 deletions
This file was deleted.

webclient/http1/src/main/java/io/helidon/webclient/http1/Http1Client.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,14 @@ static Http1Client create(Config config) {
9090
return create(it -> it.config(config));
9191
}
9292

93+
/**
94+
* Configure the default Http1 client configuration.
95+
* Note: This method needs to be used before Helidon is started to have the full effect.
96+
*
97+
* @param clientConfig global client config
98+
*/
9399
static void configureDefaults(Http1ClientConfig clientConfig) {
94100
Http1ClientImpl.GLOBAL_CONFIG.compareAndSet(null, clientConfig);
95101
}
102+
96103
}

webclient/http1/src/main/java/io/helidon/webclient/http1/Http1ClientConfigBlueprint.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,8 @@
1616

1717
package io.helidon.webclient.http1;
1818

19-
import java.util.Optional;
20-
2119
import io.helidon.builder.api.Option;
2220
import io.helidon.builder.api.Prototype;
23-
import io.helidon.common.concurrency.limits.Limit;
24-
import io.helidon.common.concurrency.limits.spi.LimitProvider;
2521
import io.helidon.webclient.api.HttpClientConfig;
2622

2723
/**
@@ -38,7 +34,13 @@ interface Http1ClientConfigBlueprint extends HttpClientConfig, Prototype.Factory
3834
@Option.Default("create()")
3935
Http1ClientProtocolConfig protocolConfig();
4036

37+
/**
38+
* Client connection cache configuration.
39+
*
40+
* @return cache configuration
41+
*/
4142
@Option.Default("create()")
42-
Http1ConnectionCacheConfig connectionCache();
43+
@Option.Configured
44+
Http1ConnectionCacheConfig connectionCacheConfig();
4345

4446
}

webclient/http1/src/main/java/io/helidon/webclient/http1/Http1ClientImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class Http1ClientImpl implements Http1Client, HttpClientSpi {
5050
this.connectionCache = Http1ConnectionCache.shared();
5151
this.clientCache = null;
5252
} else {
53-
this.connectionCache = Http1ConnectionCache.create(clientConfig.connectionCache());
53+
this.connectionCache = Http1ConnectionCache.create(clientConfig.connectionCacheConfig());
5454
this.clientCache = connectionCache;
5555
}
5656
}

0 commit comments

Comments
 (0)