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.

Overall timeout versus timeout-per-try; overall timeout cancelling waits-between-retries

See original GitHub issue

Hi guys, I have a doubt regarding how to apply an overall timeout and a separate timeout for each individual try. Given the below code, I would like to confirm if I’m using in the right way the policies for an overall and individual timeout.

var overalTimeoutPolicy = new IAsyncPolicy[] {
   Policy.TimeoutAsync(30, TimeoutStrategy.Pessimistic),
   Policy.Handle<SqlException>()
                .WaitAndRetryAsync(
                    retryCount,
                    retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt))),
   Policy.Handle<SqlException>()
                .CircuitBreakerAsync(
                    exceptionsAllowedBeforeBreaking,
                    TimeSpan.FromMinutes(1))
};

var individualTimeoutPolicy = new IAsyncPolicy[] {
   Policy.Handle<SqlException>()
                .WaitAndRetryAsync(
                    retryCount,
                    retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt))),
   Policy.TimeoutAsync(30, TimeoutStrategy.Pessimistic),
   Policy.Handle<SqlException>()
                .CircuitBreakerAsync(
                    exceptionsAllowedBeforeBreaking,
                    TimeSpan.FromMinutes(1))
};

Policy.Wrap(overalTimeoutPolicy).ExecuteAsync(action);
Policy.Wrap(individualTimeoutPolicy ).ExecuteAsync(action);

I appreciate in advance for your help!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
reisenbergercommented, Jul 18, 2018

@vany0114 That looks good:

  • If you place the timeout outside the retry in the PolicyWrap (that is, declared first), then the timeout applies as an overall timeout.
  • If you place the timeout inside the retry in the PolicyWrap (that is, declared after the retry within the wrap), then the timeout applies per try.

You can also do both in the same PolicyWrap: apply an overall timeout and a timeout per try. There is no barrier to using a TimeoutPolicy twice in the same PolicyWrap.

We discuss this in more depth, and show the flow through a PolicyWrap of this type, in the diagram in this part of the PolicyWrap wiki

1reaction
reisenbergercommented, Jul 18, 2018

Yes. If waits-and-retries inside a TimeoutPolicy mean the overall execution would take longer than the timeout allows, the timeout will time out the execution.

Specifically for the wait phase: both TimeoutStrategy.Optimistic and TimeoutStrategy.Pessimistic have the power to cancel a WaitAndRetry policy during the wait-before-retrying phase.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Timeouts in online versus??? : r/EA_NHL
In two separate games now I've gone to call a timeout after taking a bad icing to refresh my lines energy. Both times...
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