Skip to content

Commit 88a9589

Browse files
kevink-sqizaaz
andauthored
fix: use sendThreadPool in sendEvents (#107)
* fix: use sendThreadPool in sendEvents * remove redundant thread pools in Amplitude.java * chore: fix build * Separate executors * Separate executors --------- Co-authored-by: izaaz.yunus <[email protected]>
1 parent 6dfc6bb commit 88a9589

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

src/main/java/com/amplitude/Amplitude.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.net.Proxy;
44
import java.util.*;
55
import java.util.concurrent.ConcurrentLinkedQueue;
6+
import java.util.concurrent.ExecutorService;
67

78
public class Amplitude {
89
private static Map<String, Amplitude> instances = new HashMap<>();
@@ -206,6 +207,26 @@ public Amplitude setFlushTimeout(long timeout) {
206207
return this;
207208
}
208209

210+
/**
211+
* Set the thread pool for sending events via {@link HttpTransport}
212+
*
213+
* @param sendThreadPool the thread pool for sending events
214+
*/
215+
public Amplitude setSendThreadPool(ExecutorService sendThreadPool) {
216+
this.httpTransport.setSendThreadPool(sendThreadPool);
217+
return this;
218+
}
219+
220+
/**
221+
* Set the thread pool for retrying events via {@link HttpTransport}
222+
*
223+
* @param retryThreadPool the thread pool for retrying events
224+
*/
225+
public Amplitude setRetryThreadPool(ExecutorService retryThreadPool) {
226+
this.httpTransport.setRetryThreadPool(retryThreadPool);
227+
return this;
228+
}
229+
209230
/** Add middleware to the middleware runner */
210231
public synchronized void addEventMiddleware(Middleware middleware) {
211232
middlewareRunner.add(middleware);

src/main/java/com/amplitude/HttpTransport.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,29 @@ class HttpTransport {
3838
private int eventsInRetry = 0;
3939
private Object bufferLock = new Object();
4040
private Object counterLock = new Object();
41-
private ExecutorService retryThreadPool;
42-
private ExecutorService sendThreadPool;
4341

4442
private HttpCall httpCall;
4543
private AmplitudeLog logger;
4644
private AmplitudeCallbacks callbacks;
4745
private long flushTimeout;
4846

47+
// Managed by setters
48+
private ExecutorService retryThreadPool = Executors.newFixedThreadPool(10);
49+
50+
// The supplyAsyncPool is only used within the sendThreadPool so only when
51+
// the sendThreadPool is increased will the supplyAsyncPool be more utilized.
52+
// We are using the supplyAsyncPool rather than the default fork join common
53+
// pool because the fork join common pool scales with cpu... and we do not
54+
// want to perform network requests in that small pool.
55+
private ExecutorService sendThreadPool = Executors.newFixedThreadPool(20);
56+
private ExecutorService supplyAsyncPool = Executors.newCachedThreadPool();
57+
4958
HttpTransport(
5059
HttpCall httpCall, AmplitudeCallbacks callbacks, AmplitudeLog logger, long flushTimeout) {
5160
this.httpCall = httpCall;
5261
this.callbacks = callbacks;
5362
this.logger = logger;
5463
this.flushTimeout = flushTimeout;
55-
retryThreadPool = Executors.newFixedThreadPool(10);
56-
sendThreadPool = Executors.newFixedThreadPool(20);
5764
}
5865

5966
public void sendEventsWithRetry(List<Event> events) {
@@ -98,6 +105,14 @@ public void setFlushTimeout(long timeout) {
98105
flushTimeout = timeout;
99106
}
100107

108+
public void setSendThreadPool(ExecutorService sendThreadPool) {
109+
this.sendThreadPool = sendThreadPool;
110+
}
111+
112+
public void setRetryThreadPool(ExecutorService retryThreadPool) {
113+
this.retryThreadPool = retryThreadPool;
114+
}
115+
101116
public void setCallbacks(AmplitudeCallbacks callbacks) {
102117
this.callbacks = callbacks;
103118
}
@@ -118,7 +133,7 @@ private CompletableFuture<Response> sendEvents(List<Event> events) {
118133
throw new CompletionException(e);
119134
}
120135
return response;
121-
});
136+
}, supplyAsyncPool);
122137
}
123138

124139
// Call this function if event not in current Retry list.

0 commit comments

Comments
 (0)