Using FastEndpoints is there a way to set a timeout for all endpoints

217 views Asked by At

Does FastEndpoints fast-endpoints have a request timeout. I see the cancellationToken firing but not sure why (it might be a client disconnection)

What I would like to know is there a way to set some sort of timeout for all endpoints.

1

There are 1 answers

1
Dĵ ΝιΓΞΗΛψΚ On

the cancellation token passed in to FE's endpoint handler is the exact same token that the asp.net middleware hands down to minimal api endpoints. for example, it's the same token as this:

app.MapGet("/test", (CancellationToken ct) => { });

it might be a client disconnection

yes, it might be client disconnection log entries you're seeing.

typically, we use an IEndpointFilter like the following to catch/suppress OperationCancelledExceptions that are logged by kestrel.

sealed class OperationCancelledFilter : IEndpointFilter
{
    private readonly ILogger<OperationCancelledFilter> logger;

    public OperationCancelledFilter(ILogger<OperationCancelledFilter> logger)
    {
        this.logger = logger;
    }

    public async ValueTask<object?> InvokeAsync(EndpointFilterInvocationContext context, EndpointFilterDelegate next)
    {
        try
        {
            return await next(context);
        }
        catch (OperationCanceledException x)
        {
            logger.LogDebug(x, "Request was cancelled!");
            return Results.StatusCode(499);
        }
    }
}

register it as a global endpoint filter:

app.UseFastEndpoints(c => c.Endpoints.Configurator = ep =>
{
    ep.Options(b => b.AddEndpointFilter<OperationCancelledFilter>());
});

if the above doesn't do it, do update your question with the log entries/exception message you're seeing.