Skip to content
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

Automatically chunk large request bodies #710

Merged
merged 11 commits into from
Sep 6, 2023

Conversation

dnadoba
Copy link
Collaborator

@dnadoba dnadoba commented Aug 30, 2023

Motivation

If the HTTPClientRequest.Body is created from a type that is not ByteBuffer, we need to convert it to it at some point.
ByteBuffer can only hold up to Int32.max on 32-bit platforms and UInt32.max on 64-bit platforms and crashes if it exceeds this size limit.

Modification

  • Convert from some Sequence<UInt8>, some Collection<UInt8>, some RandomAccessCollection<UInt8> and String in lazy chunks if the input exceeds 4 MB.
  • some Sequence<UInt8> is special as it can't be efficiently chunked lazily. Therefore we need to do the chunking eagerly if it implements the fast path withContiguousStorageIfAvailable. Otherwise we do the chunking lazy but "slowly".
  • conform HTTPClientRequest.Body to AsyncSequence to make testing easier.

Result

Converting from some Sequence<UInt8>, some Collection<UInt8>, some RandomAccessCollection<UInt8> and String to ByteBuffer will no longer crash for inputs that are larger than 4GB. Conversion above 4MB is now more memory efficient as we do it lazily and not eagerly all at once.

@dnadoba dnadoba added the semver/minor For PRs that when merged cause a bump of the minor version, ie. 1.x.0 -> 1.(x+1).0 label Aug 30, 2023
Sources/AsyncHTTPClient/AsyncAwait/HTTPClientRequest.swift Outdated Show resolved Hide resolved
Package.swift Show resolved Hide resolved
@dnadoba
Copy link
Collaborator Author

dnadoba commented Sep 5, 2023

@Lukasa anything else?

@dnadoba dnadoba merged commit 75d7f63 into swift-server:main Sep 6, 2023
@dnadoba dnadoba deleted the dn-chunked branch September 6, 2023 10:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
semver/minor For PRs that when merged cause a bump of the minor version, ie. 1.x.0 -> 1.(x+1).0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants