-
Notifications
You must be signed in to change notification settings - Fork 483
Description
Description
When using GitLab4J API in a multi-threaded environment, some operations (e.g. connectionTest() or getVersion()) may fail intermittently due to the underlying Jersey Client sharing resources across threads.
Problem
Default JAX-RS client (Jersey) uses shared connection pools internally.
Concurrent calls like connectionTest() may cause race conditions, leading to intermittent failures.
GitLab4J (>=5.x) supports custom client injection, but this is not clearly documented, and many users perceive it as a library bug.
Example Code (to reproduce)
import org.gitlab4j.api.GitLabApi;
import org.gitlab4j.api.exceptions.GitLabApiException;
import java.util.stream.IntStream;
public class GitLabApiThreadSafetyTest {
public static void main(String[] args) throws Exception {
String url = "https://gitlab.example.com";
String token = "YOUR_PERSONAL_ACCESS_TOKEN";
GitLabApi gitLabApi = new GitLabApi(url, token);
// Run connectionTest() concurrently in multiple threads
IntStream.range(0, 10).parallel().forEach(i -> {
try {
boolean ok = gitLabApi.getVersion() != null;
System.out.println("Thread " + i + " success: " + ok);
} catch (GitLabApiException e) {
System.err.println("Thread " + i + " failed: " + e.getMessage());
}
});
}
}
Example Output (Observed)
Thread 0 success: true
Thread 1 failed: java.net.SocketException: Connection reset
Thread 2 success: true
Thread 3 failed: javax.ws.rs.ProcessingException: Premature EOF
Thread 4 success: true
...
Suggested Improvements
Documentation update
Clarify in README/Javadoc that Jersey Client may not be thread-safe under high concurrency.
Recommend OkHttp (or another proven thread-safe JAX-RS client) for multi-threaded workloads.
Convenience API
Provide a static factory method to easily create GitLabApi with OkHttp:
GitLabApi gitLabApi = GitLabApi.withOkHttp(ApiVersion.V4, url, token);
Tests
Add a multi-threaded test case (similar to the above) in the test suite to demonstrate stable behavior with OkHttp.
Benefits
Prevents confusion for new users.
Makes GitLab4J safer in real-world multi-threaded applications.
Backwards-compatible, minimal changes required.