Skip to content
A demonstration proactive custom Polly policy, to capture execution timings of delegates.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
.gitignore
CHANGELOG.md
CODE_OF_CONDUCT.md
GitVersionConfig.yaml
LICENSE.txt
README.md
appveyor.yml
build.bat
build.cake
build.ps1

README.md

Polly.Contrib.TimingPolicy

This repo contains a custom Polly policy to capture execution timings of delegates executed through the policy.

For more background on Polly see the main Polly repo.

Usage

Asynchronous executions

Define an Func<TimeSpan, Context, Task> timingPublisher to capture the execution duration as a TimeSpan. The Context input parameter allows filtering based on Polly's in-built execution metadata or context you pass in to the execution.

Configure the policy:

var timingPolicy = AsyncTimingPolicy.Create(timingPublisher);

Execute through the policy:

timingPolicy.ExecuteAsync(/* whatever */);

The policy can also be combined with other policies in a PolicyWrap - see below.

Synchronous executions

Define an Action<TimeSpan, Context> timingPublisher to capture the TimeSpan information.

Configure the policy:

var timingPolicy = TimingPolicy.Create(timingPublisher);

Execute through the policy:

timingPolicy.Execute(/* whatever */);

The policy can also be combined with other policies in a PolicyWrap.

Using TimingPolicy in PolicyWrap

The TimingPolicy can be used in any position in a PolicyWrap.

  • A TimingPolicy used outermost (configured first) in a PolicyWrap will capture overall execution-time of the PolicyWrap, including all retries or waits-for-bulkhead-execution-slot that the polices in the PolicyWrap may introduce.
  • A TimingPolicy used innermost (configured last) in a PolicyWrap will capture execution-timing solely for the underlying executed delegate.

Action versus event pattern versus IObservable<>

Action and Func were intentionally chosen to keep the code for the related blog post as simple as possible. You may wish to adapt the code to an event pattern or IObservable<> for production use.

Blog post example

The policy is an example for the blog [Custom policies Part II: Authoring a non-reactive custom policy](LINK WHEN PUBLISHED).

The policy in this repo differs in small ways from the blog post, as this repo offers TimingPolicy in all four combinations:

  • TimingPolicy (synchronous non-generic)
  • TimingPolicy<TResult> (synchronous generic)
  • AsyncTimingPolicy (asynchronous non-generic)
  • AsyncTimingPolicy<TResult> (asynchronous generic)

Interested in developing your own custom policies?

See our blog series:

And see the templates for developing custom policies: Polly.Contrib.CustomPolicyTemplates.

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.