-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
IExecutionStrategy.cs
88 lines (84 loc) · 4.64 KB
/
IExecutionStrategy.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
namespace Microsoft.EntityFrameworkCore.Storage;
/// <summary>
/// A strategy that is used to execute a command or query against the database, possibly with logic to retry when a
/// failure occurs.
/// </summary>
/// <remarks>
/// <para>
/// The service lifetime is <see cref="ServiceLifetime.Scoped" />. This means that each
/// <see cref="DbContext" /> instance will use its own instance of this service.
/// The implementation may depend on other services registered with any lifetime.
/// The implementation does not need to be thread-safe.
/// </para>
/// <para>
/// See <see href="https://aka.ms/efcore-docs-connection-resiliency">Connection resiliency and database retries</see>
/// for more information and examples.
/// </para>
/// </remarks>
public interface IExecutionStrategy
{
/// <summary>
/// Indicates whether this <see cref="IExecutionStrategy" /> might retry the execution after a failure.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-connection-resiliency">Connection resiliency and database retries</see>
/// for more information and examples.
/// </remarks>
bool RetriesOnFailure { get; }
/// <summary>
/// Executes the specified operation and returns the result.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-connection-resiliency">Connection resiliency and database retries</see>
/// for more information and examples.
/// </remarks>
/// <param name="state">The state that will be passed to the operation.</param>
/// <param name="operation">
/// A delegate representing an executable operation that returns the result of type <typeparamref name="TResult" />.
/// </param>
/// <param name="verifySucceeded">A delegate that tests whether the operation succeeded even though an exception was thrown.</param>
/// <typeparam name="TState">The type of the state.</typeparam>
/// <typeparam name="TResult">The return type of <paramref name="operation" />.</typeparam>
/// <returns>The result from the operation.</returns>
/// <exception cref="RetryLimitExceededException">
/// The operation has not succeeded after the configured number of retries.
/// </exception>
TResult Execute<TState, TResult>(
TState state,
Func<DbContext, TState, TResult> operation,
Func<DbContext, TState, ExecutionResult<TResult>>? verifySucceeded);
/// <summary>
/// Executes the specified asynchronous operation and returns the result.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-connection-resiliency">Connection resiliency and database retries</see>
/// for more information and examples.
/// </remarks>
/// <param name="state">The state that will be passed to the operation.</param>
/// <param name="operation">
/// A function that returns a started task of type <typeparamref name="TResult" />.
/// </param>
/// <param name="verifySucceeded">A delegate that tests whether the operation succeeded even though an exception was thrown.</param>
/// <param name="cancellationToken">
/// A cancellation token used to cancel the retry operation, but not operations that are already in flight
/// or that already completed successfully.
/// </param>
/// <typeparam name="TState">The type of the state.</typeparam>
/// <typeparam name="TResult">The result type of the <see cref="Task{T}" /> returned by <paramref name="operation" />.</typeparam>
/// <returns>
/// A task that will run to completion if the original task completes successfully (either the
/// first time or after retrying transient failures). If the task fails with a non-transient error or
/// the retry limit is reached, the returned task will become faulted and the exception must be observed.
/// </returns>
/// <exception cref="RetryLimitExceededException">
/// The operation has not succeeded after the configured number of retries.
/// </exception>
/// <exception cref="OperationCanceledException">If the <see cref="CancellationToken" /> is canceled.</exception>
Task<TResult> ExecuteAsync<TState, TResult>(
TState state,
Func<DbContext, TState, CancellationToken, Task<TResult>> operation,
Func<DbContext, TState, CancellationToken, Task<ExecutionResult<TResult>>>? verifySucceeded,
CancellationToken cancellationToken = default);
}