Skip to content

Commit 8bd39c7

Browse files
committed
Throttle health endpoint DB ping to once every 5 minutes
Avoids hitting SQL Server on every health check request while still keeping the Azure SQL serverless free tier from auto-pausing.
1 parent 5b09a3b commit 8bd39c7

1 file changed

Lines changed: 10 additions & 3 deletions

File tree

src/FitSpec.API/Program.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,18 @@
9090
}
9191

9292
app.UseCors("Angular");
93+
94+
var lastDbPing = DateTime.MinValue;
9395
app.MapGet("/health", async (IDapperConnectionFactory db) =>
9496
{
95-
using var conn = db.CreateConnection();
96-
await conn.ExecuteScalarAsync<int>("SELECT 1");
97-
return Results.Ok(new { status = "healthy", timestamp = DateTime.UtcNow });
97+
var now = DateTime.UtcNow;
98+
if ((now - lastDbPing).TotalMinutes >= 5)
99+
{
100+
using var conn = db.CreateConnection();
101+
await conn.ExecuteScalarAsync<int>("SELECT 1");
102+
lastDbPing = now;
103+
}
104+
return Results.Ok(new { status = "healthy", timestamp = now });
98105
});
99106
app.MapControllers();
100107
app.MapHub<FitSpec.API.Hubs.InventoryHub>("/hubs/inventory");

0 commit comments

Comments
 (0)