Skip to content

Commit

Permalink
feat: throw exception when trying to attach on released channel
Browse files Browse the repository at this point in the history
  • Loading branch information
ttypic committed Nov 22, 2023
1 parent 78a7137 commit 17b883e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/src/main/java/io/ably/lib/realtime/AblyRealtime.java
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ public Channel get(final String channelName, final ChannelOptions channelOptions
public void release(String channelName) {
Channel channel = map.remove(channelName);
if(channel != null) {
channel.markAsReleased();
try {
channel.detach();
} catch (AblyException e) {
Expand Down
22 changes: 21 additions & 1 deletion lib/src/main/java/io/ably/lib/realtime/ChannelBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ public abstract class ChannelBase extends EventEmitter<ChannelEvent, ChannelStat

private int retryCount = 0;

/**
* @see #markAsReleased()
*/
private boolean released = false;



/***
* internal
*
Expand Down Expand Up @@ -271,6 +278,13 @@ public void detach() throws AblyException {
detach(null);
}

/**
* Mark channel as released that means we can't perform any operation on this channel anymore
*/
public synchronized void markAsReleased() {
released = true;
}

/**
* Detach from this channel.
* Any resulting channel state change is emitted to any listeners registered using the
Expand Down Expand Up @@ -432,6 +446,7 @@ private void attachWithTimeout(final CompletionListener listener) throws AblyExc
* set up timer to reattach it later
*/
synchronized private void attachWithTimeout(final boolean forceReattach, final CompletionListener listener) {
checkChannelIsNotReleased();
Timer currentAttachTimer;
try {
currentAttachTimer = new Timer();
Expand Down Expand Up @@ -487,6 +502,10 @@ public void run() {
}, Defaults.realtimeRequestTimeout);
}

private void checkChannelIsNotReleased() {
if (released) throw new IllegalStateException("Can't perform any operation on released channel");
}

/**
* Must be called in suspended state. Wait for timeout specified in clientOptions, and then
* try to attach the channel
Expand Down Expand Up @@ -539,13 +558,14 @@ synchronized private void detachWithTimeout(final CompletionListener listener) {
callCompletionListenerError(listener, ErrorInfo.fromThrowable(t));
return;
}
attachTimer = currentDetachTimer;
attachTimer = released ? null : currentDetachTimer;

try {
detachImpl(new CompletionListener() {
@Override
public void onSuccess() {
clearAttachTimers();
if (released) setDetached(null);
callCompletionListenerSuccess(listener);
}

Expand Down

0 comments on commit 17b883e

Please sign in to comment.