From 16c211c1d67d3ad84c7e50f54fe4e25437c558e2 Mon Sep 17 00:00:00 2001 From: jamesthacker Date: Thu, 29 Mar 2018 10:53:25 -0400 Subject: [PATCH 1/3] Add support for custom max backoff time --- .../launchdarkly/eventsource/EventSource.java | 27 +++++++++++++++++-- .../eventsource/EventSourceTest.java | 12 +++++++-- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/launchdarkly/eventsource/EventSource.java b/src/main/java/com/launchdarkly/eventsource/EventSource.java index e0892dd..a1c0b02 100644 --- a/src/main/java/com/launchdarkly/eventsource/EventSource.java +++ b/src/main/java/com/launchdarkly/eventsource/EventSource.java @@ -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; @@ -57,6 +57,7 @@ public class EventSource implements ConnectionHandler, Closeable { private final ExecutorService eventExecutor; private final ExecutorService streamExecutor; private volatile long reconnectTimeMs = 0; + private volatile long maxReconnectTimeMs; private volatile String lastEventId; private final EventHandler handler; private final ConnectionErrorHandler connectionErrorHandler; @@ -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"); @@ -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; } @@ -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; } @@ -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; @@ -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. * diff --git a/src/test/java/com/launchdarkly/eventsource/EventSourceTest.java b/src/test/java/com/launchdarkly/eventsource/EventSourceTest.java index 26d12e3..a6212da 100644 --- a/src/test/java/com/launchdarkly/eventsource/EventSourceTest.java +++ b/src/test/java/com/launchdarkly/eventsource/EventSourceTest.java @@ -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") From 4738ef89bf3bcc641ee6263dd0b4d44376bccb68 Mon Sep 17 00:00:00 2001 From: jamesthacker Date: Thu, 29 Mar 2018 14:27:42 -0400 Subject: [PATCH 2/3] Remove volatile keywords on reconnect times --- src/main/java/com/launchdarkly/eventsource/EventSource.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/launchdarkly/eventsource/EventSource.java b/src/main/java/com/launchdarkly/eventsource/EventSource.java index a1c0b02..b6b7377 100644 --- a/src/main/java/com/launchdarkly/eventsource/EventSource.java +++ b/src/main/java/com/launchdarkly/eventsource/EventSource.java @@ -56,8 +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 volatile long maxReconnectTimeMs; + private long reconnectTimeMs = 0; + private long maxReconnectTimeMs; private volatile String lastEventId; private final EventHandler handler; private final ConnectionErrorHandler connectionErrorHandler; From 2fd5f1c5859c0178ed0585c9eee4cd9595c1be8f Mon Sep 17 00:00:00 2001 From: Arun Bhalla Date: Wed, 4 Apr 2018 12:05:34 -0700 Subject: [PATCH 3/3] Prep for 1.8.0 release --- gradle.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle.properties b/gradle.properties index 7c3abcf..d4e390c 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,3 +1,3 @@ -version=1.7.1 +version=1.8.0 ossrhUsername= ossrhPassword=