kotlinx.coroutines icon indicating copy to clipboard operation
kotlinx.coroutines copied to clipboard

Correct & document odd Flow retry/retryWhen attempts behavior

Open bubenheimer opened this issue 4 years ago • 4 comments
trafficstars

The Flow retry/retryWhen attempt counter never resets; it provides a kind of flow flakiness measure rather than the number of retry attempts after the last failure. This behavior is not clearly documented. I assume the behavior is an oversight, and not really by design; having such a measure seems rarely preferable. Typically, when counting retries, for example in network connectivity, the counter resets after a successful connection. Measuring flakiness would commonly be a secondary concern, perhaps as a kind of probabilistic/fuzzy measure, or for analytics.

I propose changing the behavior to the reset style, or adding a flag or new operator implementing this behavior. In addition, the behavior needs to be documented.

My present use cases for this are tied to network connectivity and interprocess communication.

bubenheimer avatar Jul 25 '21 14:07 bubenheimer

I forgot to mention that the obscurity of the present behavior is compounded by not being able to easily navigate to the source of these specific methods in the IDE (which I believe is due to current archive format limitations).

bubenheimer avatar Jul 25 '21 14:07 bubenheimer

A resetting retry count is essential also for implementing backoff (linear or exponential).

bubenheimer avatar Jul 25 '21 17:07 bubenheimer

This version of retry operator is far cry from the implementation of a full-blown backoff strategy. We've discussed designing providing one, but have decided that it is too domain-specific and is much better suited to be implemented in a separate library. The current version of retry operator is a mere replacement the corresponding Rx operators.

In light of the problem highlighted in this issue, we can provide a more generic version that does not have a built-in retries counter but will make it possible to keep any flow-associated state (such as retry count, last retry time, etc) to make complex retry/fail decisions. The existing retry implementations can be deprecated with a suggested replacement to more generic ones, thus, effectively, serving as an example of how you could implement a rudimentary retry counting.

elizarov avatar Jul 26 '21 07:07 elizarov

Thanks for the elaboration, makes sense!

bubenheimer avatar Jul 26 '21 20:07 bubenheimer