Skip to content

Commit fdd5af3

Browse files
authored
Use PipeWriter instead of ArrayWriter when writing to outputStream (#8476)
1 parent ffe3549 commit fdd5af3

File tree

1 file changed

+11
-23
lines changed

1 file changed

+11
-23
lines changed

src/HotChocolate/Core/src/Execution/Serialization/JsonResultFormatter.cs

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Buffers;
22
using System.Collections;
3+
using System.IO.Pipelines;
34
using System.Runtime.CompilerServices;
45
using System.Text;
56
using System.Text.Json;
@@ -226,29 +227,24 @@ private async ValueTask FormatInternalAsync(
226227
Stream outputStream,
227228
CancellationToken cancellationToken = default)
228229
{
229-
using var buffer = new ArrayWriter();
230-
FormatInternal(result, buffer);
231-
232-
await outputStream
233-
.WriteAsync(buffer.GetWrittenMemory(), cancellationToken)
234-
.ConfigureAwait(false);
235-
236-
await outputStream.FlushAsync(cancellationToken).ConfigureAwait(false);
230+
var writer = PipeWriter.Create(outputStream, new StreamPipeWriterOptions(leaveOpen: true));
231+
FormatInternal(result, writer);
232+
await writer.CompleteAsync().ConfigureAwait(false);
237233
}
238234

239235
private async ValueTask FormatInternalAsync(
240236
OperationResultBatch resultBatch,
241237
Stream outputStream,
242238
CancellationToken cancellationToken = default)
243239
{
244-
using var buffer = new ArrayWriter();
240+
var writer = PipeWriter.Create(outputStream, new StreamPipeWriterOptions(leaveOpen: true));
245241

246242
foreach (var result in resultBatch.Results)
247243
{
248244
switch (result)
249245
{
250246
case IOperationResult singleResult:
251-
FormatInternal(singleResult, buffer);
247+
FormatInternal(singleResult, writer);
252248
break;
253249

254250
case IResponseStream batchResult:
@@ -259,7 +255,7 @@ private async ValueTask FormatInternalAsync(
259255
{
260256
try
261257
{
262-
FormatInternal(partialResult, buffer);
258+
FormatInternal(partialResult, writer);
263259
}
264260
finally
265261
{
@@ -271,39 +267,31 @@ private async ValueTask FormatInternalAsync(
271267
}
272268
}
273269

274-
await outputStream
275-
.WriteAsync(buffer.GetWrittenMemory(), cancellationToken)
276-
.ConfigureAwait(false);
277-
278-
await outputStream.FlushAsync(cancellationToken).ConfigureAwait(false);
270+
await writer.CompleteAsync().ConfigureAwait(false);
279271
}
280272

281273
private async ValueTask FormatInternalAsync(
282274
IResponseStream batchResult,
283275
Stream outputStream,
284276
CancellationToken cancellationToken = default)
285277
{
286-
using var buffer = new ArrayWriter();
278+
var writer = PipeWriter.Create(outputStream, new StreamPipeWriterOptions(leaveOpen: true));
287279

288280
await foreach (var partialResult in batchResult.ReadResultsAsync()
289281
.WithCancellation(cancellationToken)
290282
.ConfigureAwait(false))
291283
{
292284
try
293285
{
294-
FormatInternal(partialResult, buffer);
286+
FormatInternal(partialResult, writer);
295287
}
296288
finally
297289
{
298290
await partialResult.DisposeAsync().ConfigureAwait(false);
299291
}
300292
}
301293

302-
await outputStream
303-
.WriteAsync(buffer.GetWrittenMemory(), cancellationToken)
304-
.ConfigureAwait(false);
305-
306-
await outputStream.FlushAsync(cancellationToken).ConfigureAwait(false);
294+
await writer.CompleteAsync().ConfigureAwait(false);
307295
}
308296

309297
private void WriteResult(Utf8JsonWriter writer, IOperationResult result)

0 commit comments

Comments
 (0)