Skip to content

Commit 50f1f56

Browse files
committed
Move to HttpSettings
1 parent 333f95f commit 50f1f56

File tree

4 files changed

+26
-55
lines changed

4 files changed

+26
-55
lines changed

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/external/http/HttpClient.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,21 +55,20 @@ public static HttpClient create(
5555
HttpSettings settings,
5656
ThreadPool threadPool,
5757
PoolingNHttpClientConnectionManager connectionManager,
58-
ThrottlerManager throttlerManager,
59-
RequestConfig requestConfig
58+
ThrottlerManager throttlerManager
6059
) {
61-
var client = createAsyncClient(Objects.requireNonNull(connectionManager), Objects.requireNonNull(requestConfig));
60+
var client = createAsyncClient(Objects.requireNonNull(connectionManager), Objects.requireNonNull(settings));
6261

6362
return new HttpClient(settings, client, threadPool, throttlerManager);
6463
}
6564

6665
private static CloseableHttpAsyncClient createAsyncClient(
6766
PoolingNHttpClientConnectionManager connectionManager,
68-
RequestConfig requestConfig
67+
HttpSettings settings
6968
) {
70-
HttpAsyncClientBuilder clientBuilder = HttpAsyncClientBuilder.create()
71-
.setConnectionManager(connectionManager)
72-
.setDefaultRequestConfig(requestConfig);
69+
var requestConfig = RequestConfig.custom().setConnectTimeout(settings.connectionTimeout()).build();
70+
71+
var clientBuilder = HttpAsyncClientBuilder.create().setConnectionManager(connectionManager).setDefaultRequestConfig(requestConfig);
7372
// The apache client will be shared across all connections because it can be expensive to create it
7473
// so we don't want to support cookies to avoid accidental authentication for unauthorized users
7574
clientBuilder.disableCookieManagement();

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/external/http/HttpClientManager.java

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
package org.elasticsearch.xpack.inference.external.http;
99

10-
import org.apache.http.client.config.RequestConfig;
1110
import org.apache.http.config.Registry;
1211
import org.apache.http.config.RegistryBuilder;
1312
import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager;
@@ -75,7 +74,6 @@ public class HttpClientManager implements Closeable {
7574
);
7675

7776
private static final TimeValue DEFAULT_CONNECTION_MAX_IDLE_TIME_SETTING = DEFAULT_CONNECTION_EVICTION_THREAD_INTERVAL_TIME;
78-
private static final TimeValue DEFAULT_CONNECT_TIMEOUT = TimeValue.timeValueSeconds(5);
7977
/**
8078
* The max duration of time for a connection to be marked as idle and ready to be closed. This defines the amount of time
8179
* a connection can be unused in the connection pool before being closed the next time the eviction thread runs.
@@ -91,12 +89,6 @@ public class HttpClientManager implements Closeable {
9189
Setting.Property.Dynamic
9290
);
9391

94-
public static final Setting<TimeValue> CONNECTION_TIMEOUT = Setting.timeSetting(
95-
"xpack.inference.http.connect_timeout",
96-
DEFAULT_CONNECT_TIMEOUT,
97-
Setting.Property.NodeScope
98-
);
99-
10092
private final ThreadPool threadPool;
10193
private final PoolingNHttpClientConnectionManager connectionManager;
10294
private IdleConnectionEvictor connectionEvictor;
@@ -145,15 +137,7 @@ public static HttpClientManager create(
145137
setMaxConnections(MAX_TOTAL_CONNECTIONS.get(settings));
146138
setMaxRouteConnections(MAX_ROUTE_CONNECTIONS.get(settings));
147139

148-
var requestConfig = RequestConfig.custom().setConnectTimeout(Math.toIntExact(CONNECTION_TIMEOUT.get(settings).getMillis())).build();
149-
150-
this.httpClient = HttpClient.create(
151-
new HttpSettings(settings, clusterService),
152-
threadPool,
153-
connectionManager,
154-
throttlerManager,
155-
requestConfig
156-
);
140+
this.httpClient = HttpClient.create(new HttpSettings(settings, clusterService), threadPool, connectionManager, throttlerManager);
157141

158142
this.evictionInterval = CONNECTION_EVICTION_THREAD_INTERVAL_SETTING.get(settings);
159143
this.connectionMaxIdle = CONNECTION_MAX_IDLE_TIME_SETTING.get(settings);
@@ -218,8 +202,7 @@ public static List<Setting<?>> getSettingsDefinitions() {
218202
MAX_TOTAL_CONNECTIONS,
219203
MAX_ROUTE_CONNECTIONS,
220204
CONNECTION_EVICTION_THREAD_INTERVAL_SETTING,
221-
CONNECTION_MAX_IDLE_TIME_SETTING,
222-
CONNECTION_TIMEOUT
205+
CONNECTION_MAX_IDLE_TIME_SETTING
223206
);
224207
}
225208

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/external/http/HttpSettings.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.elasticsearch.common.settings.Settings;
1313
import org.elasticsearch.common.unit.ByteSizeUnit;
1414
import org.elasticsearch.common.unit.ByteSizeValue;
15+
import org.elasticsearch.core.TimeValue;
1516

1617
import java.util.List;
1718
import java.util.Objects;
@@ -27,12 +28,21 @@ public class HttpSettings {
2728
Setting.Property.Dynamic
2829
);
2930

31+
// The time we wait for a connection to establish
32+
public static final Setting<TimeValue> CONNECTION_TIMEOUT = Setting.timeSetting(
33+
"xpack.inference.http.connect_timeout",
34+
TimeValue.timeValueSeconds(5),
35+
Setting.Property.NodeScope
36+
);
37+
3038
private volatile ByteSizeValue maxResponseSize;
39+
private final int connectionTimeout;
3140

3241
public HttpSettings(Settings settings, ClusterService clusterService) {
3342
Objects.requireNonNull(clusterService);
3443
Objects.requireNonNull(settings);
3544
maxResponseSize = MAX_HTTP_RESPONSE_SIZE.get(settings);
45+
connectionTimeout = Math.toIntExact(CONNECTION_TIMEOUT.get(settings).getMillis());
3646

3747
clusterService.getClusterSettings().addSettingsUpdateConsumer(MAX_HTTP_RESPONSE_SIZE, this::setMaxResponseSize);
3848
}
@@ -41,11 +51,15 @@ public ByteSizeValue getMaxResponseSize() {
4151
return maxResponseSize;
4252
}
4353

54+
public int connectionTimeout() {
55+
return connectionTimeout;
56+
}
57+
4458
private void setMaxResponseSize(ByteSizeValue maxResponseSize) {
4559
this.maxResponseSize = maxResponseSize;
4660
}
4761

4862
public static List<Setting<?>> getSettingsDefinitions() {
49-
return List.of(MAX_HTTP_RESPONSE_SIZE);
63+
return List.of(MAX_HTTP_RESPONSE_SIZE, CONNECTION_TIMEOUT);
5064
}
5165
}

x-pack/plugin/inference/src/test/java/org/elasticsearch/xpack/inference/external/http/HttpClientTests.java

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
package org.elasticsearch.xpack.inference.external.http;
99

1010
import org.apache.http.HttpHeaders;
11-
import org.apache.http.client.config.RequestConfig;
1211
import org.apache.http.client.methods.HttpPost;
1312
import org.apache.http.client.methods.HttpUriRequest;
1413
import org.apache.http.client.protocol.HttpClientContext;
@@ -85,15 +84,7 @@ public void testSend_MockServerReceivesRequest() throws Exception {
8584
String paramValue = randomAlphaOfLength(3);
8685
var httpPost = createHttpPost(webServer.getPort(), paramKey, paramValue);
8786

88-
try (
89-
var httpClient = HttpClient.create(
90-
emptyHttpSettings(),
91-
threadPool,
92-
createConnectionManager(),
93-
mockThrottlerManager(),
94-
RequestConfig.DEFAULT
95-
)
96-
) {
87+
try (var httpClient = HttpClient.create(emptyHttpSettings(), threadPool, createConnectionManager(), mockThrottlerManager())) {
9788
httpClient.start();
9889

9990
PlainActionFuture<HttpResult> listener = new PlainActionFuture<>();
@@ -111,15 +102,7 @@ public void testSend_MockServerReceivesRequest() throws Exception {
111102
}
112103

113104
public void testSend_ThrowsErrorIfCalledBeforeStart() throws Exception {
114-
try (
115-
var httpClient = HttpClient.create(
116-
emptyHttpSettings(),
117-
threadPool,
118-
createConnectionManager(),
119-
mockThrottlerManager(),
120-
RequestConfig.DEFAULT
121-
)
122-
) {
105+
try (var httpClient = HttpClient.create(emptyHttpSettings(), threadPool, createConnectionManager(), mockThrottlerManager())) {
123106
PlainActionFuture<HttpResult> listener = new PlainActionFuture<>();
124107
var thrownException = expectThrows(
125108
AssertionError.class,
@@ -254,15 +237,7 @@ public void testSend_FailsWhenMaxBytesReadIsExceeded() throws Exception {
254237
Settings settings = Settings.builder().put(HttpSettings.MAX_HTTP_RESPONSE_SIZE.getKey(), ByteSizeValue.ONE).build();
255238
var httpSettings = createHttpSettings(settings);
256239

257-
try (
258-
var httpClient = HttpClient.create(
259-
httpSettings,
260-
threadPool,
261-
createConnectionManager(),
262-
mockThrottlerManager(),
263-
RequestConfig.DEFAULT
264-
)
265-
) {
240+
try (var httpClient = HttpClient.create(httpSettings, threadPool, createConnectionManager(), mockThrottlerManager())) {
266241
httpClient.start();
267242

268243
PlainActionFuture<HttpResult> listener = new PlainActionFuture<>();

0 commit comments

Comments
 (0)