Skip to content

Commit 61b8575

Browse files
committed
use connectTimeout for all API calls
fixes #4210
1 parent 9e3c7f7 commit 61b8575

File tree

4 files changed

+32
-17
lines changed

4 files changed

+32
-17
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/configuration/RuntimeEnvironment.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1555,22 +1555,27 @@ public String getConfigurationXML() {
15551555
public void writeConfiguration(String host) throws IOException, InterruptedException, IllegalArgumentException {
15561556
String configXML = syncReadConfiguration(Configuration::getXMLRepresentationAsString);
15571557

1558-
Response response = ClientBuilder.newClient()
1558+
try (Response response = ClientBuilder.newBuilder().
1559+
connectTimeout(getConnectTimeout(), TimeUnit.SECONDS).build()
15591560
.target(host)
15601561
.path("api")
15611562
.path("v1")
15621563
.path("configuration")
15631564
.queryParam("reindex", true)
15641565
.request()
15651566
.headers(getWebAppHeaders())
1566-
.put(Entity.xml(configXML));
1567+
.put(Entity.xml(configXML))) {
15671568

1568-
if (response.getStatus() == Response.Status.ACCEPTED.getStatusCode()) {
1569-
response = ApiUtils.waitForAsyncApi(response);
1570-
}
1569+
Response.StatusType statusType = response.getStatusInfo();
1570+
1571+
if (response.getStatus() == Response.Status.ACCEPTED.getStatusCode()) {
1572+
Response apiResponse = ApiUtils.waitForAsyncApi(response);
1573+
statusType = apiResponse.getStatusInfo();
1574+
}
15711575

1572-
if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
1573-
throw new IOException(response.toString());
1576+
if (statusType.getFamily() != Response.Status.Family.SUCCESSFUL) {
1577+
throw new IOException(response.toString());
1578+
}
15741579
}
15751580
}
15761581

opengrok-indexer/src/main/java/org/opengrok/indexer/index/IndexerUtil.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
import jakarta.ws.rs.core.MultivaluedMap;
3737
import jakarta.ws.rs.core.Response;
3838

39+
import java.util.concurrent.TimeUnit;
40+
3941
public class IndexerUtil {
4042

4143
private IndexerUtil() {
@@ -70,7 +72,8 @@ public static void enableProjects(final String host) throws
7072
ProcessingException,
7173
WebApplicationException {
7274

73-
try (Client client = ClientBuilder.newClient()) {
75+
try (Client client = ClientBuilder.newBuilder().
76+
connectTimeout(RuntimeEnvironment.getInstance().getConnectTimeout(), TimeUnit.SECONDS).build()) {
7477
final Invocation.Builder request = client.target(host)
7578
.path("api")
7679
.path("v1")

opengrok-indexer/src/main/java/org/opengrok/indexer/web/ApiUtils.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2021, 2022, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2021, 2023, Oracle and/or its affiliates. All rights reserved.
2222
*/
2323
package org.opengrok.indexer.web;
2424

@@ -37,6 +37,8 @@ public class ApiUtils {
3737

3838
private static final Logger LOGGER = LoggerFactory.getLogger(ApiUtils.class);
3939

40+
private static final RuntimeEnvironment env = RuntimeEnvironment.getInstance();
41+
4042
private ApiUtils() {
4143
// utility class
4244
}
@@ -66,9 +68,9 @@ private ApiUtils() {
6668
}
6769

6870
LOGGER.log(Level.FINER, "checking asynchronous API result on {0}", location);
69-
for (int i = 0; i < RuntimeEnvironment.getInstance().getApiTimeout(); i++) {
71+
for (int i = 0; i < env.getApiTimeout(); i++) {
7072
response = ClientBuilder.newBuilder().
71-
connectTimeout(RuntimeEnvironment.getInstance().getConnectTimeout(), TimeUnit.SECONDS).build().
73+
connectTimeout(env.getConnectTimeout(), TimeUnit.SECONDS).build().
7274
target(location).request().get();
7375
if (response.getStatus() == Response.Status.ACCEPTED.getStatusCode()) {
7476
Thread.sleep(1000);
@@ -83,11 +85,13 @@ private ApiUtils() {
8385
}
8486

8587
LOGGER.log(Level.FINER, "making DELETE API request to {0}", location);
86-
Response deleteResponse = ClientBuilder.newBuilder().connectTimeout(3, TimeUnit.SECONDS).build().
87-
target(location).request().delete();
88-
if (deleteResponse.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
89-
LOGGER.log(Level.WARNING, "DELETE API call to {0} failed with HTTP error {1}",
90-
new Object[]{location, response.getStatusInfo()});
88+
try (Response deleteResponse = ClientBuilder.newBuilder().
89+
connectTimeout(env.getConnectTimeout(), TimeUnit.SECONDS).build().
90+
target(location).request().delete()) {
91+
if (deleteResponse.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
92+
LOGGER.log(Level.WARNING, "DELETE API call to {0} failed with HTTP error {1}",
93+
new Object[]{location, response.getStatusInfo()});
94+
}
9195
}
9296

9397
return response;

plugins/src/main/java/opengrok/auth/plugin/util/RestfulClient.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
2929
import jakarta.ws.rs.client.Entity;
3030
import jakarta.ws.rs.core.MediaType;
3131
import jakarta.ws.rs.core.Response;
32+
import org.opengrok.indexer.configuration.RuntimeEnvironment;
3233

34+
import java.util.concurrent.TimeUnit;
3335
import java.util.logging.Level;
3436
import java.util.logging.Logger;
3537

@@ -52,7 +54,8 @@ private RestfulClient() {
5254
public static int postIt(String uri, String input) {
5355
int result;
5456
try {
55-
try (Client client = ClientBuilder.newClient()) {
57+
try (Client client = ClientBuilder.newBuilder().
58+
connectTimeout(RuntimeEnvironment.getInstance().getConnectTimeout(), TimeUnit.SECONDS).build()) {
5659
LOGGER.log(Level.FINEST, "sending REST POST request to {0}: {1}",
5760
new Object[]{uri, input});
5861
try (Response response = client.target(uri)

0 commit comments

Comments
 (0)