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?
No. As the Polly bulkhead wiki page says:
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.