Skip to content

Commit cc489f3

Browse files
committed
use common retry policy builder function
1 parent 6635621 commit cc489f3

File tree

4 files changed

+19
-24
lines changed

4 files changed

+19
-24
lines changed

Projects/Dotmim.Sync.Core/Orchestrators/ApplyChanges/BaseOrchestrator.ApplyChanges.cs

+1-8
Original file line numberDiff line numberDiff line change
@@ -242,15 +242,8 @@ internal virtual async Task<Exception> InternalApplyTableChangesAsync(ScopeInfo
242242

243243
this.Logger.LogInformation("[InternalApplyTableChangesAsync]. Directory name {DirectoryName}. BatchParts count {BatchPartsInfoCount}", message.Changes.DirectoryName, message.Changes.BatchPartsInfo.Count);
244244

245-
// If we have a transient error happening, and we are rerunning the tranaction,
246-
// raising an interceptor
247-
var onRetry = new Func<Exception, int, TimeSpan, object, Task>((ex, cpt, ts, arg) =>
248-
this.InterceptAsync(new TransientErrorOccuredArgs(context, connection, ex, cpt, ts), progress, cancellationToken).AsTask());
249-
250245
// Defining my retry policy
251-
var retryPolicy = this.Options.TransactionMode != TransactionMode.AllOrNothing
252-
? SyncPolicy.WaitAndRetryForever(retryAttempt => TimeSpan.FromMilliseconds(500 * retryAttempt), (ex, arg) => this.Provider.ShouldRetryOn(ex), onRetry)
253-
: SyncPolicy.WaitAndRetry(0, TimeSpan.Zero);
246+
var retryPolicy = this.BuildSyncPolicy(context, connection, progress, cancellationToken);
254247

255248
var applyChangesPolicyResult = await retryPolicy.ExecuteAsync(
256249
async () =>

Projects/Dotmim.Sync.Core/Orchestrators/ApplyChanges/LocalOrchestrator.ApplyChanges.cs

+1-8
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,8 @@ public partial class LocalOrchestrator : BaseOrchestrator
2525
DbConnection connection = default, DbTransaction transaction = default,
2626
IProgress<ProgressArgs> progress = null, CancellationToken cancellationToken = default)
2727
{
28-
// If we have a transient error happening, and we are rerunning the tranaction,
29-
// raising an interceptor
30-
var onRetry = new Func<Exception, int, TimeSpan, object, Task>((ex, cpt, ts, arg) =>
31-
this.InterceptAsync(new TransientErrorOccuredArgs(context, connection, ex, cpt, ts), progress, cancellationToken).AsTask());
32-
3328
// Defining my retry policy
34-
SyncPolicy retryPolicy = this.Options.TransactionMode == TransactionMode.AllOrNothing
35-
? retryPolicy = SyncPolicy.WaitAndRetryForever(retryAttempt => TimeSpan.FromMilliseconds(500 * retryAttempt), (ex, arg) => this.Provider.ShouldRetryOn(ex), onRetry)
36-
: retryPolicy = SyncPolicy.WaitAndRetry(0, TimeSpan.Zero);
29+
var retryPolicy = this.BuildSyncPolicy(context, connection, progress, cancellationToken);
3730

3831
// Execute my OpenAsync in my policy context
3932
var applyChangesResult = await retryPolicy.ExecuteAsync<(SyncContext Context, ClientSyncChanges ClientSyncChange, ScopeInfoClient CScopeInfoClient)>(

Projects/Dotmim.Sync.Core/Orchestrators/ApplyChanges/RemoteOrchestrator.ApplyChanges.cs

+1-8
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,8 @@ public partial class RemoteOrchestrator : BaseOrchestrator
5252
// Previous sync errors
5353
BatchInfo lastSyncErrorsBatchInfo = null;
5454

55-
// If we have a transient error happening, and we are rerunning the tranaction,
56-
// raising an interceptor
57-
var onRetry = new Func<Exception, int, TimeSpan, object, Task>((ex, cpt, ts, arg) =>
58-
this.InterceptAsync(new TransientErrorOccuredArgs(context, connection, ex, cpt, ts), progress, cancellationToken).AsTask());
59-
6055
// Defining my retry policy
61-
SyncPolicy retryPolicy = this.Options.TransactionMode == TransactionMode.AllOrNothing
62-
? retryPolicy = SyncPolicy.WaitAndRetryForever(retryAttempt => TimeSpan.FromMilliseconds(500 * retryAttempt), (ex, arg) => this.Provider.ShouldRetryOn(ex), onRetry)
63-
: retryPolicy = SyncPolicy.WaitAndRetry(0, TimeSpan.Zero);
56+
var retryPolicy = this.BuildSyncPolicy(context, connection, progress, cancellationToken);
6457

6558
await retryPolicy.ExecuteAsync(
6659
async () =>

Projects/Dotmim.Sync.Core/Orchestrators/BaseOrchestrator.cs

+16
Original file line numberDiff line numberDiff line change
@@ -445,5 +445,21 @@ internal SyncException GetSyncError(SyncContext context, Exception exception, st
445445
throw this.GetSyncError(context, ex);
446446
}
447447
}
448+
449+
protected SyncPolicy BuildSyncPolicy(SyncContext context, DbConnection connection, IProgress<ProgressArgs> progress, CancellationToken cancellationToken, int retryIntervalMilliseconds = 500)
450+
{
451+
// If we have a transient error happening, and we are rerunning the transaction,
452+
// raising an interceptor
453+
var onRetry = new Func<Exception, int, TimeSpan, object, Task>((ex, cpt, ts, arg) =>
454+
this.InterceptAsync(new TransientErrorOccuredArgs(context, connection, ex, cpt, ts), progress, cancellationToken).AsTask());
455+
456+
return this.Options.TransactionMode switch
457+
{
458+
TransactionMode.AllOrNothing => SyncPolicy.WaitAndRetry(0, TimeSpan.Zero),
459+
TransactionMode.PerBatch => SyncPolicy.WaitAndRetryForever(retryAttempt => TimeSpan.FromMilliseconds(retryIntervalMilliseconds * retryAttempt), (ex, arg) => this.Provider.ShouldRetryOn(ex), onRetry),
460+
TransactionMode.None => SyncPolicy.WaitAndRetryForever(retryAttempt => TimeSpan.FromMilliseconds(retryIntervalMilliseconds * retryAttempt), (ex, arg) => this.Provider.ShouldRetryOn(ex), onRetry),
461+
_ => throw new NotImplementedException(),
462+
};
463+
}
448464
}
449465
}

0 commit comments

Comments
 (0)