| | | 1 | | using System.Text.Json; |
| | | 2 | | using Microsoft.Extensions.Logging; |
| | | 3 | | |
| | | 4 | | namespace TeleFlow.Telegram.Internal; |
| | | 5 | | |
| | | 6 | | internal sealed partial class TelegramRequestExecutor : ITelegramRequestExecutor |
| | | 7 | | { |
| | | 8 | | private const int RequestStartedEventId = 1; |
| | | 9 | | private const int RequestCompletedEventId = 2; |
| | | 10 | | private const int RequestThrottledEventId = 3; |
| | | 11 | | private const int RequestFailedEventId = 4; |
| | | 12 | | |
| | | 13 | | private readonly JsonSerializerOptions _serializerOptions; |
| | | 14 | | private readonly TelegramRequestSender _sender; |
| | | 15 | | private readonly TelegramTransportEnvelopeParser _envelopeParser; |
| | | 16 | | private readonly TelegramRetryAfterPolicy _retryAfterPolicy; |
| | | 17 | | private readonly TimeProvider _timeProvider; |
| | | 18 | | private readonly ILogger<TelegramRequestExecutor> _logger; |
| | | 19 | | |
| | | 20 | | public TelegramRequestExecutor( |
| | | 21 | | ITelegramTransport transport, |
| | | 22 | | TelegramClientOptions options, |
| | | 23 | | TelegramJsonOptions jsonOptions, |
| | | 24 | | TimeProvider timeProvider, |
| | | 25 | | ILoggerFactory loggerFactory) |
| | | 26 | | { |
| | 302 | 27 | | ArgumentNullException.ThrowIfNull(transport); |
| | 302 | 28 | | ArgumentNullException.ThrowIfNull(options); |
| | 302 | 29 | | ArgumentNullException.ThrowIfNull(jsonOptions); |
| | 302 | 30 | | ArgumentNullException.ThrowIfNull(timeProvider); |
| | 302 | 31 | | ArgumentNullException.ThrowIfNull(loggerFactory); |
| | | 32 | | |
| | 302 | 33 | | _serializerOptions = jsonOptions.SerializerOptions; |
| | 302 | 34 | | _sender = new TelegramRequestSender( |
| | 302 | 35 | | transport, |
| | 302 | 36 | | options, |
| | 302 | 37 | | new TelegramRequestContentBuilder(_serializerOptions)); |
| | 302 | 38 | | _envelopeParser = new TelegramTransportEnvelopeParser(_serializerOptions); |
| | 302 | 39 | | _retryAfterPolicy = options.RetryAfter; |
| | 302 | 40 | | _timeProvider = timeProvider; |
| | 302 | 41 | | _logger = loggerFactory.CreateLogger<TelegramRequestExecutor>(); |
| | 302 | 42 | | } |
| | | 43 | | |
| | | 44 | | public async Task<TResponse> ExecuteAsync<TResponse>( |
| | | 45 | | ITelegramRequest<TResponse> request, |
| | | 46 | | CancellationToken cancellationToken = default) |
| | | 47 | | where TResponse : ITelegramResponse |
| | | 48 | | { |
| | 85 | 49 | | var executableRequest = GetExecutableRequest(request); |
| | 85 | 50 | | var context = TelegramRequestExecutionContext.Create(executableRequest.MethodName); |
| | | 51 | | |
| | 92 | 52 | | for (var attempt = 1; ; attempt++) |
| | | 53 | | { |
| | 92 | 54 | | var diagnostics = TelegramRequestAttemptDiagnostics.Create(this, context, attempt); |
| | 92 | 55 | | var response = await SendAttemptAsync( |
| | 92 | 56 | | executableRequest, |
| | 92 | 57 | | diagnostics, |
| | 92 | 58 | | cancellationToken).ConfigureAwait(false); |
| | | 59 | | |
| | 87 | 60 | | if (!_envelopeParser.TryParse(response.Body, out var envelope, out var parseException)) |
| | | 61 | | { |
| | 5 | 62 | | if (await TryDelayRetryAfterAsync( |
| | 5 | 63 | | diagnostics, |
| | 5 | 64 | | attempt, |
| | 5 | 65 | | response, |
| | 5 | 66 | | envelope: null, |
| | 5 | 67 | | cancellationToken).ConfigureAwait(false)) |
| | | 68 | | { |
| | | 69 | | continue; |
| | | 70 | | } |
| | | 71 | | |
| | 4 | 72 | | throw CreateUnparsedResponseException( |
| | 4 | 73 | | context, |
| | 4 | 74 | | diagnostics, |
| | 4 | 75 | | response, |
| | 4 | 76 | | parseException); |
| | | 77 | | } |
| | | 78 | | |
| | 82 | 79 | | using var parsedEnvelope = envelope; |
| | | 80 | | |
| | 82 | 81 | | if (parsedEnvelope.Ok) |
| | | 82 | | { |
| | 54 | 83 | | return DeserializeSuccessResponse( |
| | 54 | 84 | | executableRequest, |
| | 54 | 85 | | context, |
| | 54 | 86 | | diagnostics, |
| | 54 | 87 | | response, |
| | 54 | 88 | | parsedEnvelope); |
| | | 89 | | } |
| | | 90 | | |
| | 28 | 91 | | if (await TryDelayRetryAfterAsync( |
| | 28 | 92 | | diagnostics, |
| | 28 | 93 | | attempt, |
| | 28 | 94 | | response, |
| | 28 | 95 | | parsedEnvelope, |
| | 28 | 96 | | cancellationToken).ConfigureAwait(false)) |
| | | 97 | | { |
| | 6 | 98 | | continue; |
| | | 99 | | } |
| | | 100 | | |
| | 21 | 101 | | throw CreateApiFailureException( |
| | 21 | 102 | | context, |
| | 21 | 103 | | diagnostics, |
| | 21 | 104 | | response, |
| | 21 | 105 | | parsedEnvelope); |
| | | 106 | | } |
| | 53 | 107 | | } |
| | | 108 | | |
| | | 109 | | private static ITelegramExecutableRequest<TResponse> GetExecutableRequest<TResponse>( |
| | | 110 | | ITelegramRequest<TResponse> request) |
| | | 111 | | where TResponse : ITelegramResponse |
| | | 112 | | { |
| | 85 | 113 | | if (request is ITelegramExecutableRequest<TResponse> executableRequest) |
| | | 114 | | { |
| | 85 | 115 | | return executableRequest; |
| | | 116 | | } |
| | | 117 | | |
| | 0 | 118 | | throw new InvalidOperationException( |
| | 0 | 119 | | $"Request type '{request.GetType().FullName}' is not executable by the Telegram runtime."); |
| | | 120 | | } |
| | | 121 | | |
| | | 122 | | private async Task<TelegramTransportResponse> SendAttemptAsync<TResponse>( |
| | | 123 | | ITelegramExecutableRequest<TResponse> executableRequest, |
| | | 124 | | TelegramRequestAttemptDiagnostics diagnostics, |
| | | 125 | | CancellationToken cancellationToken) |
| | | 126 | | where TResponse : ITelegramResponse |
| | | 127 | | { |
| | | 128 | | try |
| | | 129 | | { |
| | 92 | 130 | | var transportRequest = _sender.CreateRequest(executableRequest); |
| | 91 | 131 | | diagnostics.LogStarted(transportRequest.Content); |
| | | 132 | | |
| | 91 | 133 | | var response = await _sender.SendAsync(transportRequest, cancellationToken).ConfigureAwait(false); |
| | 87 | 134 | | diagnostics.LogCompleted(response.StatusCode); |
| | | 135 | | |
| | 87 | 136 | | return response; |
| | | 137 | | } |
| | 1 | 138 | | catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) |
| | | 139 | | { |
| | 1 | 140 | | throw; |
| | | 141 | | } |
| | 4 | 142 | | catch (Exception exception) |
| | | 143 | | { |
| | 4 | 144 | | diagnostics.LogAttemptFailure(GetHttpStatusCode(exception), exception); |
| | 4 | 145 | | throw; |
| | | 146 | | } |
| | 87 | 147 | | } |
| | | 148 | | |
| | | 149 | | private async ValueTask<bool> TryDelayRetryAfterAsync( |
| | | 150 | | TelegramRequestAttemptDiagnostics diagnostics, |
| | | 151 | | int attempt, |
| | | 152 | | TelegramTransportResponse response, |
| | | 153 | | TelegramTransportEnvelope? envelope, |
| | | 154 | | CancellationToken cancellationToken) |
| | | 155 | | { |
| | 33 | 156 | | if (!IsThrottlingResponse(response.StatusCode, envelope)) |
| | | 157 | | { |
| | 19 | 158 | | return false; |
| | | 159 | | } |
| | | 160 | | |
| | 14 | 161 | | var retryAfterDelay = TelegramRetryAfterDelayResolver.ResolveDelay(response, envelope, _timeProvider); |
| | 14 | 162 | | if (retryAfterDelay is null || |
| | 14 | 163 | | !CanRetryAfter(attempt, retryAfterDelay.Value)) |
| | | 164 | | { |
| | 6 | 165 | | return false; |
| | | 166 | | } |
| | | 167 | | |
| | 8 | 168 | | diagnostics.LogThrottled(retryAfterDelay.Value); |
| | | 169 | | |
| | 8 | 170 | | await TelegramRetryAfterDelayResolver.DelayAsync( |
| | 8 | 171 | | retryAfterDelay.Value, |
| | 8 | 172 | | _timeProvider, |
| | 8 | 173 | | cancellationToken).ConfigureAwait(false); |
| | | 174 | | |
| | 7 | 175 | | return true; |
| | 32 | 176 | | } |
| | | 177 | | |
| | | 178 | | private TResponse DeserializeSuccessResponse<TResponse>( |
| | | 179 | | ITelegramExecutableRequest<TResponse> executableRequest, |
| | | 180 | | TelegramRequestExecutionContext context, |
| | | 181 | | TelegramRequestAttemptDiagnostics diagnostics, |
| | | 182 | | TelegramTransportResponse response, |
| | | 183 | | TelegramTransportEnvelope envelope) |
| | | 184 | | where TResponse : ITelegramResponse |
| | | 185 | | { |
| | 54 | 186 | | if (!envelope.HasResult) |
| | | 187 | | { |
| | 0 | 188 | | var exception = new TelegramDecodeException( |
| | 0 | 189 | | $"Telegram response for method '{context.MethodName}' did not contain a result payload.", |
| | 0 | 190 | | methodName: context.MethodName, |
| | 0 | 191 | | httpStatusCode: response.StatusCode); |
| | | 192 | | |
| | 0 | 193 | | diagnostics.LogFinalFailure(response.StatusCode, exception); |
| | 0 | 194 | | throw exception; |
| | | 195 | | } |
| | | 196 | | |
| | | 197 | | try |
| | | 198 | | { |
| | 54 | 199 | | return executableRequest.DeserializeResponse(_serializerOptions, envelope.Result); |
| | | 200 | | } |
| | 0 | 201 | | catch (TelegramRequestException) |
| | | 202 | | { |
| | 0 | 203 | | throw; |
| | | 204 | | } |
| | 1 | 205 | | catch (Exception exception) when (exception is JsonException or NotSupportedException) |
| | | 206 | | { |
| | 1 | 207 | | var decodeException = new TelegramDecodeException( |
| | 1 | 208 | | $"Failed to deserialize Telegram response for method '{context.MethodName}'.", |
| | 1 | 209 | | exception, |
| | 1 | 210 | | context.MethodName, |
| | 1 | 211 | | httpStatusCode: response.StatusCode); |
| | | 212 | | |
| | 1 | 213 | | diagnostics.LogFinalFailure(response.StatusCode, decodeException); |
| | 1 | 214 | | throw decodeException; |
| | | 215 | | } |
| | 53 | 216 | | } |
| | | 217 | | |
| | | 218 | | private Exception CreateUnparsedResponseException( |
| | | 219 | | TelegramRequestExecutionContext context, |
| | | 220 | | TelegramRequestAttemptDiagnostics diagnostics, |
| | | 221 | | TelegramTransportResponse response, |
| | | 222 | | JsonException? parseException) |
| | | 223 | | { |
| | 4 | 224 | | var retryAfterHeaderDelay = TelegramRetryAfterDelayResolver.ResolveDelay(response, envelope: null, _timeProvider |
| | | 225 | | |
| | 4 | 226 | | if (response.StatusCode == 429) |
| | | 227 | | { |
| | 1 | 228 | | var exception = CreateRetryAfterException( |
| | 1 | 229 | | context.MethodName, |
| | 1 | 230 | | response.StatusCode, |
| | 1 | 231 | | envelope: null, |
| | 1 | 232 | | retryAfterHeaderDelay, |
| | 1 | 233 | | GetRetryAfterFailureDescription(retryAfterHeaderDelay)); |
| | | 234 | | |
| | 1 | 235 | | diagnostics.LogFinalFailure(response.StatusCode, exception); |
| | 1 | 236 | | return exception; |
| | | 237 | | } |
| | | 238 | | |
| | 3 | 239 | | var decodeException = new TelegramDecodeException( |
| | 3 | 240 | | $"Failed to parse Telegram API response envelope for method '{context.MethodName}'.", |
| | 3 | 241 | | parseException, |
| | 3 | 242 | | context.MethodName, |
| | 3 | 243 | | httpStatusCode: response.StatusCode, |
| | 3 | 244 | | retryAfterHeaderDelay?.Seconds); |
| | | 245 | | |
| | 3 | 246 | | diagnostics.LogFinalFailure(response.StatusCode, decodeException); |
| | 3 | 247 | | return decodeException; |
| | | 248 | | } |
| | | 249 | | |
| | | 250 | | private Exception CreateApiFailureException( |
| | | 251 | | TelegramRequestExecutionContext context, |
| | | 252 | | TelegramRequestAttemptDiagnostics diagnostics, |
| | | 253 | | TelegramTransportResponse response, |
| | | 254 | | TelegramTransportEnvelope envelope) |
| | | 255 | | { |
| | 21 | 256 | | if (TelegramApiExceptionFactory.IsThrottling(response.StatusCode, envelope)) |
| | | 257 | | { |
| | 5 | 258 | | var retryAfterDelay = TelegramRetryAfterDelayResolver.ResolveDelay(response, envelope, _timeProvider); |
| | 5 | 259 | | var exception = CreateRetryAfterException( |
| | 5 | 260 | | context.MethodName, |
| | 5 | 261 | | response.StatusCode, |
| | 5 | 262 | | envelope, |
| | 5 | 263 | | retryAfterDelay, |
| | 5 | 264 | | GetRetryAfterFailureDescription(retryAfterDelay)); |
| | | 265 | | |
| | 5 | 266 | | diagnostics.LogFinalFailure(response.StatusCode, exception); |
| | 5 | 267 | | return exception; |
| | | 268 | | } |
| | | 269 | | |
| | 16 | 270 | | var apiException = TelegramApiExceptionFactory.CreateApiException( |
| | 16 | 271 | | context.MethodName, |
| | 16 | 272 | | response.StatusCode, |
| | 16 | 273 | | envelope); |
| | | 274 | | |
| | 16 | 275 | | diagnostics.LogFinalFailure(response.StatusCode, apiException); |
| | 16 | 276 | | return apiException; |
| | | 277 | | } |
| | | 278 | | |
| | | 279 | | private static bool IsThrottlingResponse( |
| | | 280 | | int statusCode, |
| | | 281 | | TelegramTransportEnvelope? envelope) |
| | | 282 | | { |
| | 33 | 283 | | return envelope is null |
| | 33 | 284 | | ? statusCode == 429 |
| | 33 | 285 | | : TelegramApiExceptionFactory.IsThrottling(statusCode, envelope); |
| | | 286 | | } |
| | | 287 | | |
| | | 288 | | private static int? GetHttpStatusCode(Exception exception) |
| | | 289 | | { |
| | 4 | 290 | | return exception is TelegramRequestException requestException |
| | 4 | 291 | | ? requestException.HttpStatusCode |
| | 4 | 292 | | : null; |
| | | 293 | | } |
| | | 294 | | |
| | | 295 | | private readonly record struct TelegramRequestExecutionContext( |
| | | 296 | | string MethodName, |
| | | 297 | | bool IsPollingRequest) |
| | | 298 | | { |
| | | 299 | | public static TelegramRequestExecutionContext Create(string methodName) |
| | | 300 | | { |
| | 85 | 301 | | return new TelegramRequestExecutionContext( |
| | 85 | 302 | | methodName, |
| | 85 | 303 | | IsPollingMethod(methodName)); |
| | | 304 | | } |
| | | 305 | | } |
| | | 306 | | |
| | | 307 | | private readonly struct TelegramRequestAttemptDiagnostics |
| | | 308 | | { |
| | | 309 | | private readonly TelegramRequestExecutor _executor; |
| | | 310 | | private readonly TelegramRequestExecutionContext _context; |
| | | 311 | | private readonly int _attempt; |
| | | 312 | | private readonly bool _debugEnabled; |
| | | 313 | | private readonly bool _errorEnabled; |
| | | 314 | | private readonly bool _timingEnabled; |
| | | 315 | | private readonly bool _enabled; |
| | | 316 | | private readonly long _started; |
| | | 317 | | |
| | | 318 | | private TelegramRequestAttemptDiagnostics( |
| | | 319 | | TelegramRequestExecutor executor, |
| | | 320 | | TelegramRequestExecutionContext context, |
| | | 321 | | int attempt, |
| | | 322 | | bool debugEnabled, |
| | | 323 | | bool errorEnabled, |
| | | 324 | | bool timingEnabled) |
| | | 325 | | { |
| | 92 | 326 | | _executor = executor; |
| | 92 | 327 | | _context = context; |
| | 92 | 328 | | _attempt = attempt; |
| | 92 | 329 | | _debugEnabled = debugEnabled; |
| | 92 | 330 | | _errorEnabled = errorEnabled; |
| | 92 | 331 | | _timingEnabled = timingEnabled; |
| | 92 | 332 | | _enabled = debugEnabled || errorEnabled || timingEnabled; |
| | 92 | 333 | | _started = _enabled ? executor._timeProvider.GetTimestamp() : 0; |
| | 92 | 334 | | } |
| | | 335 | | |
| | | 336 | | public static TelegramRequestAttemptDiagnostics Create( |
| | | 337 | | TelegramRequestExecutor executor, |
| | | 338 | | TelegramRequestExecutionContext context, |
| | | 339 | | int attempt) |
| | | 340 | | { |
| | 92 | 341 | | var debugEnabled = !context.IsPollingRequest && executor._logger.IsEnabled(LogLevel.Debug); |
| | 92 | 342 | | var errorEnabled = !context.IsPollingRequest && executor._logger.IsEnabled(LogLevel.Error); |
| | 92 | 343 | | var timingEnabled = !context.IsPollingRequest && TelegramHandlerRequestTimingScope.HasCurrent; |
| | | 344 | | |
| | 92 | 345 | | return new TelegramRequestAttemptDiagnostics( |
| | 92 | 346 | | executor, |
| | 92 | 347 | | context, |
| | 92 | 348 | | attempt, |
| | 92 | 349 | | debugEnabled, |
| | 92 | 350 | | errorEnabled, |
| | 92 | 351 | | timingEnabled); |
| | | 352 | | } |
| | | 353 | | |
| | | 354 | | public void LogStarted(TelegramTransportContent content) |
| | | 355 | | { |
| | 91 | 356 | | if (!_debugEnabled) |
| | | 357 | | { |
| | 81 | 358 | | return; |
| | | 359 | | } |
| | | 360 | | |
| | 10 | 361 | | LogRequestStarted( |
| | 10 | 362 | | _executor._logger, |
| | 10 | 363 | | _context.MethodName, |
| | 10 | 364 | | _attempt, |
| | 10 | 365 | | GetContentKind(content)); |
| | 10 | 366 | | } |
| | | 367 | | |
| | | 368 | | public void LogCompleted(int httpStatusCode) |
| | | 369 | | { |
| | 87 | 370 | | if (!_enabled) |
| | | 371 | | { |
| | 77 | 372 | | return; |
| | | 373 | | } |
| | | 374 | | |
| | 10 | 375 | | var ended = _executor._timeProvider.GetTimestamp(); |
| | 10 | 376 | | RecordTiming(ended); |
| | | 377 | | |
| | 10 | 378 | | if (!_debugEnabled) |
| | | 379 | | { |
| | 1 | 380 | | return; |
| | | 381 | | } |
| | | 382 | | |
| | 9 | 383 | | LogRequestCompleted( |
| | 9 | 384 | | _executor._logger, |
| | 9 | 385 | | _context.MethodName, |
| | 9 | 386 | | _attempt, |
| | 9 | 387 | | httpStatusCode, |
| | 9 | 388 | | _executor.GetElapsedMilliseconds(_started, ended)); |
| | 9 | 389 | | } |
| | | 390 | | |
| | | 391 | | public void LogAttemptFailure( |
| | | 392 | | int? httpStatusCode, |
| | | 393 | | Exception exception) |
| | | 394 | | { |
| | 4 | 395 | | if (!_enabled) |
| | | 396 | | { |
| | 3 | 397 | | return; |
| | | 398 | | } |
| | | 399 | | |
| | 1 | 400 | | var ended = _executor._timeProvider.GetTimestamp(); |
| | 1 | 401 | | RecordTiming(ended); |
| | | 402 | | |
| | 1 | 403 | | if (_errorEnabled) |
| | | 404 | | { |
| | 1 | 405 | | _executor.LogRequestFailure( |
| | 1 | 406 | | _context.MethodName, |
| | 1 | 407 | | _attempt, |
| | 1 | 408 | | httpStatusCode, |
| | 1 | 409 | | _started, |
| | 1 | 410 | | ended, |
| | 1 | 411 | | exception); |
| | | 412 | | } |
| | 1 | 413 | | } |
| | | 414 | | |
| | | 415 | | public void LogFinalFailure( |
| | | 416 | | int httpStatusCode, |
| | | 417 | | Exception exception) |
| | | 418 | | { |
| | 26 | 419 | | if (!_errorEnabled) |
| | | 420 | | { |
| | 24 | 421 | | return; |
| | | 422 | | } |
| | | 423 | | |
| | 2 | 424 | | _executor.LogRequestFailure( |
| | 2 | 425 | | _context.MethodName, |
| | 2 | 426 | | _attempt, |
| | 2 | 427 | | httpStatusCode, |
| | 2 | 428 | | _started, |
| | 2 | 429 | | _executor._timeProvider.GetTimestamp(), |
| | 2 | 430 | | exception); |
| | 2 | 431 | | } |
| | | 432 | | |
| | | 433 | | public void LogThrottled(TelegramRetryAfterDelay retryAfter) |
| | | 434 | | { |
| | 8 | 435 | | if (_context.IsPollingRequest || |
| | 8 | 436 | | !_executor._logger.IsEnabled(LogLevel.Warning)) |
| | | 437 | | { |
| | 7 | 438 | | return; |
| | | 439 | | } |
| | | 440 | | |
| | 1 | 441 | | _executor.LogRequestThrottled( |
| | 1 | 442 | | _context.MethodName, |
| | 1 | 443 | | _attempt, |
| | 1 | 444 | | retryAfter); |
| | 1 | 445 | | } |
| | | 446 | | |
| | | 447 | | private void RecordTiming(long ended) |
| | | 448 | | { |
| | 11 | 449 | | if (_timingEnabled) |
| | | 450 | | { |
| | 4 | 451 | | TelegramHandlerRequestTimingScope.Record(_started, ended); |
| | | 452 | | } |
| | 11 | 453 | | } |
| | | 454 | | } |
| | | 455 | | |
| | | 456 | | private static bool IsPollingMethod(string methodName) |
| | | 457 | | { |
| | 85 | 458 | | return string.Equals(methodName, "getUpdates", StringComparison.Ordinal); |
| | | 459 | | } |
| | | 460 | | |
| | | 461 | | private bool CanRetryAfter(int attempt, TelegramRetryAfterDelay retryAfter) |
| | | 462 | | { |
| | 12 | 463 | | return _retryAfterPolicy.Enabled && |
| | 12 | 464 | | attempt <= _retryAfterPolicy.MaxRetries && |
| | 12 | 465 | | retryAfter.Value <= _retryAfterPolicy.MaxDelay; |
| | | 466 | | } |
| | | 467 | | |
| | | 468 | | private static TelegramRetryAfterException CreateRetryAfterException( |
| | | 469 | | string methodName, |
| | | 470 | | int httpStatusCode, |
| | | 471 | | TelegramTransportEnvelope? envelope, |
| | | 472 | | TelegramRetryAfterDelay? retryAfter, |
| | | 473 | | string fallbackDescription) |
| | | 474 | | { |
| | 6 | 475 | | var description = envelope?.Description ?? fallbackDescription; |
| | 6 | 476 | | var message = $"Telegram request '{methodName}' failed: {description}"; |
| | | 477 | | |
| | 6 | 478 | | return new TelegramRetryAfterException( |
| | 6 | 479 | | message, |
| | 6 | 480 | | methodName, |
| | 6 | 481 | | httpStatusCode: httpStatusCode, |
| | 6 | 482 | | telegramErrorCode: envelope?.ErrorCode, |
| | 6 | 483 | | description: description, |
| | 6 | 484 | | retryAfterSeconds: retryAfter?.Seconds); |
| | | 485 | | } |
| | | 486 | | |
| | | 487 | | private static string GetRetryAfterFailureDescription(TelegramRetryAfterDelay? retryAfter) |
| | | 488 | | { |
| | 6 | 489 | | return retryAfter is null |
| | 6 | 490 | | ? "Telegram throttling response did not provide retry timing metadata." |
| | 6 | 491 | | : "Telegram request was throttled and automatic retry-after handling was not applied by the configured polic |
| | | 492 | | } |
| | | 493 | | |
| | | 494 | | private static string GetContentKind(TelegramTransportContent content) |
| | | 495 | | { |
| | 10 | 496 | | return content switch |
| | 10 | 497 | | { |
| | 10 | 498 | | TelegramJsonTransportContent => "json", |
| | 0 | 499 | | TelegramMultipartTransportContent => "multipart", |
| | 0 | 500 | | _ => content.GetType().Name |
| | 10 | 501 | | }; |
| | | 502 | | } |
| | | 503 | | |
| | | 504 | | private void LogRequestThrottled( |
| | | 505 | | string methodName, |
| | | 506 | | int attempt, |
| | | 507 | | TelegramRetryAfterDelay retryAfter) |
| | | 508 | | { |
| | 1 | 509 | | LogRequestThrottledCore( |
| | 1 | 510 | | _logger, |
| | 1 | 511 | | methodName, |
| | 1 | 512 | | attempt, |
| | 1 | 513 | | retryAfter.Value); |
| | 1 | 514 | | } |
| | | 515 | | |
| | | 516 | | private void LogRequestFailure( |
| | | 517 | | string methodName, |
| | | 518 | | int attempt, |
| | | 519 | | int? httpStatusCode, |
| | | 520 | | long attemptStarted, |
| | | 521 | | long attemptEnded, |
| | | 522 | | Exception exception) |
| | | 523 | | { |
| | 3 | 524 | | LogRequestFailed( |
| | 3 | 525 | | _logger, |
| | 3 | 526 | | exception, |
| | 3 | 527 | | methodName, |
| | 3 | 528 | | attempt, |
| | 3 | 529 | | httpStatusCode, |
| | 3 | 530 | | GetElapsedMilliseconds(attemptStarted, attemptEnded), |
| | 3 | 531 | | exception.GetType().FullName ?? exception.GetType().Name); |
| | 3 | 532 | | } |
| | | 533 | | |
| | | 534 | | private double GetElapsedMilliseconds(long startingTimestamp, long endingTimestamp) |
| | | 535 | | { |
| | 12 | 536 | | return _timeProvider.GetElapsedTime(startingTimestamp, endingTimestamp).TotalMilliseconds; |
| | | 537 | | } |
| | | 538 | | |
| | | 539 | | [LoggerMessage( |
| | | 540 | | EventId = RequestStartedEventId, |
| | | 541 | | Level = LogLevel.Debug, |
| | | 542 | | Message = "Telegram request started. method={MethodName}, attempt={Attempt}, content={ContentKind}.")] |
| | | 543 | | private static partial void LogRequestStarted( |
| | | 544 | | ILogger logger, |
| | | 545 | | string methodName, |
| | | 546 | | int attempt, |
| | | 547 | | string contentKind); |
| | | 548 | | |
| | | 549 | | [LoggerMessage( |
| | | 550 | | EventId = RequestCompletedEventId, |
| | | 551 | | Level = LogLevel.Debug, |
| | | 552 | | Message = "Telegram request completed. method={MethodName}, attempt={Attempt}, status={HttpStatusCode}, request_ |
| | | 553 | | private static partial void LogRequestCompleted( |
| | | 554 | | ILogger logger, |
| | | 555 | | string methodName, |
| | | 556 | | int attempt, |
| | | 557 | | int httpStatusCode, |
| | | 558 | | double requestElapsedMilliseconds); |
| | | 559 | | |
| | | 560 | | [LoggerMessage( |
| | | 561 | | EventId = RequestThrottledEventId, |
| | | 562 | | Level = LogLevel.Warning, |
| | | 563 | | Message = "Telegram request throttled. method={MethodName}, attempt={Attempt}, retry_after={RetryAfter}.")] |
| | | 564 | | private static partial void LogRequestThrottledCore( |
| | | 565 | | ILogger logger, |
| | | 566 | | string methodName, |
| | | 567 | | int attempt, |
| | | 568 | | TimeSpan retryAfter); |
| | | 569 | | |
| | | 570 | | [LoggerMessage( |
| | | 571 | | EventId = RequestFailedEventId, |
| | | 572 | | Level = LogLevel.Error, |
| | | 573 | | Message = "Telegram request failed. method={MethodName}, attempt={Attempt}, status={HttpStatusCode}, request_ms= |
| | | 574 | | private static partial void LogRequestFailed( |
| | | 575 | | ILogger logger, |
| | | 576 | | Exception exception, |
| | | 577 | | string methodName, |
| | | 578 | | int attempt, |
| | | 579 | | int? httpStatusCode, |
| | | 580 | | double requestElapsedMilliseconds, |
| | | 581 | | string exceptionType); |
| | | 582 | | } |