|
1 | 1 | package io.split.engine.sse.client; |
2 | 2 |
|
3 | 3 | import com.google.common.base.Strings; |
4 | | -import io.split.engine.sse.EventSourceClient; |
| 4 | +import io.split.engine.sse.exceptions.EventParsingException; |
5 | 5 | import org.apache.http.client.config.RequestConfig; |
6 | 6 | import org.apache.http.client.methods.CloseableHttpResponse; |
7 | 7 | import org.apache.http.client.methods.HttpGet; |
@@ -32,7 +32,7 @@ public enum StatusMessage { |
32 | 32 | CONNECTED, |
33 | 33 | RETRYABLE_ERROR, |
34 | 34 | NONRETRYABLE_ERROR, |
35 | | - DISCONNECTED |
| 35 | + INITIALIZATION_IN_PROGRESS |
36 | 36 | } |
37 | 37 |
|
38 | 38 | private enum ConnectionState { |
@@ -63,6 +63,8 @@ public synchronized boolean open(URI uri) { |
63 | 63 | return false; |
64 | 64 | } |
65 | 65 |
|
| 66 | + _statusCallback.apply(StatusMessage.INITIALIZATION_IN_PROGRESS); |
| 67 | + |
66 | 68 | CountDownLatch signal = new CountDownLatch(1); |
67 | 69 | Thread thread = new Thread(() -> connectAndLoop(uri, signal)); |
68 | 70 | thread.setDaemon(true); |
@@ -109,29 +111,31 @@ private void connectAndLoop(URI uri, CountDownLatch signal) { |
109 | 111 | handleMessage(readMessageAsString(reader)); |
110 | 112 | } catch (EOFException exc) { |
111 | 113 | // This is when ably closes the connection on their end. IE: an invalid or expired token. |
112 | | - // Evaluate if we should send the DISCONNECTED event or not. |
113 | | - _statusCallback.apply(StatusMessage.DISCONNECTED); |
| 114 | + _statusCallback.apply(StatusMessage.RETRYABLE_ERROR); |
114 | 115 | return; |
115 | 116 | } catch (SocketTimeoutException exc) { // KeepAlive expired |
116 | 117 | _statusCallback.apply(StatusMessage.RETRYABLE_ERROR); |
117 | 118 | } catch (SocketException exc) { // Connection closed by us |
118 | 119 | if ("Socket closed".equals(exc.getMessage())) { |
119 | | - _statusCallback.apply(StatusMessage.DISCONNECTED); |
| 120 | + _statusCallback.apply(StatusMessage.NONRETRYABLE_ERROR); |
120 | 121 | return; |
121 | 122 | } |
| 123 | + |
122 | 124 | throw exc; // If it's not a socket closed (caused by us), rethrow the exception |
123 | 125 | } |
124 | 126 | } |
125 | | - } catch (IOException e) { |
126 | | - _log.warn(e.getMessage()); |
| 127 | + } catch (Exception e) { |
| 128 | + _log.error(e.getMessage()); |
127 | 129 | _statusCallback.apply(StatusMessage.NONRETRYABLE_ERROR); |
128 | | - } finally { |
| 130 | + } |
| 131 | + finally { |
129 | 132 | try { |
130 | 133 | _ongoingResponse.get().close(); |
131 | 134 | } catch (IOException e) { |
132 | 135 | _log.warn(e.getMessage()); |
133 | 136 | } |
134 | 137 |
|
| 138 | + _state.set(ConnectionState.CLOSED); |
135 | 139 | _log.warn("SSEClient finished."); |
136 | 140 | } |
137 | 141 | } |
|
0 commit comments