Does Polly Bulkhead policy itself place calls onto threads?

527 views Asked by At

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?

1

There are 1 answers

0
mountain traveller On

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.