Skip to content

Commit

Permalink
Merge pull request #57 from privacyidea/latch_timeout
Browse files Browse the repository at this point in the history
optimize threading
  • Loading branch information
lukasmatusiewicz authored Mar 4, 2024
2 parents e500a60 + 1e0ec02 commit fc770f8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
16 changes: 13 additions & 3 deletions src/main/java/org/privacyidea/AsyncRequestCallable.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
Expand All @@ -43,7 +44,8 @@ public class AsyncRequestCallable implements Callable<String>, Callback
final String[] callbackResult = {null};
private CountDownLatch latch;

public AsyncRequestCallable(PrivacyIDEA privacyIDEA, Endpoint endpoint, String path, Map<String, String> params, Map<String, String> headers, boolean authTokenRequired, String method)
public AsyncRequestCallable(PrivacyIDEA privacyIDEA, Endpoint endpoint, String path, Map<String, String> params,
Map<String, String> headers, boolean authTokenRequired, String method)
{
this.privacyIDEA = privacyIDEA;
this.endpoint = endpoint;
Expand All @@ -69,7 +71,11 @@ public String call() throws Exception
String tmpPath = path;
path = ENDPOINT_AUTH;
endpoint.sendRequestAsync(ENDPOINT_AUTH, privacyIDEA.serviceAccountParam(), Collections.emptyMap(), PIConstants.POST, this);
latch.await();
if (!latch.await(30, TimeUnit.SECONDS))
{
privacyIDEA.error("Latch timed out...");
return "";
}
// Extract the auth token from the response
String response = callbackResult[0];
String authToken = privacyIDEA.parser.extractAuthToken(response);
Expand All @@ -87,7 +93,11 @@ public String call() throws Exception
// Do the actual request
latch = new CountDownLatch(1);
endpoint.sendRequestAsync(path, params, headers, method, this);
latch.await();
if (!latch.await(30, TimeUnit.SECONDS))
{
privacyIDEA.error("Latch timed out...");
return "";
}
return callbackResult[0];
}

Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/privacyidea/Endpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
Expand Down
16 changes: 12 additions & 4 deletions src/main/java/org/privacyidea/PrivacyIDEA.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package org.privacyidea;

import java.io.Closeable;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
Expand All @@ -26,7 +28,6 @@
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -54,15 +55,15 @@
* This is the main class. It implements the common endpoints such as /validate/check as methods for easy usage.
* To create an instance of this class, use the nested PrivacyIDEA.Builder class.
*/
public class PrivacyIDEA
public class PrivacyIDEA implements Closeable
{
private final PIConfig configuration;
private final IPILogger log;
private final IPISimpleLogger simpleLog;
private final Endpoint endpoint;
// Thread pool for connections
private final BlockingQueue<Runnable> queue = new ArrayBlockingQueue<>(100);
private final ExecutorService threadPool = new ThreadPoolExecutor(20, 20, 10, TimeUnit.SECONDS, queue);
private final BlockingQueue<Runnable> queue = new ArrayBlockingQueue<>(1000);
private final ThreadPoolExecutor threadPool = new ThreadPoolExecutor(20, 20, 10, TimeUnit.SECONDS, queue);
final JSONParser parser;
// Responses from these endpoints will not be logged. The list can be overwritten.
private List<String> logExcludedEndpoints = Arrays.asList(PIConstants.ENDPOINT_AUTH,
Expand All @@ -75,6 +76,7 @@ private PrivacyIDEA(PIConfig configuration, IPILogger logger, IPISimpleLogger si
this.configuration = configuration;
this.endpoint = new Endpoint(this);
this.parser = new JSONParser(this);
this.threadPool.allowCoreThreadTimeOut(true);
}

/**
Expand Down Expand Up @@ -535,6 +537,12 @@ else if (this.simpleLog != null)
}
}

@Override
public void close() throws IOException
{
this.threadPool.shutdown();
}

/**
* Get a new Builder to create a PrivacyIDEA instance.
*
Expand Down

0 comments on commit fc770f8

Please sign in to comment.