public async Task InvokeAsync(Context context, RequestProcessorCallback next) { context.TraceIdentifier = Interlocked.Increment(ref s_nextTraceId).ToString(); var requestScope = new { TraceId = context.TraceIdentifier, Name = context.RequestName.Substring(context.RequestName.LastIndexOf('.') + 1), Body = GetBodyAsJson(context.Request) }; using (_logger.BeginScope(requestScope)) { var opts = context.RequestMetadata.GetAttributes <OptionAttribute>() .Select(attr => (attr.Key, attr.Value)) .ToList(); _logger.Log(LogLevel.Debug, "starting with opts: {0}", opts); try { await next(context); } catch (OperationCanceledException e) { _logger.Log(LogLevel.Warning, e, "canceled"); } catch (Exception e) { _logger.Log(LogLevel.Error, "crashed: {0}", e.Message); } } }
public async Task InvokeAsync(Context context, RequestProcessorCallback next) { var semaphores = GetSemaphoresOrdered(context).ToArray(); if (semaphores.Length == 0) { await next(context); } else { foreach (var semaphore in semaphores) { await semaphore.WaitAsync(); } try { await next(context); } finally { foreach (var semaphore in semaphores.Reverse()) { semaphore.Release(); } } } }
public async Task InvokeAsync(Context context, RequestProcessorCallback next) { int attempt = 0; do { try { await next(context); break; } catch (TaskCanceledException) { throw; } catch (OperationCanceledException) { throw; } catch (Exception e) { if (attempt++ >= _retryPolicy.RetryCount) { throw; } if (!_retryPolicy.RetryOnError(attempt, e)) { throw; } _logger.WillRetry(context, attempt, e); await Task.Delay(_retryPolicy.GetSleepDuration(attempt, e)); } } while (true); }
public Task InvokeAsync(Context context, RequestProcessorCallback next) { var formatter = ResolveFormatter(context.Request.ContentType); context.Request.Items[typeof(IRequestContentFormatter)] = formatter; context.Items[typeof(IRequestContentFormatter)] = formatter; return(next(context)); }
public Task InvokeAsync(Context context, RequestProcessorCallback next) { if (_predicate(context.Request)) { return(next(context)); } _logger.RequestFiltered(context); return(Task.CompletedTask); }
public RequestProcessor(IEnumerable <IRequestProcessorMiddleware> middlewares, IRequestProcessor endware) { if (middlewares == null) { throw new ArgumentNullException(nameof(middlewares)); } if (endware == null) { throw new ArgumentNullException(nameof(endware)); } _next = BuildChain(new Queue <IRequestProcessorMiddleware>(middlewares), endware); }
private static async Task MetricsMiddleware(Context ctx, RequestProcessorCallback next, ILogger logger) { var sw = new Stopwatch(); sw.Start(); try { await next(ctx); } finally { sw.Stop(); logger.LogInformation("Elapsed: {0}", sw.Elapsed); } }
public async Task InvokeAsync(Context context, RequestProcessorCallback next) { var timeout = GetTimeout(context); if (timeout != null && timeout > TimeSpan.Zero) { var watchdogTokenSource = new CancellationTokenSource(timeout.Value); var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource( watchdogTokenSource.Token, context.Aborted); context.Aborted = linkedTokenSource.Token; _logger.TimeoutConfigured(context, timeout.Value); } await next(context); }
private static async Task InvokeAsync(ThrottleOptions options) { var middleware = new ThrottleMiddleware(options); RequestProcessorCallback next = async context => { SimpleLock.Take(); await Task.Delay(100); SimpleLock.Release(); }; var requests = Enumerable .Repeat(new Context(new FakeRequest()), 10) .Select(ctx => Task.Run(() => middleware.InvokeAsync(ctx, next))); await Task.WhenAll(requests); }
public async Task InvokeAsync(Context context, RequestProcessorCallback next) { var debounceAttribute = context.RequestMetadata.GetAttribute <DebounceAttribute>(); if (debounceAttribute != null) { var lastRun = await _lastRunStore.GetLastRunAsync(context.Request, context.Aborted); if (context.Request.Created < lastRun) { _logger.RequestDebounced(context); return; } await next(context); await _lastRunStore.SetLastRunAsync(context.Request); } else { await next(context); } }
/// <inheritdoc /> public Task InvokeAsync(Context context, RequestProcessorCallback next) { return(Task.CompletedTask); }
/// <inheritdoc /> public Task InvokeAsync(Context context, RequestProcessorCallback next) { return(_func(context, next)); }