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

Close ReactiveSocket on DuplexConnection close. #128

Closed
NiteshKant opened this issue Jul 6, 2016 · 5 comments
Closed

Close ReactiveSocket on DuplexConnection close. #128

NiteshKant opened this issue Jul 6, 2016 · 5 comments

Comments

@NiteshKant
Copy link
Contributor

Problem

Today, ReactiveSocket does not know when the underlying DuplexConnection is closed. This has two problems:

  • The underlying connection close is only discovered on writing a KeepAlive frame or writing/reading other frames. Although this "works" but is not ideal as that means we have to wait till the KeepAlive duration or next write to know about something that is easily available from the transport layer.
  • There is currently no way of notifying a user when a ReactiveSocket closes. onShutdown() only handles explicit shutdown() calls.

Proposal

We can add two methods to ReactiveSocket, viz.,

    /**
     * Close this {@code ReactiveSocket} upon subscribing to the returned {@code Publisher}
     *
     * <em>This method is idempotent and hence can be called as many times at any point with same outcome.</em>
     *
     * @return A {@code Publisher} that completes when this {@code ReactiveSocket} close is complete.
     */
    Publisher<Void> close();

and

    /**
     * Returns a {@code Publisher} that completes when this {@code ReactiveSocket} is closed. A {@code ReactiveSocket}
     * can be closed by explicitly calling {@link #close()} or when the underlying transport connection is closed.
     *
     * @return A {@code Publisher} that completes when this {@code ReactiveSocket} close is complete.
     */
    Publisher<Void> closeNotifier();

The above can replace the following methods that currently exist:

void onShutdown(Completable c);
void shutdown();
void close() throws Exception;

Same methods will be added to DuplexConnection thus wiring close() together and providing correct notifications to users when the ReactiveSocket is closed.

We can modify onShutdown() and shutdown() methods to the signature of my proposal but I think close() is more inline with other implementations of connections found elsewhere.

Current close() method is inherited from AutoCloseable which signifies synchronous execution as it return void.

@NiteshKant NiteshKant added this to the 0.2.2 milestone Jul 6, 2016
@NiteshKant NiteshKant self-assigned this Jul 6, 2016
@yschimke
Copy link
Member

yschimke commented Jul 6, 2016

It seems a bit of a shame to lose AutoCloseable. it simplifies client usage, where often the synchronous close is useful with try-with-resources blocks.

@NiteshKant
Copy link
Contributor Author

@yschimke I would like to hear more of the cases where try-with-resources is useful for real world applications. I would always end up doing Observable.concatWith(socket.close()) (in RxJava world ofcourse) as one can't really synchronously close the socket, it is always at the completion of an asynchronous call.

Having said that, I think we can always provide a utility method elsewhere which converts asynchronous close() to synchronous AutoClose implementation.

NiteshKant added a commit to NiteshKant/reactivesocket-java that referenced this issue Jul 11, 2016
As described in the issue, currently `ReactiveSocket` does not listen to `DuplexConnection` closures.

As suggested in the issue, removed `onShutdown()` and `shutdown()` methods, in favor of `close()` and `closeNotifier()` methods.
Added `close()` and `closeNotifier()` methods in `DuplexConnection`
@robertroeser
Copy link
Member

technically this wouldn't close until someone request(n) the close - so that could be a little weird.

why not use a CompletableFuture?

NiteshKant added a commit to NiteshKant/reactivesocket-java that referenced this issue Jul 12, 2016
As described in the issue, currently `ReactiveSocket` does not listen to `DuplexConnection` closures.

As suggested in the issue, removed `onShutdown()` and `shutdown()` methods, in favor of `close()` and `closeNotifier()` methods.
Added `close()` and `closeNotifier()` methods in `DuplexConnection`
stevegury pushed a commit that referenced this issue Jul 12, 2016
* Fixes issue #128 (Close on `DuplexConnection`)

As described in the issue, currently `ReactiveSocket` does not listen to `DuplexConnection` closures.

As suggested in the issue, removed `onShutdown()` and `shutdown()` methods, in favor of `close()` and `onClose()` methods.
Added `close()` and `onClose()` methods in `DuplexConnection`
@yschimke
Copy link
Member

@NiteshKant I agree but would probably differentiate between expert users, and normal users.

I think making the simplest most Java idiomatic thing work correctly (but not optimally) is a cost worth paying. e.g. the clarity of the sample code that will be cut and paste everywhere.

It's a lot easier for users to go from correct+simpler -> correct+optimal once they care enough.

@NiteshKant
Copy link
Contributor Author

0.5.x has close() and onClose() methods on ReactiveSocket. Closing this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants