Skip to content

Commit

Permalink
Use abstract instead interface
Browse files Browse the repository at this point in the history
  • Loading branch information
DavoudEshtehari committed Sep 26, 2020
1 parent f4e1a24 commit 366c21a
Show file tree
Hide file tree
Showing 12 changed files with 214 additions and 191 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -709,11 +709,11 @@
<Reference Include="System.Memory" />
</ItemGroup>
<ItemGroup>
<Compile Include="Microsoft\Data\Reliability\Common\ISqlRetryIntervalEnumerator.cs" />
<Compile Include="Microsoft\Data\Reliability\Common\ISqlRetryLogic.cs" />
<Compile Include="Microsoft\Data\Reliability\Common\ISqlRetryLogicProvider.cs" />
<Compile Include="Microsoft\Data\Reliability\Common\SqlRetryingEventArgs.cs" />
<Compile Include="Microsoft\Data\Reliability\Common\SqlRetryLogicBase.cs" />
<Compile Include="Microsoft\Data\Reliability\Common\SqlRetryLogicBaseProvider.cs" />
<Compile Include="Microsoft\Data\Reliability\SqlConfigurableRetryLogicProviders.cs" />
<Compile Include="Microsoft\Data\Reliability\Common\SqlRetryIntervalEnumerator.cs" />
<Compile Include="Microsoft\Data\Reliability\Common\SqlRetryIntervalBaseEnumerator.cs" />
<Compile Include="Microsoft\Data\Reliability\SqlRetryIntervalEnumerators.cs" />
<Compile Include="Microsoft\Data\Reliability\Common\SqlRetryLogicProvider.cs" />
<Compile Include="Microsoft\Data\Reliability\Common\SqlRetryLogic.cs" />
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections;
using System.Collections.Generic;

namespace Microsoft.Data.SqlClient
{
/// <summary>
/// Generates a sequence of the time intervals.
/// </summary>
public abstract class SqlRetryIntervalBaseEnumerator : IEnumerator<TimeSpan>
{
/// <summary>
/// The gap time of each interval
/// </summary>
public TimeSpan GapTimeInterval { get; protected set; }

/// <summary>
/// Maximum time interval value.
/// </summary>
public TimeSpan MaxTimeInterval { get; protected set; }

/// <summary>
/// Minimum time interval value.
/// </summary>
public TimeSpan MinTimeInterval { get; protected set; }

/// <summary>
/// Gets the element in the collection at the current position of the enumerator.
/// </summary>
public TimeSpan Current { get; private set; } = TimeSpan.Zero;

object IEnumerator.Current => Current;

/// <summary>
/// Constructor
/// </summary>
public SqlRetryIntervalBaseEnumerator()
{
GapTimeInterval = TimeSpan.Zero;
MaxTimeInterval = TimeSpan.Zero;
MinTimeInterval = TimeSpan.Zero;
}

/// <summary>
/// Constructor
/// </summary>
public SqlRetryIntervalBaseEnumerator(TimeSpan timeInterval, TimeSpan maxTime, TimeSpan minTime)
{
Validate(timeInterval, maxTime, minTime);
GapTimeInterval = timeInterval;
MaxTimeInterval = maxTime;
MinTimeInterval = minTime;
}

/// <summary>
/// Sets the enumerator to its initial position, which is before the first element in the collection.
/// </summary>
public virtual void Reset()
{
Current = TimeSpan.Zero;
}

/// <summary>
/// Validate the enumeration parameters.
/// </summary>
/// <param name="timeInterval">The gap time of each interval</param>
/// <param name="maxTimeInterval">Maximum time interval value.</param>
/// <param name="minTimeInterval">Minimum time interval value.</param>
protected virtual void Validate(TimeSpan timeInterval, TimeSpan maxTimeInterval, TimeSpan minTimeInterval)
{
// valid time iterval must be between 0 and 2 minutes
// TODO: grab the localized messages from the resource file
if(timeInterval.TotalMinutes > 2)
{
throw new ArgumentOutOfRangeException(nameof(timeInterval));
}
else if (maxTimeInterval < minTimeInterval)
{
throw new ArgumentOutOfRangeException(nameof(maxTimeInterval));
}
}

/// <summary>
/// Calculate the next interval time.
/// </summary>
/// <returns>Next time interval</returns>
protected abstract TimeSpan GetNextInterval();

/// <summary>
/// Advances the enumerator to the next element of the collection.
/// </summary>
/// <returns>true if the enumerator was successfully advanced to the next element; false if the enumerator has passed the end of the collection.</returns>
public virtual bool MoveNext()
{
TimeSpan next = Current;
if (Current < MaxTimeInterval)
{
next = GetNextInterval();
}

bool result = next <= MaxTimeInterval;
if (result)
{
Current = next;
}

return result;
}

/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public virtual void Dispose()
{
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,32 @@

namespace Microsoft.Data.SqlClient.Reliability
{
internal class SqlRetryLogic : ISqlRetryLogic
internal class SqlRetryLogic : SqlRetryLogicBase
{
private const int firstCounter = 1;

public int NumberOfTries { get; private set; }

public ISqlRetryIntervalEnumerator RetryIntervalEnumerator { get; private set; }

public Predicate<Exception> TransientPredicate { get; private set; }

public int Current { get; private set; } = firstCounter;

public SqlRetryLogic(int numberOfTries, ISqlRetryIntervalEnumerator enumerator, Predicate<Exception> transientPredicate)
public SqlRetryLogic(int numberOfTries, SqlRetryIntervalBaseEnumerator enumerator, Predicate<Exception> transientPredicate)
{
Validate(numberOfTries, enumerator, transientPredicate);

NumberOfTries = numberOfTries;
RetryIntervalEnumerator = enumerator;
TransientPredicate = transientPredicate;
Current = firstCounter;
}

public SqlRetryLogic(ISqlRetryIntervalEnumerator enumerator, Predicate<Exception> transientPredicate = null)
public SqlRetryLogic(SqlRetryIntervalBaseEnumerator enumerator, Predicate<Exception> transientPredicate = null)
: this(firstCounter, enumerator, transientPredicate ?? (_ => false))
{
}

public void Reset()
public override void Reset()
{
Current = firstCounter;
RetryIntervalEnumerator.Reset();
}

private void Validate(int numberOfTries, ISqlRetryIntervalEnumerator enumerator, Predicate<Exception> transientPredicate)
private void Validate(int numberOfTries, SqlRetryIntervalBaseEnumerator enumerator, Predicate<Exception> transientPredicate)
{
if (numberOfTries < firstCounter)
{
Expand All @@ -54,7 +47,7 @@ private void Validate(int numberOfTries, ISqlRetryIntervalEnumerator enumerator,
}
}

public bool TryNextInterval(out TimeSpan intervalTime)
public override bool TryNextInterval(out TimeSpan intervalTime)
{
intervalTime = TimeSpan.Zero;
bool result = Current < NumberOfTries;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,44 @@

using System;

namespace Microsoft.Data.SqlClient.Reliability
namespace Microsoft.Data.SqlClient
{
/// <summary>
/// It retrieves next time interval with respect to the number of retries if transient condition happens.
/// </summary>
public interface ISqlRetryLogic
public abstract class SqlRetryLogicBase
{
/// <summary>
/// Number of retries.
/// </summary>
int NumberOfTries { get; }
public int NumberOfTries { get; protected set; }

/// <summary>
/// Current retry number.
/// </summary>
int Current { get; }
public int Current { get; protected set; }

/// <summary>
/// The timer interval enumerator.
/// </summary>
ISqlRetryIntervalEnumerator RetryIntervalEnumerator { get; }
public SqlRetryIntervalBaseEnumerator RetryIntervalEnumerator { get; protected set; }

/// <summary>
/// Delegate to a transient condition predicator.
/// The function that this delegate points to it must return a true value when an expected transient exception happens.
/// </summary>
Predicate<Exception> TransientPredicate { get; }
public Predicate<Exception> TransientPredicate { get; protected set; }

/// <summary>
/// Try to get the next interval time by the enumerator if the counter does not exceed from the number of retries.
/// </summary>
/// <param name="intervalTime">The interval time that is generated by the enumerator</param>
/// <returns>True if the number of retries does not exceed unless False</returns>
bool TryNextInterval(out TimeSpan intervalTime);
public abstract bool TryNextInterval(out TimeSpan intervalTime);

/// <summary>
/// Set the counters and enumerator to default values for next use.
/// </summary>
void Reset();
public abstract void Reset();
}
}
Loading

0 comments on commit 366c21a

Please sign in to comment.