HedgingController<T>
using Polly.Hedging.Controller;
using Polly.Telemetry;
using Polly.Utils;
using System;
using System.Runtime.CompilerServices;
using System.Threading;
namespace Polly.Hedging.Utils
{
    [System.Runtime.CompilerServices.NullableContext(1)]
    [System.Runtime.CompilerServices.Nullable(0)]
    internal sealed class HedgingController<[System.Runtime.CompilerServices.Nullable(2)] T>
    {
        private readonly ObjectPool<HedgingExecutionContext<T>> _contextPool;
        private readonly ObjectPool<TaskExecution<T>> _executionPool;
        private int _rentedContexts;
        private int _rentedExecutions;
        public int RentedContexts => _rentedContexts;
        public int RentedExecutions => _rentedExecutions;
        public HedgingController(ResilienceStrategyTelemetry telemetry, TimeProvider provider, HedgingHandler<T> handler, int maxAttempts)
        {
            CancellationTokenSourcePool pool = CancellationTokenSourcePool.Create(provider);
            _executionPool = new ObjectPool<TaskExecution<T>>(delegate {
                Interlocked.Increment(ref _rentedExecutions);
                return new TaskExecution<T>(handler, pool, provider, telemetry);
            }, delegate {
                Interlocked.Decrement(ref _rentedExecutions);
                return true;
            });
            _contextPool = new ObjectPool<HedgingExecutionContext<T>>(delegate {
                Interlocked.Increment(ref _rentedContexts);
                return new HedgingExecutionContext<T>(_executionPool, provider, maxAttempts, ReturnContext);
            }, delegate {
                Interlocked.Decrement(ref _rentedContexts);
                return true;
            });
        }
        public HedgingExecutionContext<T> GetContext(ResilienceContext context)
        {
            HedgingExecutionContext<T> hedgingExecutionContext = _contextPool.Get();
            hedgingExecutionContext.Initialize(context);
            return hedgingExecutionContext;
        }
        private void ReturnContext(HedgingExecutionContext<T> context)
        {
            _contextPool.Return(context);
        }
    }
}