sys1yagi / mastodon4j

mastodon client for java, kotlin https://github.com/tootsuite/documentation/blob/master/Using-the-API/API.md
MIT License
135 stars 28 forks source link

Add mechanism of detect network disconnection and reconnect streaming #52

Open MulticolorWorld opened 7 years ago

MulticolorWorld commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago

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