示例#1
0
        public async Task Invoke(HttpContext context, IBeatPulseService pulseService)
        {
            var request = context.Request;

            if (!IsBeatPulseRequest(request, out string beatPulsePath))
            {
                await _next.Invoke(context);

                return;
            }
            else
            {
                if (TryFromCache(beatPulsePath, out OutputMessage output))
                {
                    await WriteResponseAsync(request.HttpContext, output, _options.DetailedOutput);
                }
                else
                {
                    output = new OutputMessage();

                    using (var cancellationTokenSource = new CancellationTokenSource())
                    {
                        var task = pulseService.IsHealthy(beatPulsePath, context, cancellationTokenSource.Token);

                        if (await Task.WhenAny(task, Task.Delay(_options.Timeout, cancellationTokenSource.Token)) == task)
                        {
                            var responses = await task;

                            if (!responses.Any())
                            {
                                await WriteNotFoundAsync(request.HttpContext, _options.DetailedOutput);

                                return;
                            }

                            output.AddHealthCheckMessages(responses);
                            output.EndAtUtc = DateTime.UtcNow;

                            if (_options.CacheMode.UseServerMemory() && _options.CacheOutput)
                            {
                                _cache.TryAdd(beatPulsePath, output);
                            }

                            await WriteResponseAsync(request.HttpContext, output, _options.DetailedOutput);
                        }
                        else
                        {
                            //timeout
                            _cache.TryRemove(beatPulsePath, out OutputMessage removed);

                            cancellationTokenSource.Cancel();

                            await WriteTimeoutAsync(request.HttpContext, _options.DetailedOutput);
                        }
                    }
                }
            }
        }
        public async Task Invoke(HttpContext context, IBeatPulseService pulseService)
        {
            var request = context.Request;

            if (!IsBeatPulseRequest(request, out string beatPulsePath))
            {
                await _next.Invoke(context);

                return;
            }

            if (!await IsAuthenticatedRequest(context))
            {
                context.Response.StatusCode = StatusCodes.Status401Unauthorized;
                return;
            }

            if (TryFromCache(beatPulsePath, out OutputLivenessMessage output))
            {
                await request.HttpContext
                .Response
                .WriteLivenessMessage(_options, output);

                return;
            }

            output = new OutputLivenessMessage();

            var responses = await pulseService.IsHealthy(beatPulsePath, _options, context);

            if (!responses.Any())
            {
                // beat pulse path is not valid across any liveness
                // return unavailable with not found reason.
                output.SetNotFound();
            }
            else
            {
                // beat pulse is executed, set response
                // messages and add to cache if is configured.
                output.AddHealthCheckMessages(responses);
                output.SetExecuted();

                if (_options.CacheMode.UseServerMemory() && _options.CacheOutput)
                {
                    _cache.TryAdd(beatPulsePath, output);
                }
            }

            await request.HttpContext
            .Response
            .WriteLivenessMessage(_options, output);
        }