Skip to content

Commit e698da7

Browse files
authored
moved "property" calls on Jersey APIs (#1312)
- Mutating the client (which property calls do) after creation, changes the ClientConfig mode to RequestScoped. This forces Jersey to reinitialize the injection scope with each request, which degrades performance when many API calls are made
1 parent eb69fa0 commit e698da7

1 file changed

Lines changed: 13 additions & 38 deletions

File tree

src/main/java/org/gitlab4j/api/GitLabApiClient.java

Lines changed: 13 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,9 @@
1515
import java.util.logging.Level;
1616
import java.util.logging.Logger;
1717

18-
import javax.net.ssl.HostnameVerifier;
19-
import javax.net.ssl.SSLContext;
20-
import javax.net.ssl.SSLEngine;
21-
import javax.net.ssl.SSLSession;
22-
import javax.net.ssl.TrustManager;
23-
import javax.net.ssl.X509ExtendedTrustManager;
24-
import javax.ws.rs.client.Client;
25-
import javax.ws.rs.client.ClientBuilder;
26-
import javax.ws.rs.client.Entity;
27-
import javax.ws.rs.client.Invocation;
28-
import javax.ws.rs.client.WebTarget;
29-
import javax.ws.rs.core.Form;
30-
import javax.ws.rs.core.MediaType;
31-
import javax.ws.rs.core.MultivaluedMap;
32-
import javax.ws.rs.core.Response;
33-
import javax.ws.rs.core.StreamingOutput;
18+
import javax.net.ssl.*;
19+
import javax.ws.rs.client.*;
20+
import javax.ws.rs.core.*;
3421

3522
import org.gitlab4j.api.Constants.TokenType;
3623
import org.gitlab4j.api.GitLabApi.ApiVersion;
@@ -41,12 +28,7 @@
4128
import org.glassfish.jersey.client.ClientProperties;
4229
import org.glassfish.jersey.client.JerseyClientBuilder;
4330
import org.glassfish.jersey.jackson.JacksonFeature;
44-
import org.glassfish.jersey.media.multipart.BodyPart;
45-
import org.glassfish.jersey.media.multipart.Boundary;
46-
import org.glassfish.jersey.media.multipart.FormDataBodyPart;
47-
import org.glassfish.jersey.media.multipart.FormDataMultiPart;
48-
import org.glassfish.jersey.media.multipart.MultiPart;
49-
import org.glassfish.jersey.media.multipart.MultiPartFeature;
31+
import org.glassfish.jersey.media.multipart.*;
5032
import org.glassfish.jersey.media.multipart.file.FileDataBodyPart;
5133
import org.glassfish.jersey.media.multipart.file.StreamDataBodyPart;
5234

@@ -72,9 +54,6 @@ public class GitLabApiClient implements AutoCloseable {
7254
private SSLContext openSslContext;
7355
private HostnameVerifier openHostnameVerifier;
7456
private Long sudoAsId;
75-
private Integer connectTimeout;
76-
private Integer readTimeout;
77-
7857
/**
7958
* Construct an instance to communicate with a GitLab API server using the specified GitLab API version,
8059
* server URL, private token, and secret token.
@@ -259,6 +238,7 @@ public GitLabApiClient(
259238
// to use the features and services explicitly configured by gitlab4j
260239
clientConfig.property(ClientProperties.FEATURE_AUTO_DISCOVERY_DISABLE, true);
261240
clientConfig.property(ClientProperties.METAINF_SERVICES_LOOKUP_DISABLE, true);
241+
clientConfig.property(ClientProperties.FOLLOW_REDIRECTS, true);
262242

263243
clientConfig.register(JacksonJson.class);
264244
clientConfig.register(JacksonFeature.class);
@@ -304,8 +284,13 @@ void enableRequestResponseLogging(Logger logger, Level level, int maxEntityLengt
304284
* @param readTimeout the per request read timeout in milliseconds, can be null to use default
305285
*/
306286
void setRequestTimeout(Integer connectTimeout, Integer readTimeout) {
307-
this.connectTimeout = connectTimeout;
308-
this.readTimeout = readTimeout;
287+
clientConfig.property(ClientProperties.CONNECT_TIMEOUT, connectTimeout);
288+
clientConfig.property(ClientProperties.READ_TIMEOUT, readTimeout);
289+
290+
// Recreate the Client instance if already created.
291+
if (apiClient != null) {
292+
createApiClient();
293+
}
309294
}
310295

311296
/**
@@ -855,7 +840,7 @@ protected Invocation.Builder invocation(URL url, MultivaluedMap<String, String>
855840
createApiClient();
856841
}
857842

858-
WebTarget target = apiClient.target(url.toExternalForm()).property(ClientProperties.FOLLOW_REDIRECTS, true);
843+
WebTarget target = apiClient.target(url.toExternalForm());
859844
if (queryParams != null) {
860845
for (Map.Entry<String, List<String>> param : queryParams.entrySet()) {
861846
target = target.queryParam(param.getKey(), param.getValue().toArray());
@@ -874,16 +859,6 @@ protected Invocation.Builder invocation(URL url, MultivaluedMap<String, String>
874859
// If sudo as ID is set add the Sudo header
875860
if (sudoAsId != null && sudoAsId.intValue() > 0) builder = builder.header(SUDO_HEADER, sudoAsId);
876861

877-
// Set the per request connect timeout
878-
if (connectTimeout != null) {
879-
builder.property(ClientProperties.CONNECT_TIMEOUT, connectTimeout);
880-
}
881-
882-
// Set the per request read timeout
883-
if (readTimeout != null) {
884-
builder.property(ClientProperties.READ_TIMEOUT, readTimeout);
885-
}
886-
887862
return (builder);
888863
}
889864

0 commit comments

Comments
 (0)