recoverWith and switch
See original GitHub issueHey there!
I’ve encountered a problem, where I’m not sure if it’s my code or a bug. I have created a helper function called retry
, which takes a stream and returns the same stream altered, so that it automatically retries on errors with an exponential backoff.
This is the helper function:
retry
export function retry(createOriginalStream, { delay = 1000, exponentialDelay } = {}) {
let currentDelay = exponentialDelay || delay;
const $original = createOriginalStream().multicast();
$original
.take(1)
.recoverWith(() => most.empty())
.forEach(() => {
currentDelay = delay;
});
return $original
.recoverWith(error =>
most.just()
.delay(currentDelay)
.map(() =>
retry(createOriginalStream, { delay, exponentialDelay: currentDelay * 2 })
)
.switch()
);
}
This is how it’s used:
failure case
most.combine((someValue, someBoolean) =>
someBoolean
? retry(() => createStream(someValue))
: most.empty()
, someValueStream, someBooleanStream)
.switch()
If someBoolean
changes from true
to false
and the stream is failing, it keeps retrying although there shouldn’t be any listeners. Each time someBoolean
switches another failing stream is added trying to recover.
I’m not sure if I’m doing something wrong or if switch
fails to unsubscribe from the new recovered streams returned by recoverWith
.
Any help is appreciated. 😃
Issue Analytics
- State:
- Created 7 years ago
- Comments:14 (8 by maintainers)
Top Results From Across the Web
recoverWith - Documentation - Akka
Allow switching to alternative Source when a failure has happened upstream. Throwing an exception inside recoverWith will be logged on ERROR level automatically ......
Read more >scala - Convert Try to Future and recoverWith as Future
Futures are immutable. Recovering resF does not change resF , it creates a new Future . – Michael Zajac. Sep 20, ...
Read more >Map a Future for both Success and Failure in Scala - Baeldung
So, there was no way to turn a failure into a value different from an ... Then, we can call the recoverWith method,...
Read more >Future Type Operation with Scala - Knoldus Blogs
In this article, we explored Scala Future type operations like map, flatmap , zip , zipwith and also error handling operations like recover....
Read more >A practical guide to error handling in Scala Cats and Cats Effect
recoverWith. The “F” counterpart of recover, a value returned by the mapping function has to be wrapped into F (can be a failure...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Not sure this helps, but I asked about a retry function a “long” time ago, and this is what it looks like
const retry = (n, stream) => stream.recoverWith(e => n === 0 ? most.throwError(e) : retry(n - 1, stream));
I had to dig it up the gitter history, now I saved it as a gist for a quick lookup.
Seems to do the job.
@maxhoffmann Closing this, but please reopen if you need.
@Sinewyk Thanks for posting that retry. I added it to the Recipes wiki.