Rx back off and retry

后端 未结 2 2036
眼角桃花
眼角桃花 2020-12-16 19:55

This is based on the code presented in this SO : Write an Rx "RetryAfter" extension method

I am using the code by Markus Olsson (evaluation only at the mom

2条回答
  •  南方客
    南方客 (楼主)
    2020-12-16 20:13

    It's because you are putting a Delay on the result stream. (The value for n passed to ExponentialBackoff on the second iteration is 1, giving a delay of 1 second.)

    Delay operates on source, but the source proceeds as normal. Delay schedules the results it receives to be emitted after the specified duration. So the subscriber is getting the results after the logic of Generate has run to completion.

    If you think about it this is how Delay must be - otherwise Delay would be able to somehow interfere with upstream operators!

    It is possible to interfere with upstream operators (without throwing exceptions), by being a slow consumer. But that would certainly be a very bad way for a simple Delay to behave.

    I don't think the Delay is what you intend here - because Delay doesn't delay it's subscription. If you use DelaySubscription instead, you'll get what you're after I think. This is what's used in the linked question too.

    Your question provides a great illustration of the difference between Delay and DelaySubscription! It's worth thinking about Defer in here too.

    The distinction between these three is subtle but significant, so let's summarize all three:

    • Delay - Calls target operator immediately to get an IObservable, on its Subscribe calls Subscribe on target immediately, schedules events for delivery after specified delay on the specified Scheduler.

    • DelaySubscription - Calls target operator immediately to get an IObservable. On its Subscribe schedules Subscribe on target for execution after specified delay on the specified Scheduler.

    • Defer - Has no target operator. On Subscribe runs provided factory function to get target IObservable and immediately calls Subscribe. There's no delay added, hence no Scheduler to specify.

提交回复
热议问题