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

Release 1.8.0 #27

Merged
merged 4 commits into from
Apr 4, 2018
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
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
version=1.7.1
version=1.8.0
ossrhUsername=
ossrhPassword=
29 changes: 26 additions & 3 deletions src/main/java/com/launchdarkly/eventsource/EventSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class EventSource implements ConnectionHandler, Closeable {
private final Logger logger;

private static final long DEFAULT_RECONNECT_TIME_MS = 1000;
static final long MAX_RECONNECT_TIME_MS = 30000;
static final long DEFAULT_MAX_RECONNECT_TIME_MS = 30000;
static final int DEFAULT_CONNECT_TIMEOUT_MS = 10000;
static final int DEFAULT_WRITE_TIMEOUT_MS = 5000;
static final int DEFAULT_READ_TIMEOUT_MS = 1000 * 60 * 5;
Expand All @@ -56,7 +56,8 @@ public class EventSource implements ConnectionHandler, Closeable {
@Nullable private final RequestBody body;
private final ExecutorService eventExecutor;
private final ExecutorService streamExecutor;
private volatile long reconnectTimeMs = 0;
private long reconnectTimeMs = 0;
private long maxReconnectTimeMs;
private volatile String lastEventId;
private final EventHandler handler;
private final ConnectionErrorHandler connectionErrorHandler;
Expand All @@ -75,6 +76,7 @@ public class EventSource implements ConnectionHandler, Closeable {
this.method = builder.method;
this.body = builder.body;
this.reconnectTimeMs = builder.reconnectTimeMs;
this.maxReconnectTimeMs = builder.maxReconnectTimeMs;
ThreadFactory eventsThreadFactory = createThreadFactory("okhttp-eventsource-events");
this.eventExecutor = Executors.newSingleThreadExecutor(eventsThreadFactory);
ThreadFactory streamThreadFactory = createThreadFactory("okhttp-eventsource-stream");
Expand Down Expand Up @@ -289,7 +291,7 @@ private void maybeWaitWithBackoff(int reconnectAttempts) {
}

long backoffWithJitter(int reconnectAttempts) {
long jitterVal = Math.min(MAX_RECONNECT_TIME_MS, reconnectTimeMs * pow2(reconnectAttempts));
long jitterVal = Math.min(maxReconnectTimeMs, reconnectTimeMs * pow2(reconnectAttempts));
return jitterVal / 2 + nextLong(jitter, jitterVal) / 2;
}

Expand Down Expand Up @@ -333,6 +335,14 @@ public void setReconnectionTimeMs(long reconnectionTimeMs) {
this.reconnectTimeMs = reconnectionTimeMs;
}

public void setMaxReconnectTimeMs(long maxReconnectTimeMs) {
this.maxReconnectTimeMs = maxReconnectTimeMs;
}

public long getMaxReconnectTimeMs() {
return this.maxReconnectTimeMs;
}

public void setLastEventId(String lastEventId) {
this.lastEventId = lastEventId;
}
Expand All @@ -348,6 +358,7 @@ public void setUri(URI uri) {
public static final class Builder {
private String name = "";
private long reconnectTimeMs = DEFAULT_RECONNECT_TIME_MS;
private long maxReconnectTimeMs = DEFAULT_MAX_RECONNECT_TIME_MS;
private final URI uri;
private final EventHandler handler;
private ConnectionErrorHandler connectionErrorHandler = ConnectionErrorHandler.DEFAULT;
Expand Down Expand Up @@ -419,6 +430,18 @@ public Builder reconnectTimeMs(long reconnectTimeMs) {
return this;
}

/**
* Set the max reconnect time for the EventSource connection in milliseconds. The exponential backoff computed
* for reconnect attempts will not be larger than this value. Defaults to 30000 ms (30 seconds).
*
* @param maxReconnectTimeMs the maximum reconnect base time in milliseconds
* @return the builder
*/
public Builder maxReconnectTimeMs(long maxReconnectTimeMs) {
this.maxReconnectTimeMs = maxReconnectTimeMs;
return this;
}

/**
* Set the headers to be sent when establishing the EventSource connection.
*
Expand Down
12 changes: 10 additions & 2 deletions src/test/java/com/launchdarkly/eventsource/EventSourceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,17 @@ public void setUp() {
}

@Test
public void respectsMaximumBackoffTime() {
public void respectsDefaultMaximumBackoffTime() {
eventSource.setReconnectionTimeMs(2000);
Assert.assertTrue(eventSource.backoffWithJitter(300) < EventSource.MAX_RECONNECT_TIME_MS);
assertEquals(EventSource.DEFAULT_MAX_RECONNECT_TIME_MS, eventSource.getMaxReconnectTimeMs());
Assert.assertTrue(eventSource.backoffWithJitter(300) < eventSource.getMaxReconnectTimeMs());
}

@Test
public void respectsCustomMaximumBackoffTime() {
eventSource.setReconnectionTimeMs(2000);
eventSource.setMaxReconnectTimeMs(5000);
Assert.assertTrue(eventSource.backoffWithJitter(300) < eventSource.getMaxReconnectTimeMs());
}

@Ignore("Useful for inspecting jitter values empirically")
Expand Down