Share via


SqlClient의 구성 가능한 다시 시도 논리 코어 API

적용 대상: .NET Framework .NET .NET Standard

ADO.NET 다운로드

기본 제공 다시 시도 논리 공급자가 요구 사항을 충족하지 않는 경우, 자체 사용자 지정 공급자를 만들 수 있습니다. 그런 다음 사용자 지정 논리를 적용하기 위해 해당 공급자를 SqlConnection 또는 SqlCommand 개체에 할당할 수 있습니다.

기본 제공 공급자는 사용자 지정 공급자를 구현하는 데 사용될 수 있는 세 가지 인터페이스를 중심으로 설계되었습니다. 사용자 지정 다시 시도 공급자는 SqlConnection 또는 SqlCommand에서 내부 다시 시도 공급자와 동일한 방식으로 사용할 수 있습니다.

  1. SqlRetryIntervalBaseEnumerator: 시간 간격 시퀀스를 생성합니다.
  2. SqlRetryLogicBase: 다시 시도 횟수를 초과하지 않고 일시적인 조건이 충족되는 경우 지정된 열거자에 대한 다음 시간 간격을 검색합니다.
  3. SqlRetryLogicBaseProvider: 연결 및 명령 작업에 다시 시도 논리를 적용합니다.

주의

사용자 지정 다시 시도 논리 공급자를 구현하면 동시성, 성능 및 예외 관리를 비롯한 모든 측면을 담당하게 됩니다.

예시

이 샘플의 구현은 단계별 사용자 지정을 보여 줄 수 있도록 최대한 간단합니다. 스레드 보안, 비동기, 동시성 같은 고급 관행은 포함되지 않습니다. 실제 구현에 대한 심층 분석을 위해 Microsoft.Data.SqlClient GitHub 리포지토리에서 미리 정의된 다시 시도 논리를 연구할 수 있습니다.

  1. 사용자 지정 구성 가능한 다시 시도 논리 클래스를 정의합니다.

    • 열거자: 시간 간격의 고정 시퀀스를 정의하고 허용되는 시간 범위를 2분에서 4분으로 확장합니다.
    public class CustomEnumerator : SqlRetryIntervalBaseEnumerator
    {
        // Set the maximum acceptable time to 4 minutes
        private readonly TimeSpan _maxValue = TimeSpan.FromMinutes(4);
    
        public CustomEnumerator(TimeSpan timeInterval, TimeSpan maxTime, TimeSpan minTime)
            : base(timeInterval, maxTime, minTime) {}
    
        // Return fixed time on each request
        protected override TimeSpan GetNextInterval()
        {
            return GapTimeInterval;
        }
    
        // Override the validate method with the new time range validation
        protected override void Validate(TimeSpan timeInterval, TimeSpan maxTimeInterval, TimeSpan minTimeInterval)
        {
            if (minTimeInterval < TimeSpan.Zero || minTimeInterval > _maxValue)
            {
                throw new ArgumentOutOfRangeException(nameof(minTimeInterval));
            }
    
            if (maxTimeInterval < TimeSpan.Zero || maxTimeInterval > _maxValue)
            {
                throw new ArgumentOutOfRangeException(nameof(maxTimeInterval));
            }
    
            if (timeInterval < TimeSpan.Zero || timeInterval > _maxValue)
            {
                throw new ArgumentOutOfRangeException(nameof(timeInterval));
            }
    
            if (maxTimeInterval < minTimeInterval)
            {
                throw new ArgumentOutOfRangeException(nameof(minTimeInterval));
            }
        }
    }
    
    • 다시 시도 논리: 활성 트랜잭션의 일부가 아닌 명령에 대해 다시 시도 논리를 구현합니다. 다시 시도 횟수를 60회에서 20회로 줄입니다.
    public class CustomRetryLogic : SqlRetryLogicBase
    {
        // Maximum number of attempts
        private const int maxAttempts = 20;
    
        public CustomRetryLogic(int numberOfTries,
                                 SqlRetryIntervalBaseEnumerator enumerator,
                                 Predicate<Exception> transientPredicate)
        {
            if (!(numberOfTries > 0 && numberOfTries <= maxAttempts))
            {
                // 'numberOfTries' should be between 1 and 20.
                throw new ArgumentOutOfRangeException(nameof(numberOfTries));
            }
    
            // Assign parameters to the relevant properties
            NumberOfTries = numberOfTries;
            RetryIntervalEnumerator = enumerator;
            TransientPredicate = transientPredicate;
            Current = 0;
        }
    
        // Prepare this object for the next round
        public override void Reset()
        {
            Current = 0;
            RetryIntervalEnumerator.Reset();
        }
    
        public override bool TryNextInterval(out TimeSpan intervalTime)
        {
            intervalTime = TimeSpan.Zero;
            // First try has occurred before starting the retry process. 
            // Check if retry is still allowed
            bool result = Current < NumberOfTries - 1;
    
            if (result)
            {
                // Increase the number of attempts
                Current++;
                // It's okay if the RetryIntervalEnumerator gets to the last value before we've reached our maximum number of attempts.
                // MoveNext() will simply leave the enumerator on the final interval value and we will repeat that for the final attempts.
                RetryIntervalEnumerator.MoveNext();
                // Receive the current time from enumerator
                intervalTime = RetryIntervalEnumerator.Current;
            }
            return result;
        }
    }
    
    • 공급자: Retrying 이벤트 없이 동기 작업을 다시 시도하는 다시 시도 공급자를 구현합니다. 기존의 SqlException 일시적인 예외 오류 번호에 TimeoutException을 추가합니다.
    public class CustomProvider : SqlRetryLogicBaseProvider
    {
        // Preserve the given retryLogic on creation
        public CustomProvider(SqlRetryLogicBase retryLogic)
        {
            RetryLogic = retryLogic;
        }
    
        public override TResult Execute<TResult>(object sender, Func<TResult> function)
        {
            // Create a list to save transient exceptions to report later if necessary
            IList<Exception> exceptions = new List<Exception>();
            // Prepare it before reusing
            RetryLogic.Reset();
            // Create an infinite loop to attempt the defined maximum number of tries
            do
            {
                try
                {
                    // Try to invoke the function
                    return function.Invoke();
                }
                // Catch any type of exception for further investigation
                catch (Exception e)
                {
                    // Ask the RetryLogic object if this exception is a transient error
                    if (RetryLogic.TransientPredicate(e))
                    {
                        // Add the exception to the list of exceptions we've retried on
                        exceptions.Add(e);
                        // Ask the RetryLogic for the next delay time before the next attempt to run the function
                        if (RetryLogic.TryNextInterval(out TimeSpan gapTime))
                        {
                            Console.WriteLine($"Wait for {gapTime} before next try");
                            // Wait before next attempt
                            Thread.Sleep(gapTime);
                        }
                        else
                        {
                            // Number of attempts has exceeded the maximum number of tries
                            throw new AggregateException("The number of retries has exceeded the maximum number of attempts.", exceptions);
                        }
                    }
                    else
                    {
                        // If the exception wasn't a transient failure throw the original exception
                        throw;
                    }
                }
            } while (true);
        }
    
        public override Task<TResult> ExecuteAsync<TResult>(object sender, Func<Task<TResult>> function, CancellationToken cancellationToken = default)
        {
            throw new NotImplementedException();
        }
    
        public override Task ExecuteAsync(object sender, Func<Task> function, CancellationToken cancellationToken = default)
        {
            throw new NotImplementedException();
        }
    }
    
  2. 정의된 사용자 지정 형식으로 구성된 다시 시도 공급자 인스턴스를 만듭니다.

    public static SqlRetryLogicBaseProvider CreateCustomProvider(SqlRetryLogicOption options)
    {
        // 1. create an enumerator instance
        CustomEnumerator customEnumerator = new CustomEnumerator(options.DeltaTime, options.MaxTimeInterval, options.MinTimeInterval);
        // 2. Use the enumerator object to create a new RetryLogic instance
        CustomRetryLogic customRetryLogic = new CustomRetryLogic(5, customEnumerator, (e) => TransientErrorsCondition(e, options.TransientErrors));
        // 3. Create a provider using the RetryLogic object
        CustomProvider customProvider = new CustomProvider(customRetryLogic);
        return customProvider;
    }
    
    • 다음 함수는 주어진 다시 시도 가능한 예외 목록과 특수 TimeoutException 예외 목록을 사용하여 예외를 평가함으로써 다시 시도 가능 여부를 확인합니다.
    // Return true if the exception is a transient fault.
    private static bool TransientErrorsCondition(Exception e, IEnumerable<int> retriableConditions)
    {
        bool result = false;
    
        // Assess only SqlExceptions
        if (retriableConditions != null && e is SqlException ex)
        {
            foreach (SqlError item in ex.Errors)
            {
                // Check each error number to see if it is a retriable error number
                if (retriableConditions.Contains(item.Number))
                {
                    result = true;
                    break;
                }
            }
        }
        // Other types of exceptions can also be assessed
        else if (e is TimeoutException)
        {
            result = true;
        }
        return result;
    }
    
  3. 사용자 지정된 다시 시도 논리 사용:

    • 다시 시도 논리 매개 변수 정의:
    // Define the retry logic parameters
    var options = new SqlRetryLogicOption()
    {
        // Tries 5 times before throwing an exception
        NumberOfTries = 5,
        // Preferred gap time to delay before retry
        DeltaTime = TimeSpan.FromSeconds(1),
        // Maximum gap time for each delay time before retry
        MaxTimeInterval = TimeSpan.FromSeconds(20),
        // SqlException retriable error numbers
        TransientErrors = new int[] { 4060, 1024, 1025}
    };
    
    • 사용자 지정 공급자 만들기:
    // Create a custom retry logic provider
    SqlRetryLogicBaseProvider provider = CustomRetry.CreateCustomProvider(options);
    
    // Assumes that connection is a valid SqlConnection object 
    // Set the retry logic provider on the connection instance
    connection.RetryLogicProvider = provider;
    // Establishing the connection will trigger retry if one of the given transient failure occurs.
    connection.Open();
    

참고 항목

사용 전 구성 가능한 다시 시도 논리 스위치를 사용하도록 설정하는 것을 잊지 마세요. 자세한 내용은 구성 가능한 다시 시도 논리 사용을 참조하세요.

참고 항목