< Summary - AsyncResponse (Release / net8.0+net10.0 / unit+integration)

Information
Class: AsyncResponse.Transports.SqlServer.SqlServerWorkerTransport
Assembly: AsyncResponse.Transports.SqlServer
File(s): /home/runner/work/AsyncResponse/AsyncResponse/src/Transports/AsyncResponse.Transports.SqlServer/SqlServerWorkerTransport.cs
Line coverage
100%
Covered lines: 42
Uncovered lines: 0
Coverable lines: 42
Total lines: 140
Line coverage: 100%
Branch coverage
100%
Covered branches: 8
Total branches: 8
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
.ctor(...)100%11100%
PublishAsync()100%88100%
<PublishAsync()100%11100%

File(s)

/home/runner/work/AsyncResponse/AsyncResponse/src/Transports/AsyncResponse.Transports.SqlServer/SqlServerWorkerTransport.cs

#LineLine coverage
 1using Microsoft.Data.SqlClient;
 2using Microsoft.Extensions.Options;
 3using System.Diagnostics;
 4using System.Text.Json;
 5
 6namespace AsyncResponse.Transports.SqlServer;
 7
 8/// <summary>Publishes <see cref="WorkerJobEnvelope"/> messages to the SQL Server worker queue.</summary>
 9public sealed class SqlServerWorkerTransport : IWorkerTransport
 10{
 11    private readonly SqlServerAsyncResponseTransportOptions _options;
 12    private readonly SqlServerTransportStore _store;
 13
 14    /// <summary>Creates a SQL Server worker transport over the configured connection string.</summary>
 15    public SqlServerWorkerTransport(IOptions<SqlServerAsyncResponseTransportOptions> options)
 316        : this(options, new SqlServerTransportStore(options))
 17    {
 318    }
 19
 320    internal SqlServerWorkerTransport(
 321        IOptions<SqlServerAsyncResponseTransportOptions> options,
 322        SqlServerTransportStore store)
 23    {
 324        _options = options.Value;
 325        SqlServerTransportOptionsValidator.ValidateCommon(_options);
 326        _store = store;
 327    }
 28
 29    /// <inheritdoc />
 30    public async Task PublishAsync(WorkerJobEnvelope job, CancellationToken cancellationToken = default)
 31    {
 332        ArgumentNullException.ThrowIfNull(job);
 33
 334        using var activity = AsyncResponseDiagnostics.StartActivity(
 335            "asyncresponse.worker.publish",
 336            ActivityKind.Producer,
 337            job.CorrelationId);
 338        activity?.SetTag("asyncresponse.transport", "sqlserver");
 339        activity?.SetTag("messaging.system", "sqlserver");
 340        activity?.SetTag("messaging.destination.name", _options.WorkerQueue);
 341        AsyncResponseDiagnostics.SetReplyTarget(activity, job.ReplyTarget);
 342        AsyncResponseDiagnostics.SetWorker(activity, job.Call);
 43
 44        try
 45        {
 346            var headers = string.IsNullOrWhiteSpace(job.CorrelationId)
 347                ? null
 348                : new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
 349                {
 350                    [_options.CorrelationIdHeader] = job.CorrelationId!
 351                };
 52
 353            var payload = AsyncResponseJson.Serialize(job);
 54            // Stable id outside the retry loop so a retried publish is idempotent rather than enqueuing
 55            // the same worker job twice.
 356            var messageId = Guid.NewGuid();
 357            await SqlServerTransportRetry.ExecuteAsync(
 358                async token =>
 359                {
 360                    await _store.PublishAsync(messageId, _options.WorkerQueue, payload, headers, token).ConfigureAwait(f
 161                    return true;
 162                },
 363                _options.PublishMaxAttempts,
 364                _options.PublishRetryBaseDelay,
 365                _options.PublishRetryMaxDelay,
 366                cancellationToken).ConfigureAwait(false);
 167        }
 368        catch (Exception ex)
 69        {
 370            AsyncResponseDiagnostics.SetError(activity, ex);
 371            throw;
 72        }
 173    }
 74}
 75
 76internal static class SqlServerTransportRetry
 77{
 78    public static Task<T> ExecuteAsync<T>(
 79        Func<CancellationToken, Task<T>> action,
 80        int maxAttempts,
 81        TimeSpan baseDelay,
 82        TimeSpan maxDelay,
 83        CancellationToken cancellationToken)
 84        => AsyncResponseRetry.ExecuteAsync(action, IsTransient, maxAttempts, baseDelay, maxDelay, cancellationToken);
 85
 86    public static bool IsTransient(Exception exception)
 87        => exception is not OperationCanceledException
 88           && (exception is SqlException sqlException && SqlServerTransientFaults.IsTransient(sqlException)
 89               || exception is TimeoutException);
 90}
 91
 92/// <summary>
 93/// Classifies SQL Server errors worth retrying. <see cref="SqlException"/> exposes no public
 94/// transient flag, so this mirrors the error numbers Microsoft's own retry guidance and the
 95/// SqlClient configurable-retry defaults treat as transient, plus severity ≥ 20 (broken connection).
 96/// </summary>
 97internal static class SqlServerTransientFaults
 98{
 99    private static readonly HashSet<int> TransientErrorNumbers =
 100    [
 101        -2,    // client-side command timeout
 102        20,    // instance does not support encryption
 103        64,    // connection lost during login
 104        121,   // transport semaphore timeout
 105        233,   // no process on the other end of the pipe
 106        997,   // overlapped I/O in progress
 107        1204,  // lock resources exhausted
 108        1205,  // deadlock victim
 109        1222,  // lock request timeout
 110        4060,  // database unavailable
 111        4221,  // readable secondary timeout
 112        10053, // transport-level connection abort
 113        10054, // transport-level connection reset
 114        10060, // network unreachable / connect timeout
 115        10928, // Azure SQL resource limit reached
 116        10929, // Azure SQL minimum guarantee exceeded
 117        40143, // Azure SQL connection failure
 118        40197, // Azure SQL service processing error
 119        40501, // Azure SQL service busy
 120        40540, // Azure SQL service unavailable
 121        40613, // Azure SQL database unavailable
 122        49918, // cannot process request, not enough resources
 123        49919, // cannot process create/update request
 124        49920  // cannot process request, too many operations
 125    ];
 126
 127    public static bool IsTransient(SqlException exception)
 128    {
 129        if (exception.Class >= 20)
 130            return true;
 131
 132        foreach (SqlError error in exception.Errors)
 133        {
 134            if (TransientErrorNumbers.Contains(error.Number))
 135                return true;
 136        }
 137
 138        return TransientErrorNumbers.Contains(exception.Number);
 139    }
 140}