0

I have the following Polly policies defined:

sharedBulkhead = Policy.Bulkhead(maxParallelizations, maxQueuingActions);
resilienceStrategy = Policy.Wrap(retryPolicy, circuitBreaker, sharedBulkhead);
policyWrap = fallbackForAnyException.Wrap(fallbackForCircuitBreaker.Wrap(resilienceStrategy));

I execute the policy like so:

public bool Notify(IGrouping<string, TModel> messages)
    {
        var endPoint = messages.Key;
        Task.Run(() =>
        {
            foreach (var message in messages)
            {
                policyWrap.Execute((context) => CallApi(endPoint), new Context(endPoint));
            }
        });

        return true;
    }

I want each call to Notify() to run on a new thread. So, my question is: Do I have to explicitly call Task.Run(() => for a new thread, or is that automatically run on a new thread by polly?

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
Clairvoyant
  • 81
  • 1
  • 9
  • Hi Nimish. We are going to need some background as your objective is not obvious from the question title or description. What problem are you trying to solve here? – Gusdor Dec 17 '18 at 18:16
  • I want to know if what I am doing is right? Do I have to execute the policy inside a thread created explicitly? – Clairvoyant Dec 17 '18 at 18:29
  • As api calls are I/O bound, I would not wrap them in `Task.Run`. You might prefer [the async version of Execute](https://github.com/App-vNext/Polly/wiki/Asynchronous-action-execution) – Crowcoder Dec 17 '18 at 18:45
  • If the call to be governed by the Policy (ie `CallApi(...)` in this case) itself is async, then the async variants of the policies should be used. – mountain traveller Dec 17 '18 at 20:36

1 Answers1

0

I want each call to Notify() to run on a new thread. So, my question is: [...] is that automatically run on a new thread by polly?

No. As the Polly bulkhead wiki page says:

The policy itself does not place calls onto threads; it assumes upstream systems have already placed calls into threads, but limits their parallelization of execution

There is no concept of task-scheduling (using a TaskScheduler) within Polly (except, for completeness, in the very specialised case of synchronous pessimistic TimeoutPolicy).

For completeness, however, all Polly policies are thread-safe.

mountain traveller
  • 7,591
  • 33
  • 38