rsocket-kotlin icon indicating copy to clipboard operation
rsocket-kotlin copied to clipboard

Get Connection State

Open mihai1voicescu opened this issue 3 years ago • 2 comments

It would be great if we could listen to the current connection state.

Motivation

Viewing the current connection state is important, especially when using the reconnect feature. This will enable a bunch of new use cases without the need to create a hacky workaround.

Example use case:

  • Client side: Displaying a disconnected message in a chat app
  • Server side: Marking the worker disconnected and not sending any more work to it.
  • Logging so we can debug the rest of the application

Desired solution

Add the possible states and expose them via a StateFlow.

I am open to do a PR if you would like, but I would like a list of possible states 😄 .

As an example:

  • Connecting - Connecting for the first time
  • Connected
  • Disconnected - Has disconnected
  • Reconnecting - Attempting to reconnect, will only be emitted for reconnect version
  • Failed - Closed with an error
  • Closed - Closed normally

mihai1voicescu avatar Jan 20 '22 16:01 mihai1voicescu

Hey, thanks for an idea!

As for Client side: We already have something similar internally to support reconnect feature. There are 3 states there: Connecting, Connected and Failed. Im all for providing some public API for it, but we also need to think about resume feature, that will come soon, and as so provide more info about state (f.e. Resuming, Resume Failed and may be something else).

As for Server side: On current moment, with reconnect feature only, server can know only that client disconnected, this is already there, you can call ConnectionAcceptorContext.requester.coroutineContext.job.invokeOnCompletion { callback }. But with resume this can be improved, so server will know, that client may resume in near future.

I like the proposal, but on current moment I'm in progress with polishing (and breaking :) ) Public API and implementing new features like resume and lease, so I'm not sure, that it's not the best time to add such a feature.

But! For Client side, this can be implemented externally for now. You can look at https://github.com/rsocket/rsocket-kotlin/blob/master/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/internal/ReconnectableRSocket.kt. This file have full logic for reconnect implementation, and if you need state preserved, you can just copy it and change return type from RSocket to ReconnectableRSocket with exposed state as some sealed class! Don't forget then not to use out-of-the-box reconnect feature.

olme04 avatar Jan 21 '22 06:01 olme04

For the moment I went for another route on the client, since I need just Connected and Disconnected and use the retry feature I just created a StateFlow(Disconnected) and:

  • Each time the Acceptor handler finishes state goes to Connected
  • Each time the shouldRetry callback is called state goes to Disconnected

mihai1voicescu avatar Jan 21 '22 11:01 mihai1voicescu