question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

recoverWith and switch

See original GitHub issue

Hey 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:closed
  • Created 7 years ago
  • Comments:14 (8 by maintainers)

github_iconTop GitHub Comments

4reactions
Sinewykcommented, Mar 21, 2017

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.

0reactions
briancavaliercommented, Aug 13, 2017

@maxhoffmann Closing this, but please reopen if you need.

@Sinewyk Thanks for posting that retry. I added it to the Recipes wiki.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found