2020-07-14 11:26:47 +00:00
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.Threading.Tasks;
|
2020-09-10 12:16:41 +00:00
|
|
|
|
using MediaBrowser.Common.Extensions;
|
2020-07-14 11:26:47 +00:00
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Http.Extensions;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
2023-01-14 02:37:37 +00:00
|
|
|
|
namespace Jellyfin.Api.Middleware
|
2020-07-14 11:26:47 +00:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Response time middleware.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class ResponseTimeMiddleware
|
|
|
|
|
{
|
|
|
|
|
private const string ResponseHeaderResponseTime = "X-Response-Time-ms";
|
|
|
|
|
|
|
|
|
|
private readonly RequestDelegate _next;
|
|
|
|
|
private readonly ILogger<ResponseTimeMiddleware> _logger;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the <see cref="ResponseTimeMiddleware"/> class.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="next">Next request delegate.</param>
|
|
|
|
|
/// <param name="logger">Instance of the <see cref="ILogger{ExceptionMiddleware}"/> interface.</param>
|
|
|
|
|
public ResponseTimeMiddleware(
|
|
|
|
|
RequestDelegate next,
|
2022-06-13 22:27:38 +00:00
|
|
|
|
ILogger<ResponseTimeMiddleware> logger)
|
2020-07-14 11:26:47 +00:00
|
|
|
|
{
|
|
|
|
|
_next = next;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Invoke request.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="context">Request context.</param>
|
2022-06-13 22:27:38 +00:00
|
|
|
|
/// <param name="serverConfigurationManager">Instance of the <see cref="IServerConfigurationManager"/> interface.</param>
|
2020-07-14 11:26:47 +00:00
|
|
|
|
/// <returns>Task.</returns>
|
2022-06-13 22:27:38 +00:00
|
|
|
|
public async Task Invoke(HttpContext context, IServerConfigurationManager serverConfigurationManager)
|
2020-07-14 11:26:47 +00:00
|
|
|
|
{
|
2022-12-14 21:03:26 +00:00
|
|
|
|
var startTimestamp = Stopwatch.GetTimestamp();
|
|
|
|
|
|
2022-06-13 22:27:38 +00:00
|
|
|
|
var enableWarning = serverConfigurationManager.Configuration.EnableSlowResponseWarning;
|
|
|
|
|
var warningThreshold = serverConfigurationManager.Configuration.SlowResponseThresholdMs;
|
2020-07-14 11:26:47 +00:00
|
|
|
|
context.Response.OnStarting(() =>
|
|
|
|
|
{
|
2022-12-14 21:03:26 +00:00
|
|
|
|
var responseTime = Stopwatch.GetElapsedTime(startTimestamp);
|
|
|
|
|
var responseTimeMs = responseTime.TotalMilliseconds;
|
|
|
|
|
if (enableWarning && responseTimeMs > warningThreshold && _logger.IsEnabled(LogLevel.Debug))
|
2022-06-13 22:27:38 +00:00
|
|
|
|
{
|
2022-11-30 21:07:34 +00:00
|
|
|
|
_logger.LogDebug(
|
2022-06-13 22:27:38 +00:00
|
|
|
|
"Slow HTTP Response from {Url} to {RemoteIp} in {Elapsed:g} with Status Code {StatusCode}",
|
|
|
|
|
context.Request.GetDisplayUrl(),
|
|
|
|
|
context.GetNormalizedRemoteIp(),
|
2022-12-14 21:03:26 +00:00
|
|
|
|
responseTime,
|
2022-06-13 22:27:38 +00:00
|
|
|
|
context.Response.StatusCode);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-14 21:03:26 +00:00
|
|
|
|
context.Response.Headers[ResponseHeaderResponseTime] = responseTimeMs.ToString(CultureInfo.InvariantCulture);
|
2020-07-14 11:26:47 +00:00
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Call the next delegate/middleware in the pipeline
|
|
|
|
|
await this._next(context).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|