Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use sendThreadPool in sendEvents #107

Merged
merged 5 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/main/java/com/amplitude/Amplitude.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.net.Proxy;
import java.util.*;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutorService;

public class Amplitude {
private static Map<String, Amplitude> instances = new HashMap<>();
Expand Down Expand Up @@ -206,6 +207,26 @@ public Amplitude setFlushTimeout(long timeout) {
return this;
}

/**
* Set the thread pool for sending events via {@link HttpTransport}
*
* @param sendThreadPool the thread pool for sending events
*/
public Amplitude setSendThreadPool(ExecutorService sendThreadPool) {
this.httpTransport.setSendThreadPool(sendThreadPool);
return this;
}

/**
* Set the thread pool for retrying events via {@link HttpTransport}
*
* @param retryThreadPool the thread pool for retrying events
*/
public Amplitude setRetryThreadPool(ExecutorService retryThreadPool) {
this.httpTransport.setRetryThreadPool(retryThreadPool);
return this;
}

/** Add middleware to the middleware runner */
public synchronized void addEventMiddleware(Middleware middleware) {
middlewareRunner.add(middleware);
Expand Down
25 changes: 20 additions & 5 deletions src/main/java/com/amplitude/HttpTransport.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,29 @@ class HttpTransport {
private int eventsInRetry = 0;
private Object bufferLock = new Object();
private Object counterLock = new Object();
private ExecutorService retryThreadPool;
private ExecutorService sendThreadPool;

private HttpCall httpCall;
private AmplitudeLog logger;
private AmplitudeCallbacks callbacks;
private long flushTimeout;

// Managed by setters
private ExecutorService retryThreadPool = Executors.newFixedThreadPool(10);

// The supplyAsyncPool is only used within the sendThreadPool so only when
// the sendThreadPool is increased will the supplyAsyncPool be more utilized.
// We are using the supplyAsyncPool rather than the default fork join common
// pool because the fork join common pool scales with cpu... and we do not
// want to perform network requests in that small pool.
private ExecutorService sendThreadPool = Executors.newFixedThreadPool(20);
private ExecutorService supplyAsyncPool = Executors.newCachedThreadPool();

HttpTransport(
HttpCall httpCall, AmplitudeCallbacks callbacks, AmplitudeLog logger, long flushTimeout) {
this.httpCall = httpCall;
this.callbacks = callbacks;
this.logger = logger;
this.flushTimeout = flushTimeout;
retryThreadPool = Executors.newFixedThreadPool(10);
sendThreadPool = Executors.newFixedThreadPool(20);
}

public void sendEventsWithRetry(List<Event> events) {
Expand Down Expand Up @@ -98,6 +105,14 @@ public void setFlushTimeout(long timeout) {
flushTimeout = timeout;
}

public void setSendThreadPool(ExecutorService sendThreadPool) {
this.sendThreadPool = sendThreadPool;
}

public void setRetryThreadPool(ExecutorService retryThreadPool) {
this.retryThreadPool = retryThreadPool;
}

public void setCallbacks(AmplitudeCallbacks callbacks) {
this.callbacks = callbacks;
}
Expand All @@ -118,7 +133,7 @@ private CompletableFuture<Response> sendEvents(List<Event> events) {
throw new CompletionException(e);
}
return response;
});
}, supplyAsyncPool);
}

// Call this function if event not in current Retry list.
Expand Down
Loading