-
-
Notifications
You must be signed in to change notification settings - Fork 803
Properly handle parsing of large request documents #9133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| using System.Buffers; | ||
|
|
||
| namespace HotChocolate.Language; | ||
|
|
||
| /// <summary> | ||
|
|
@@ -21,4 +23,11 @@ public interface IDocumentHashProvider | |
| /// <param name="document">The GraphQL operation document.</param> | ||
| /// <returns>The hash of the GraphQL operation document.</returns> | ||
| OperationDocumentHash ComputeHash(ReadOnlySpan<byte> document); | ||
|
|
||
| /// <summary> | ||
| /// Computes the hash of a GraphQL operation document. | ||
| /// </summary> | ||
| /// <param name="document">The GraphQL operation document.</param> | ||
| /// <returns>The hash of the GraphQL operation document.</returns> | ||
| OperationDocumentHash ComputeHash(ReadOnlySequence<byte> document); | ||
|
Comment on lines
25
to
+32
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -166,7 +166,8 @@ private readonly GraphQLRequest ParseRequest(ref Utf8JsonReader reader, Operatio | |
| ErrorHandlingMode? errorHandlingMode = null; | ||
| JsonDocument? variables = null; | ||
| JsonDocument? extensions = null; | ||
| ReadOnlySpan<byte> documentBody = default; | ||
| ReadOnlySequence<byte> documentSequence = default; | ||
| ReadOnlySpan<byte> documentSpan = default; | ||
| var isDocumentEscaped = false; | ||
|
|
||
| while (reader.Read() && reader.TokenType != JsonTokenType.EndObject) | ||
|
|
@@ -183,7 +184,15 @@ private readonly GraphQLRequest ParseRequest(ref Utf8JsonReader reader, Operatio | |
| if (reader.TokenType == JsonTokenType.String) | ||
| { | ||
| isDocumentEscaped = reader.ValueIsEscaped; | ||
| documentBody = reader.ValueSpan; | ||
|
|
||
| if (reader.HasValueSequence) | ||
| { | ||
| documentSequence = reader.ValueSequence; | ||
| } | ||
| else | ||
| { | ||
| documentSpan = reader.ValueSpan; | ||
| } | ||
| } | ||
| else if (reader.TokenType == JsonTokenType.Null) | ||
| { | ||
|
|
@@ -312,7 +321,7 @@ private readonly GraphQLRequest ParseRequest(ref Utf8JsonReader reader, Operatio | |
| } | ||
|
|
||
| // Handle persisted queries via extensions | ||
| if (documentBody.IsEmpty | ||
| if (documentSpan.IsEmpty | ||
| && documentId.IsEmpty | ||
| && _useCache | ||
| && extensions is not null | ||
|
|
@@ -323,10 +332,19 @@ private readonly GraphQLRequest ParseRequest(ref Utf8JsonReader reader, Operatio | |
| } | ||
|
|
||
| // Parse the GraphQL document if provided | ||
| if (!documentBody.IsEmpty) | ||
| if (!documentSpan.IsEmpty) | ||
| { | ||
| ParseDocument( | ||
| documentBody, | ||
| documentSpan, | ||
| isDocumentEscaped, | ||
| ref document, | ||
| ref documentHash, | ||
| ref documentId); | ||
| } | ||
| else if (!documentSequence.IsEmpty) | ||
| { | ||
| ParseDocument( | ||
| documentSequence, | ||
| isDocumentEscaped, | ||
| ref document, | ||
| ref documentHash, | ||
|
|
@@ -406,6 +424,62 @@ private readonly void ParseDocument( | |
| } | ||
| } | ||
|
|
||
| private readonly void ParseDocument( | ||
| ReadOnlySequence<byte> documentBody, | ||
| bool isEscaped, | ||
| ref DocumentNode? document, | ||
| ref OperationDocumentHash documentHash, | ||
| ref OperationDocumentId documentId) | ||
| { | ||
| // When we need to unescape, escape sequences can span segment boundaries, | ||
| // so we must work with contiguous memory. Delegate to the span-based overload | ||
| // which already handles unescaping. | ||
| if (isEscaped) | ||
| { | ||
| var length = checked((int)documentBody.Length); | ||
| var rented = s_bytePool.Rent(length); | ||
|
|
||
| try | ||
| { | ||
| documentBody.CopyTo(rented); | ||
| ParseDocument(rented.AsSpan(0, length), isEscaped, ref document, ref documentHash, ref documentId); | ||
| } | ||
|
Comment on lines
+437
to
+446
|
||
| finally | ||
| { | ||
| s_bytePool.Return(rented); | ||
| } | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (_useCache) | ||
| { | ||
| if (documentId.HasValue && _cache!.TryGetDocument(documentId.Value, out var cachedDocument)) | ||
| { | ||
| document = cachedDocument.Body; | ||
| documentHash = cachedDocument.Hash; | ||
| } | ||
| else if (documentBody.Length > 0) | ||
| { | ||
| var hash = _hashProvider!.ComputeHash(documentBody); | ||
| documentHash = hash; | ||
|
|
||
| document = _cache!.TryGetDocument(hash.Value, out cachedDocument) | ||
| ? cachedDocument.Body | ||
| : Utf8GraphQLParser.Parse(documentBody, _options); | ||
|
|
||
| if (documentId.IsEmpty) | ||
| { | ||
| documentId = new OperationDocumentId(hash.Value); | ||
| } | ||
| } | ||
| } | ||
| else | ||
| { | ||
| document = Utf8GraphQLParser.Parse(documentBody, _options); | ||
| } | ||
| } | ||
|
|
||
| private readonly bool TryExtractHash(JsonDocument? extensions, out string hash) | ||
| => TryExtractHashInternal(extensions, _hashProvider, out hash); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| using System.Buffers; | ||
|
|
||
| namespace HotChocolate.Language; | ||
|
|
||
| internal static class SequenceHelper | ||
| { | ||
| public static ReadOnlySequence<byte> CreateMultiSegment(byte[] data) | ||
| { | ||
| if (data.Length < 2) | ||
| { | ||
| return new ReadOnlySequence<byte>(data); | ||
| } | ||
|
|
||
| var mid = data.Length / 2; | ||
| var first = new MemorySegment(data.AsMemory(0, mid)); | ||
| var last = first.Append(data.AsMemory(mid)); | ||
|
|
||
| return new ReadOnlySequence<byte>(first, 0, last, last.Memory.Length); | ||
| } | ||
|
|
||
| private sealed class MemorySegment : ReadOnlySequenceSegment<byte> | ||
| { | ||
| public MemorySegment(ReadOnlyMemory<byte> memory) | ||
| { | ||
| Memory = memory; | ||
| } | ||
|
|
||
| public MemorySegment Append(ReadOnlyMemory<byte> memory) | ||
| { | ||
| var segment = new MemorySegment(memory) | ||
| { | ||
| RunningIndex = RunningIndex + Memory.Length | ||
| }; | ||
| Next = segment; | ||
| return segment; | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On
NETSTANDARD2_0, the multi-segmentComputeHash(ReadOnlySequence<byte>)path rents/copies the sequence intorented, but then callsComputeHash(rented.AsSpan(...)), which in turn rents and copies again in the span overload (because the span overload always copies into anArrayPoolbuffer on NETSTANDARD2_0). This causes a double rent+copy for multi-segment sequences. Consider computing the hash directly from the first rented buffer by calling theComputeHash(byte[] document, int length)abstraction and formatting it, to avoid the extra allocation/copy.