Skip to content

Commit

Permalink
Add ability to customize timeout for Emitter (close #311)
Browse files Browse the repository at this point in the history
  • Loading branch information
antonkazakov authored and oguzhanunlu committed Jun 11, 2019
1 parent 687187e commit 2f89ace
Showing 1 changed file with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public class Emitter {
private int sendLimit;
private long byteLimitGet;
private long byteLimitPost;
private int emitTimeout;
private TimeUnit timeUnit;

private EventStore eventStore;
Expand All @@ -103,6 +104,7 @@ public static class EmitterBuilder {
int emptyLimit = 5; // Optional
long byteLimitGet = 40000; // Optional
long byteLimitPost = 40000; // Optional
private int emitTimeout = 5; // Optional
TimeUnit timeUnit = TimeUnit.SECONDS;
OkHttpClient client = null; //Optional

Expand Down Expand Up @@ -217,6 +219,16 @@ public EmitterBuilder byteLimitPost(long byteLimitPost) {
return this;
}

/**
* @param emitTimeout The maximum timeout for emitting events. If emit time exceeds this value
* TimeOutException will be thrown
* @return itself
*/
public EmitterBuilder emitTimeout(int emitTimeout){
this.emitTimeout = emitTimeout;
return this;
}

/**
* @param timeUnit a valid TimeUnit
* @return itself
Expand Down Expand Up @@ -264,6 +276,7 @@ private Emitter(EmitterBuilder builder) {
this.sendLimit = builder.sendLimit;
this.byteLimitGet = builder.byteLimitGet;
this.byteLimitPost = builder.byteLimitPost;
this.emitTimeout = builder.emitTimeout;
this.uri = builder.uri;
this.timeUnit = builder.timeUnit;
this.eventStore = null;
Expand Down Expand Up @@ -480,12 +493,12 @@ protected LinkedList<RequestResult> performAsyncEmit(LinkedList<ReadyRequest> re
Logger.d(TAG, "Request Futures: %s", futures.size());

// Get results of futures
// - Wait up to 5 seconds for the request
// - Wait up to emitTimeout seconds for the request
for (int i = 0; i < futures.size(); i++) {
int code = -1;

try {
code = (int) futures.get(i).get(5, TimeUnit.SECONDS);
code = (int) futures.get(i).get(emitTimeout, TimeUnit.SECONDS);
} catch (InterruptedException ie) {
Logger.e(TAG, "Request Future was interrupted: %s", ie.getMessage());
} catch (ExecutionException ee) {
Expand Down

0 comments on commit 2f89ace

Please sign in to comment.