fix(inbound-api): endpoint filter hot-reads MaxRequestBodyBytes via IOptionsMonitor
Claude-Session: https://claude.ai/code/session_01MtdgwpEeCUn6cUA5f1LMPj
This commit is contained in:
@@ -24,17 +24,17 @@ namespace ZB.MOM.WW.ScadaBridge.InboundAPI;
|
|||||||
public sealed class InboundApiEndpointFilter : IEndpointFilter
|
public sealed class InboundApiEndpointFilter : IEndpointFilter
|
||||||
{
|
{
|
||||||
private readonly ILogger<InboundApiEndpointFilter> _logger;
|
private readonly ILogger<InboundApiEndpointFilter> _logger;
|
||||||
private readonly InboundApiOptions _options;
|
private readonly IOptionsMonitor<InboundApiOptions> _options;
|
||||||
|
|
||||||
/// <summary>Initializes a new <see cref="InboundApiEndpointFilter"/> with the given logger and options.</summary>
|
/// <summary>Initializes a new <see cref="InboundApiEndpointFilter"/> with the given logger and options.</summary>
|
||||||
/// <param name="logger">Logger for request rejection diagnostics.</param>
|
/// <param name="logger">Logger for request rejection diagnostics.</param>
|
||||||
/// <param name="options">Inbound API options including the maximum request body size.</param>
|
/// <param name="options">Live-reloadable inbound API options, read per-request.</param>
|
||||||
public InboundApiEndpointFilter(
|
public InboundApiEndpointFilter(
|
||||||
ILogger<InboundApiEndpointFilter> logger,
|
ILogger<InboundApiEndpointFilter> logger,
|
||||||
IOptions<InboundApiOptions> options)
|
IOptionsMonitor<InboundApiOptions> options)
|
||||||
{
|
{
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_options = options.Value;
|
_options = options;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Applies active-node gating and request body size checks before delegating to the next filter or handler.</summary>
|
/// <summary>Applies active-node gating and request body size checks before delegating to the next filter or handler.</summary>
|
||||||
@@ -63,7 +63,7 @@ public sealed class InboundApiEndpointFilter : IEndpointFilter
|
|||||||
// Cap the request body size. Reject an over-limit body up
|
// Cap the request body size. Reject an over-limit body up
|
||||||
// front via Content-Length; also lower the per-request max body size so a
|
// front via Content-Length; also lower the per-request max body size so a
|
||||||
// chunked/unknown-length stream is cut off by Kestrel as it is read.
|
// chunked/unknown-length stream is cut off by Kestrel as it is read.
|
||||||
var maxBytes = _options.MaxRequestBodyBytes;
|
var maxBytes = _options.CurrentValue.MaxRequestBodyBytes;
|
||||||
if (httpContext.Request.ContentLength is { } declaredLength && declaredLength > maxBytes)
|
if (httpContext.Request.ContentLength is { } declaredLength && declaredLength > maxBytes)
|
||||||
{
|
{
|
||||||
_logger.LogWarning(
|
_logger.LogWarning(
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ public class EndpointGatingTests
|
|||||||
{
|
{
|
||||||
private static InboundApiEndpointFilter CreateFilter(InboundApiOptions? options = null) =>
|
private static InboundApiEndpointFilter CreateFilter(InboundApiOptions? options = null) =>
|
||||||
new(NullLogger<InboundApiEndpointFilter>.Instance,
|
new(NullLogger<InboundApiEndpointFilter>.Instance,
|
||||||
Options.Create(options ?? new InboundApiOptions()));
|
new TestOptionsMonitor<InboundApiOptions>(options ?? new InboundApiOptions()));
|
||||||
|
|
||||||
private static (DefaultHttpContext ctx, DefaultEndpointFilterInvocationContext invocation)
|
private static (DefaultHttpContext ctx, DefaultEndpointFilterInvocationContext invocation)
|
||||||
BuildInvocation(bool? activeNode, long? contentLength)
|
BuildInvocation(bool? activeNode, long? contentLength)
|
||||||
@@ -144,4 +144,37 @@ public class EndpointGatingTests
|
|||||||
public bool IsReadOnly => false;
|
public bool IsReadOnly => false;
|
||||||
public long? MaxRequestBodySize { get; set; }
|
public long? MaxRequestBodySize { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// --- InboundAPI hot-reload: options must be re-read per request, not cached at ctor time ---
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task BodySizeCap_HotReloads_WithoutRestart()
|
||||||
|
{
|
||||||
|
var monitor = new TestOptionsMonitor<InboundApiOptions>(new InboundApiOptions { MaxRequestBodyBytes = 10 });
|
||||||
|
var filter = new InboundApiEndpointFilter(NullLogger<InboundApiEndpointFilter>.Instance, monitor);
|
||||||
|
|
||||||
|
var (_, firstInvocation) = BuildInvocation(activeNode: true, contentLength: 20);
|
||||||
|
var firstNext = NextSentinel(out var firstHandlerRan);
|
||||||
|
var firstResult = await filter.InvokeAsync(firstInvocation, firstNext);
|
||||||
|
|
||||||
|
Assert.False(firstHandlerRan());
|
||||||
|
var firstStatus = Assert.IsAssignableFrom<IStatusCodeHttpResult>(firstResult);
|
||||||
|
Assert.Equal(StatusCodes.Status413PayloadTooLarge, firstStatus.StatusCode);
|
||||||
|
|
||||||
|
monitor.CurrentValue = new InboundApiOptions { MaxRequestBodyBytes = 1024 * 1024 };
|
||||||
|
|
||||||
|
var (_, secondInvocation) = BuildInvocation(activeNode: true, contentLength: 20);
|
||||||
|
var secondNext = NextSentinel(out var secondHandlerRan);
|
||||||
|
await filter.InvokeAsync(secondInvocation, secondNext);
|
||||||
|
|
||||||
|
Assert.True(secondHandlerRan());
|
||||||
|
}
|
||||||
|
|
||||||
|
private sealed class TestOptionsMonitor<T> : IOptionsMonitor<T>
|
||||||
|
{
|
||||||
|
public TestOptionsMonitor(T currentValue) => CurrentValue = currentValue;
|
||||||
|
public T CurrentValue { get; set; }
|
||||||
|
public T Get(string? name) => CurrentValue;
|
||||||
|
public IDisposable? OnChange(Action<T, string?> listener) => null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user