-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from MikeAmputer/kravtsov/retryable-executor
Retryable executor
- Loading branch information
Showing
19 changed files
with
205 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 0 additions & 117 deletions
117
src/ClickHouse.Facades/Context/ClickHouseRetryHelpers.cs
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/ClickHouse.Facades/Context/Retryable/ClickHouseRetryPolicy.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using ClickHouse.Client; | ||
|
||
namespace ClickHouse.Facades; | ||
|
||
public class ClickHouseRetryPolicy | ||
{ | ||
public Predicate<ClickHouseServerException> TransientExceptionPredicate { get; set; } = _ => true; | ||
|
||
public Action<ClickHouseServerException>? ExceptionHandler { get; set; } = null; | ||
|
||
public int RetryCount { get; set; } = 3; | ||
|
||
public Func<int, TimeSpan> RetryDelayProvider { get; set; } = _ => TimeSpan.Zero; | ||
} |
82 changes: 82 additions & 0 deletions
82
src/ClickHouse.Facades/Context/Retryable/ClickHouseRetryableExecutor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using ClickHouse.Client; | ||
|
||
namespace ClickHouse.Facades; | ||
|
||
internal class ClickHouseRetryableExecutor<TContext> : IClickHouseRetryableExecutor<TContext> | ||
where TContext : ClickHouseContext<TContext> | ||
{ | ||
private readonly IClickHouseContextFactory<TContext> _contextFactory; | ||
|
||
private ClickHouseRetryPolicy _retryPolicy; | ||
|
||
public ClickHouseRetryableExecutor( | ||
IClickHouseContextFactory<TContext> contextFactory, | ||
ClickHouseRetryPolicy retryPolicy) | ||
{ | ||
_contextFactory = contextFactory | ||
?? throw new ArgumentNullException(nameof(contextFactory)); | ||
|
||
_retryPolicy = retryPolicy | ||
?? throw new ArgumentNullException(nameof(retryPolicy)); | ||
} | ||
|
||
public Task ExecuteAsync(Func<TContext, Task> action, CancellationToken cancellationToken = default) | ||
{ | ||
return ExecuteAsync(async context => | ||
{ | ||
await action(context); | ||
return 0; | ||
}, | ||
cancellationToken); | ||
} | ||
|
||
public async Task<TResult> ExecuteAsync<TResult>( | ||
Func<TContext, Task<TResult>> action, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
var attemptNumber = 1; | ||
while (true) | ||
{ | ||
cancellationToken.ThrowIfCancellationRequested(); | ||
|
||
try | ||
{ | ||
await using var context = await _contextFactory.CreateContextAsync(); | ||
|
||
return await action(context); | ||
} | ||
catch (ClickHouseServerException e) | ||
{ | ||
if (attemptNumber >= _retryPolicy.RetryCount + 1) | ||
{ | ||
throw; | ||
} | ||
|
||
if (_retryPolicy.TransientExceptionPredicate != null && !_retryPolicy.TransientExceptionPredicate(e)) | ||
{ | ||
throw; | ||
} | ||
|
||
_retryPolicy.ExceptionHandler?.Invoke(e); | ||
} | ||
|
||
await Task.Delay(_retryPolicy.RetryDelayProvider(attemptNumber), cancellationToken); | ||
attemptNumber++; | ||
} | ||
} | ||
|
||
public IClickHouseReadonlyRetryableExecutor<TContext> SetRetryPolicy(ClickHouseRetryPolicy retryPolicy) | ||
{ | ||
_retryPolicy = retryPolicy ?? throw new ArgumentNullException(nameof(retryPolicy)); | ||
|
||
return this; | ||
} | ||
|
||
public IClickHouseReadonlyRetryableExecutor<TContext> UpdateRetryPolicy(Action<ClickHouseRetryPolicy> updateAction) | ||
{ | ||
updateAction(_retryPolicy); | ||
|
||
return this; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/ClickHouse.Facades/Context/Retryable/IClickHouseReadonlyRetryableExecutor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace ClickHouse.Facades; | ||
|
||
public interface IClickHouseReadonlyRetryableExecutor<out TContext> | ||
where TContext : ClickHouseContext<TContext> | ||
{ | ||
Task ExecuteAsync( | ||
Func<TContext, Task> action, | ||
CancellationToken cancellationToken = default); | ||
|
||
Task<TResult> ExecuteAsync<TResult>( | ||
Func<TContext, Task<TResult>> action, | ||
CancellationToken cancellationToken = default); | ||
} |
9 changes: 9 additions & 0 deletions
9
src/ClickHouse.Facades/Context/Retryable/IClickHouseRetryableExecutor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace ClickHouse.Facades; | ||
|
||
public interface IClickHouseRetryableExecutor<out TContext> : IClickHouseReadonlyRetryableExecutor<TContext> | ||
where TContext : ClickHouseContext<TContext> | ||
{ | ||
IClickHouseReadonlyRetryableExecutor<TContext> SetRetryPolicy(ClickHouseRetryPolicy retryPolicy); | ||
|
||
IClickHouseReadonlyRetryableExecutor<TContext> UpdateRetryPolicy(Action<ClickHouseRetryPolicy> updateAction); | ||
} |
Oops, something went wrong.