Выполнениие лабораторной работы на 5, трек 2.
Skeleton contracts / contracts (pull_request) Canceled after 0s
Skeleton contracts / contracts (pull_request) Canceled after 0s
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
using System.Data.Common;
|
||||
using Microsoft.EntityFrameworkCore.Diagnostics;
|
||||
using Npgsql;
|
||||
|
||||
namespace Messenger.Api.Services;
|
||||
|
||||
public sealed class QueryCounter
|
||||
{
|
||||
public int Count;
|
||||
}
|
||||
|
||||
// Считает SQL-команды, выполненные EF Core в рамках текущего HTTP-запроса (для измерения «запросов к БД на запрос API»).
|
||||
// Включается только при DIAGNOSTIC_HEADERS=true. Команда ReserveMessageSlot (сырой NpgsqlCommand) не учитывается.
|
||||
public sealed class QueryCountingInterceptor(IHttpContextAccessor accessor) : DbCommandInterceptor
|
||||
{
|
||||
public const string ItemKey = "diagnostics.query_counter";
|
||||
|
||||
private void Count()
|
||||
{
|
||||
if (accessor.HttpContext?.Items[ItemKey] is QueryCounter counter)
|
||||
{
|
||||
Interlocked.Increment(ref counter.Count);
|
||||
}
|
||||
}
|
||||
|
||||
public override InterceptionResult<DbDataReader> ReaderExecuting(DbCommand command, CommandEventData eventData, InterceptionResult<DbDataReader> result)
|
||||
{
|
||||
Count();
|
||||
return result;
|
||||
}
|
||||
|
||||
public override ValueTask<InterceptionResult<DbDataReader>> ReaderExecutingAsync(DbCommand command, CommandEventData eventData, InterceptionResult<DbDataReader> result, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Count();
|
||||
return ValueTask.FromResult(result);
|
||||
}
|
||||
|
||||
public override InterceptionResult<int> NonQueryExecuting(DbCommand command, CommandEventData eventData, InterceptionResult<int> result)
|
||||
{
|
||||
Count();
|
||||
return result;
|
||||
}
|
||||
|
||||
public override ValueTask<InterceptionResult<int>> NonQueryExecutingAsync(DbCommand command, CommandEventData eventData, InterceptionResult<int> result, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Count();
|
||||
return ValueTask.FromResult(result);
|
||||
}
|
||||
|
||||
public override InterceptionResult<object> ScalarExecuting(DbCommand command, CommandEventData eventData, InterceptionResult<object> result)
|
||||
{
|
||||
Count();
|
||||
return result;
|
||||
}
|
||||
|
||||
public override ValueTask<InterceptionResult<object>> ScalarExecutingAsync(DbCommand command, CommandEventData eventData, InterceptionResult<object> result, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Count();
|
||||
return ValueTask.FromResult(result);
|
||||
}
|
||||
}
|
||||
|
||||
public static class DependencyFailures
|
||||
{
|
||||
// Недоступность или перегрузка PostgreSQL (нет соединения, таймаут, исчерпан пул, остановка сервера) — это 503,
|
||||
// а не 500: ошибка ограничена, внутренние детали не раскрываются, запрос не принимается как доверенный.
|
||||
public static bool IsDatabaseUnavailable(Exception? error)
|
||||
{
|
||||
for (var e = error; e is not null; e = e.InnerException)
|
||||
{
|
||||
switch (e)
|
||||
{
|
||||
case PostgresException pg:
|
||||
// 08 — соединение, 53 — нехватка ресурсов, 57 — вмешательство оператора (shutdown и т.п.)
|
||||
if (pg.SqlState.StartsWith("08") || pg.SqlState.StartsWith("53") || pg.SqlState.StartsWith("57"))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
break;
|
||||
case NpgsqlException:
|
||||
case TimeoutException:
|
||||
case System.Net.Sockets.SocketException:
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user