Skip to content
This repository has been archived by the owner on Jul 30, 2023. It is now read-only.

Add mechanism of detect network disconnection and reconnect streaming #52

Open
MulticolorWorld opened this issue May 7, 2017 · 3 comments

Comments

@MulticolorWorld
Copy link
Contributor

No description provided.

@MulticolorWorld MulticolorWorld changed the title Add onException(e: Exception) to streaming handler for detecting network disconnection Add mechanism of detect network disconnection and reconnect streaming May 7, 2017
@MulticolorWorld
Copy link
Contributor Author

MulticolorWorld commented May 7, 2017

idea 1

Continue to reconnect streaming until called shutdown()

fun connectHoge(handler: Handler): Shutdownable {
    val dispatcher = Dispatcher()
    dispatcher.invokeLater(Runnable {
        while (true) {
            val response = client.get("hoge")
            if (response.isSuccessful) {
                val reader = response.body().byteStream().bufferedReader()
                try {
                    while (true) {
                        val line = reader.readLine()
                        .....
                    }
                } catch (e: IOException) {
                    //waiting reconnect time
                    continue
                }
            }
        }
    })
    return Shutdownable(dispatcher)
}

@sys1yagi
Copy link
Owner

sys1yagi commented May 7, 2017

I agree the idea. But I think that Streaming shouldn't have responsibility for automatic reconnection.
It would be better to provide a disconnect event and a reconnect function.

interface Retryable {
    fun retry()
}

interface Handler {
    // ...
    fun onDisconnected(retryable: Retryable)
}

It can delegate the reconnection strategy to the user.

@MulticolorWorld
Copy link
Contributor Author

OK. I try to write code according to this sample. Thank you!

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

No branches or pull requests

2 participants