Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 46
Connection resumption improvements#900
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
Merged
ikbalkaya
merged 5 commits into
main
from
bug/lib_not_resending_pending_messages_after_resumeJan 25, 2023
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1477848
Reattach channel on reconnection
ikbalkaya c6b777f
Make resume case explicit
ikbalkaya 570507c
Add messages to queues and remove explicit send call
ikbalkaya 9b8238d
Reset message serials
ikbalkaya 07ad898
Add tests for new pending message queue and improve transport interface
ikbalkaya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 69 additions & 29 deletions
98 lib/src/main/java/io/ably/lib/transport/ConnectionManager.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -81,6 +81,8 @@ public interface Channels { | ||
| void onMessage(ProtocolMessage msg); | ||
| void suspendAll(ErrorInfo error, boolean notifyStateChange); | ||
| Iterable<Channel> values(); | ||
| void reattachOnResumeFailure(); | ||
| } | ||
| /*********************************** | ||
| @@ -1183,44 +1185,48 @@ private void onChannelMessage(ProtocolMessage message) { | ||
| } | ||
| private synchronized void onConnected(ProtocolMessage message) { | ||
| /* if the returned connection id differs from | ||
| * the existing connection id, then this means | ||
| * we need to suspend all existing attachments to | ||
| * the old connection. | ||
| * If realtime did not reply with an error, it | ||
| * signifies that this was a result of an earlier | ||
| * connection being invalidated due to being stale. | ||
| * | ||
| * Suspend all channels attached to the previous id; | ||
| * this will be reattached in setConnection() */ | ||
| ErrorInfo error = message.error; | ||
| if(connection.id != null && !message.connectionId.equals(connection.id)) { | ||
| /* we need to suspend the original connection */ | ||
| if(error == null) { | ||
| error = REASON_SUSPENDED; | ||
| final ErrorInfo error = message.error; | ||
| connection.reason = error; | ||
| if (connection.id != null) { // there was a previous connection, so this is a resume and RTN15c applies | ||
| Log.d(TAG, "There was a connection resume"); | ||
| if(message.connectionId.equals(connection.id)) { | ||
| // resume succeeded | ||
| if(message.error == null) { | ||
| // RTN15c1: no action required wrt channel state | ||
| Log.d(TAG, "connection has reconnected and resumed successfully"); | ||
| } else { | ||
| // RTN15c2: no action required wrt channel state | ||
| Log.d(TAG, "connection resume success with non-fatal error: " + error.message); | ||
| } | ||
| // Add pending messages to the front of queued messages to be sent later | ||
| addPendingMessagesToQueuedMessages(false); | ||
| } else { | ||
| // RTN15c3: resume failed | ||
| if (error != null){ | ||
| Log.d(TAG, "connection resume failed with error: " + error.message); | ||
| }else { // This shouldn't happen but, putting it here for safety | ||
| Log.d(TAG, "connection resume failed without error" ); | ||
| } | ||
| channels.reattachOnResumeFailure(); | ||
| // Add any messages still pending from the previous transport (RTN19a) to the front of queued messages | ||
| // however, this time the pending messages have to have newly assigned ` | ||
| // msgSerial`s. They can't simply be replayed, as they are in the successful resume case | ||
| addPendingMessagesToQueuedMessages(true); | ||
| } | ||
| channels.suspendAll(error, false); | ||
| } | ||
| /* set the new connection id */ | ||
| ConnectionDetails connectionDetails = message.connectionDetails; | ||
| connection.key = connectionDetails.connectionKey; | ||
| if (!message.connectionId.equals(connection.id)) { | ||
| /* The connection id has changed. Reset the message serial and the | ||
| * pending message queue (which fails the messages currently in | ||
| * there). */ | ||
| pendingMessages.reset(msgSerial, | ||
| new ErrorInfo("Connection resume failed", 500, 50000)); | ||
| msgSerial = 0; | ||
| } | ||
| connection.id = message.connectionId; | ||
| if(message.connectionSerial != null) { | ||
| connection.serial = message.connectionSerial.longValue(); | ||
| connection.serial = message.connectionSerial; | ||
| if (connection.key != null) | ||
| connection.recoveryKey = connection.key + ":" + message.connectionSerial; | ||
| } | ||
| ConnectionDetails connectionDetails = message.connectionDetails; | ||
| /* Get any parameters from connectionDetails. */ | ||
| connection.key = connectionDetails.connectionKey; //RTN16d | ||
| maxIdleInterval = connectionDetails.maxIdleInterval; | ||
| connectionStateTtl = connectionDetails.connectionStateTtl; | ||
| @@ -1232,12 +1238,39 @@ private synchronized void onConnected(ProtocolMessage message) { | ||
| requestState(transport, new StateIndication(ConnectionState.failed, e.errorInfo)); | ||
| return; | ||
| } | ||
| /* indicated connected currentState */ | ||
| setSuspendTime(); | ||
| requestState(new StateIndication(ConnectionState.connected, error)); | ||
| } | ||
| /** | ||
| * Add all pending queued messages to the front of QueuedMessages for them to be sent later | ||
| * Spec: RTN19a | ||
| * @param resetMessageSerial whether to reset message serial, this will determine whether to reset message serials | ||
| * on pending queue, for example when a connection resume failed | ||
| */ | ||
| private void addPendingMessagesToQueuedMessages(boolean resetMessageSerial) { | ||
| // Add messages from pending messages to front of queuedMessages in order to retry them | ||
| queuedMessages.addAll(0, pendingMessages.queue); | ||
| //rewind start serial back to the first serial since we are clearing the queue | ||
| if (!pendingMessages.queue.isEmpty()){ | ||
| //Reset current serial to the first pending message on previous queue as we are going to clear the queue now | ||
| msgSerial = pendingMessages.queue.get(0).msg.msgSerial; | ||
| pendingMessages.resetStartSerial((int) (msgSerial)); | ||
| pendingMessages.clearQueue(); | ||
| } | ||
| //RTN19a | ||
| if (resetMessageSerial){ | ||
| pendingMessages.resetStartSerial(0); | ||
| msgSerial = 0; //msgSerial will increase in sendImpl when messages are sent | ||
| } | ||
| } | ||
| public List<QueuedMessage> getPendingMessages() { | ||
| return pendingMessages.queue; | ||
| } | ||
| private synchronized void onDisconnected(ProtocolMessage message) { | ||
| ErrorInfo reason = message.error; | ||
| if(reason != null && isTokenError(reason)) { | ||
| @@ -1718,6 +1751,13 @@ public synchronized void reset(long oldMsgSerial, ErrorInfo err) { | ||
| startSerial = 0; | ||
| } | ||
| public void resetStartSerial(int from) { | ||
paddybyers marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| startSerial = from; | ||
| } | ||
| synchronized void clearQueue() { | ||
| queue.clear(); | ||
| } | ||
| } | ||
| /*********************** | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 17 additions & 5 deletions
22 lib/src/main/java/io/ably/lib/transport/WebSocketTransport.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.