Skip to content

Commit fc770f8

Browse files
Merge pull request #57 from privacyidea/latch_timeout
optimize threading
2 parents e500a60 + 1e0ec02 commit fc770f8

3 files changed

Lines changed: 26 additions & 7 deletions

File tree

src/main/java/org/privacyidea/AsyncRequestCallable.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Map;
2222
import java.util.concurrent.Callable;
2323
import java.util.concurrent.CountDownLatch;
24+
import java.util.concurrent.TimeUnit;
2425
import okhttp3.Call;
2526
import okhttp3.Callback;
2627
import okhttp3.Response;
@@ -43,7 +44,8 @@ public class AsyncRequestCallable implements Callable<String>, Callback
4344
final String[] callbackResult = {null};
4445
private CountDownLatch latch;
4546

46-
public AsyncRequestCallable(PrivacyIDEA privacyIDEA, Endpoint endpoint, String path, Map<String, String> params, Map<String, String> headers, boolean authTokenRequired, String method)
47+
public AsyncRequestCallable(PrivacyIDEA privacyIDEA, Endpoint endpoint, String path, Map<String, String> params,
48+
Map<String, String> headers, boolean authTokenRequired, String method)
4749
{
4850
this.privacyIDEA = privacyIDEA;
4951
this.endpoint = endpoint;
@@ -69,7 +71,11 @@ public String call() throws Exception
6971
String tmpPath = path;
7072
path = ENDPOINT_AUTH;
7173
endpoint.sendRequestAsync(ENDPOINT_AUTH, privacyIDEA.serviceAccountParam(), Collections.emptyMap(), PIConstants.POST, this);
72-
latch.await();
74+
if (!latch.await(30, TimeUnit.SECONDS))
75+
{
76+
privacyIDEA.error("Latch timed out...");
77+
return "";
78+
}
7379
// Extract the auth token from the response
7480
String response = callbackResult[0];
7581
String authToken = privacyIDEA.parser.extractAuthToken(response);
@@ -87,7 +93,11 @@ public String call() throws Exception
8793
// Do the actual request
8894
latch = new CountDownLatch(1);
8995
endpoint.sendRequestAsync(path, params, headers, method, this);
90-
latch.await();
96+
if (!latch.await(30, TimeUnit.SECONDS))
97+
{
98+
privacyIDEA.error("Latch timed out...");
99+
return "";
100+
}
91101
return callbackResult[0];
92102
}
93103

src/main/java/org/privacyidea/Endpoint.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.security.KeyManagementException;
2424
import java.security.NoSuchAlgorithmException;
2525
import java.util.Map;
26+
import java.util.concurrent.TimeUnit;
2627
import javax.net.ssl.SSLContext;
2728
import javax.net.ssl.SSLSocketFactory;
2829
import javax.net.ssl.TrustManager;

src/main/java/org/privacyidea/PrivacyIDEA.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*/
1717
package org.privacyidea;
1818

19+
import java.io.Closeable;
20+
import java.io.IOException;
1921
import java.util.Arrays;
2022
import java.util.Collections;
2123
import java.util.LinkedHashMap;
@@ -26,7 +28,6 @@
2628
import java.util.concurrent.BlockingQueue;
2729
import java.util.concurrent.Callable;
2830
import java.util.concurrent.ExecutionException;
29-
import java.util.concurrent.ExecutorService;
3031
import java.util.concurrent.Future;
3132
import java.util.concurrent.ThreadPoolExecutor;
3233
import java.util.concurrent.TimeUnit;
@@ -54,15 +55,15 @@
5455
* This is the main class. It implements the common endpoints such as /validate/check as methods for easy usage.
5556
* To create an instance of this class, use the nested PrivacyIDEA.Builder class.
5657
*/
57-
public class PrivacyIDEA
58+
public class PrivacyIDEA implements Closeable
5859
{
5960
private final PIConfig configuration;
6061
private final IPILogger log;
6162
private final IPISimpleLogger simpleLog;
6263
private final Endpoint endpoint;
6364
// Thread pool for connections
64-
private final BlockingQueue<Runnable> queue = new ArrayBlockingQueue<>(100);
65-
private final ExecutorService threadPool = new ThreadPoolExecutor(20, 20, 10, TimeUnit.SECONDS, queue);
65+
private final BlockingQueue<Runnable> queue = new ArrayBlockingQueue<>(1000);
66+
private final ThreadPoolExecutor threadPool = new ThreadPoolExecutor(20, 20, 10, TimeUnit.SECONDS, queue);
6667
final JSONParser parser;
6768
// Responses from these endpoints will not be logged. The list can be overwritten.
6869
private List<String> logExcludedEndpoints = Arrays.asList(PIConstants.ENDPOINT_AUTH,
@@ -75,6 +76,7 @@ private PrivacyIDEA(PIConfig configuration, IPILogger logger, IPISimpleLogger si
7576
this.configuration = configuration;
7677
this.endpoint = new Endpoint(this);
7778
this.parser = new JSONParser(this);
79+
this.threadPool.allowCoreThreadTimeOut(true);
7880
}
7981

8082
/**
@@ -535,6 +537,12 @@ else if (this.simpleLog != null)
535537
}
536538
}
537539

540+
@Override
541+
public void close() throws IOException
542+
{
543+
this.threadPool.shutdown();
544+
}
545+
538546
/**
539547
* Get a new Builder to create a PrivacyIDEA instance.
540548
*

0 commit comments

Comments
 (0)