Closed mu88 closed 4 months ago
Hi @AndriySvyryd, do you know how this problem can be mitigated?
@mu88 please be patient, depending on workload it can sometimes take us a while to process issues.
@mu88 Just wrap the operation in a non-retrying strategy. When execution strategies are nested only the top one takes effect. It would look something like this:
public async Task<Dictionary<string, int>> GetMetricsAsync()
{
MyDbContext dbContext = _dbContextFactory.CreateDbContext();
int numberOfInteractionPoints;
try
{
await new NpgsqlRetryingExecutionStrategy(dbContext, maxRetryCount: 0).ExecuteAsync(
async () => numberOfInteractionPoints = await dbContext.InteractionPoints.CountAsync());
}
catch (Exception)
{
numberOfInteractionPoints = 0;
}
return new Dictionary<string, int> { { nameof(InteractionPoint), numberOfInteractionPoints } };
}
Thx @AndriySvyryd for getting back to me - I really appreciate it and sry if I might have seemed unpatient!
At first glance, your code looked like a good idea to me, but after a second I thought: "Wait, but this way I can no longer test it via SQLite in-memory database".
With your mentioned approach, the EF Core is no longer DB-platform-agnostic and I'd have to introduce some sort of strategy pattern.
@mu88 You don't have to use a PostgreSQL-specific one, any class derived from ExecutionStrategy
would work. You can create your own one.
Works like a charm, thank you very much 👏🏻
Ask a question
Our app uses EF Core and PostgreSQL with enabled retries:
We've implemented a custom .NET metric which is provided via an ASP.NET Core Web API endpoint in Prometheus format. For the sake of brevity, here's a rather shortened example:
This works nicely when everything is up and running and the metrics endpoint returns after 200 ms. But when the database is down, the retry strategy kicks in and delays the web request so that it returns after one minute. This leads to further issues in our metrics scraping process.
Therefore, I'd would be great if I could opt-out from the automatic retries for this particular query, e.g.:
Include your code
A small but contrived example you find here: https://github.com/mu88/Repro_EFCore_RetryOptOut
Include stack traces
When no database is running and calling
HTTP GET /metrics
, the request succeeds after approx. 1 min withHTTP 200
(due to thetry/catch
). Here's the app's exception caused by the retry:Logs
### You can add a header ``` C:\Users\myUser\AppData\Local\Programs\Rider\plugins\dpa\DotFiles\JetBrains.DPA.Runner.exe --handle=32880 --backend-pid=35080 --etw-collect-flags=67108622 --detach-event-name=dpa.detach.32880 "C:/Program Files/IIS Express/iisexpress.exe" /config:"C:\work\GitHub\Repro_EFCore_RetryOptOut\.idea\config\applicationhost.config" /site:"WebApi" "/apppool:"WebApi AppPool"" Starting IIS Express ... Successfully registered URL "http://localhost:40503/" for site "WebApi" application "/" Successfully registered URL "https://localhost:44339/" for site "WebApi" application "/" Registration completed for site "WebApi" IIS Express is running. Enter 'Q' to stop IIS Express info: Microsoft.Hosting.Lifetime[0] Application started. Press Ctrl+C to shut down. info: Microsoft.Hosting.Lifetime[0] Hosting environment: Development info: Microsoft.Hosting.Lifetime[0] Content root path: C:\work\GitHub\Repro_EFCore_RetryOptOut\WebApi fail: Microsoft.EntityFrameworkCore.Database.Connection[20004] An error occurred using the connection to database 'master' on server 'tcp://127.0.0.1:5432'. info: Microsoft.EntityFrameworkCore.Infrastructure[10404] A transient exception occurred during execution. The operation will be retried after 0ms. Npgsql.NpgsqlException (0x80004005): Failed to connect to 127.0.0.1:5432 ---> System.Net.Sockets.SocketException (10061): No connection could be made because the target machine actively refused it. at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken) at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 token) at System.Net.Sockets.Socket.Include verbose output
Please include verbose output when asking questions about the
dotnet ef
or Package Manager Console tools.Use triple-tick fences for tool output. For example:
Include provider and version information
EF Core version:
8.0.6
Database provider:Npgsql.EntityFrameworkCore.PostgreSQL
Target framework: .NET 8.0 Operating system: Windows / WSL2 (Ubuntu) / Linux IDE: both JetBrains Rider and Visual Studio