Skip to content

Commit

Permalink
Abandon Response Formatting Gracefully on Cancellation
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib committed Feb 27, 2025
1 parent 60d8bc1 commit 287d6cb
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,23 @@ public async ValueTask FormatAsync(
throw ThrowHelper.Formatter_InvalidAcceptMediaType();
}

try
{
await FormatInternalAsync(response, result, proposedStatusCode, format, cancellationToken);
}
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
// if the request is aborted we will fail gracefully.
}
}

private async ValueTask FormatInternalAsync(
HttpResponse response,
IExecutionResult result,
HttpStatusCode? proposedStatusCode,
FormatInfo format,
CancellationToken cancellationToken)
{
switch (result)
{
case IOperationResult operationResult:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,11 @@ private async ValueTask FormatOperationResultAsync(
MessageHelper.WriteNextMessage(_payloadFormatter, operationResult, buffer);
MessageHelper.WriteCompleteMessage(buffer);

await outputStream.WriteAsync(buffer.GetInternalBuffer(), 0, buffer.Length, ct).ConfigureAwait(false);
await outputStream.FlushAsync(ct).ConfigureAwait(false);
if (!ct.IsCancellationRequested)
{
await outputStream.WriteAsync(buffer.GetInternalBuffer(), 0, buffer.Length, ct).ConfigureAwait(false);
await outputStream.FlushAsync(ct).ConfigureAwait(false);
}
}
catch (Exception ex)
{
Expand Down Expand Up @@ -111,8 +114,14 @@ private async ValueTask FormatResultBatchAsync(
{
buffer ??= new ArrayWriter();
MessageHelper.WriteNextMessage(_payloadFormatter, operationResult, buffer);

writer.Write(buffer.GetWrittenSpan());
await writer.FlushAsync(ct).ConfigureAwait(false);

if (!ct.IsCancellationRequested)
{
await writer.FlushAsync(ct).ConfigureAwait(false);
}

keepAlive?.Reset();
buffer.Reset();
}
Expand Down Expand Up @@ -148,8 +157,11 @@ private async ValueTask FormatResultBatchAsync(
buffer?.Dispose();
}

MessageHelper.WriteCompleteMessage(writer);
await writer.FlushAsync(ct).ConfigureAwait(false);
if (!ct.IsCancellationRequested)
{
MessageHelper.WriteCompleteMessage(writer);
await writer.FlushAsync(ct).ConfigureAwait(false);
}
}

private async ValueTask FormatResponseStreamAsync(
Expand All @@ -165,8 +177,11 @@ private async ValueTask FormatResponseStreamAsync(
await formatter.ProcessAsync(ct).ConfigureAwait(false);
}

MessageHelper.WriteCompleteMessage(writer);
await writer.FlushAsync(ct).ConfigureAwait(false);
if (!ct.IsCancellationRequested)
{
MessageHelper.WriteCompleteMessage(writer);
await writer.FlushAsync(ct).ConfigureAwait(false);
}
}

private sealed class StreamFormatter(
Expand Down Expand Up @@ -207,6 +222,12 @@ public async Task ProcessAsync(CancellationToken ct)
}
}
}
catch (OperationCanceledException)
{
// if the operation was canceled we do not need to log this
// and will stop gracefully.
return;
}
finally
{
await responseStream.DisposeAsync().ConfigureAwait(false);
Expand Down

0 comments on commit 287d6cb

Please sign in to comment.