Repository navigation
fix: Uploading files in chunks to improve memory usage - #3697
vanlooverenkoen wants to merge 2 commits into
Conversation
📝 WalkthroughWalkthroughThe upload method for binary file uploads was refactored to use a streaming approach. Instead of reading the entire data stream into memory and posting it, the method now streams data directly to the server using Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant FileUploader
participant Server
Client->>FileUploader: Call upload(stream, length)
FileUploader->>Server: Initiate StreamedRequest with Content-Length
loop While data available
FileUploader->>Server: Stream data chunk
end
FileUploader->>Server: Close request sink
Server-->>FileUploader: Respond with status code
FileUploader-->>Client: Return upload result
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
⏰ Context from checks skipped due to timeout of 90000ms (61)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
packages/serverpod_client/lib/src/file_uploader.dart (1)
94-101: Consider removing unused_readStreamDatamethod.This method appears to be dead code after the streaming implementation changes. The new approach streams data directly without needing to read the entire stream into memory first.
If this method is no longer used, consider removing it:
- Future<List<int>> _readStreamData(Stream<List<int>> stream) async { - // TODO: Find more efficient solution? - var data = <int>[]; - await for (var segment in stream) { - data += segment; - } - return data; - }Run the following script to verify if this method is still being used elsewhere:
#!/bin/bash # Description: Check if _readStreamData method is used anywhere in the codebase # Expected: No usage found, confirming it's dead code rg -A 3 "_readStreamData" --type dart
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
packages/serverpod_client/lib/src/file_uploader.dart(1 hunks)
🧰 Additional context used
🧠 Learnings (1)
📓 Common learnings
Learnt from: nielsenko
PR: serverpod/serverpod#0
File: :0-0
Timestamp: 2025-06-19T12:43:05.099Z
Learning: In FileUploader implementations, avoid using `http.ByteStream(stream).toBytes()` for large file uploads as it loads the entire stream into memory before uploading. Instead, use streaming upload mechanisms that send data directly from the stream to the HTTP request without buffering the complete content in memory first.
⏰ Context from checks skipped due to timeout of 90000ms (60)
- GitHub Check: Module integration tests in modules/serverpod_auth/serverpod_auth_server with Flutter 3.24.0
- GitHub Check: Flutter unit tests (3.24.0, ubuntu-latest, packages/serverpod_flutter)
- GitHub Check: Flutter unit tests (3.24.0, windows-latest, packages/serverpod_flutter)
- GitHub Check: Module integration tests in modules/serverpod_auth/serverpod_auth_server with Flutter 3.32.1
- GitHub Check: Flutter unit tests (3.32.1, ubuntu-latest, packages/serverpod_flutter)
- GitHub Check: Serverpod integration tests (3.32.1, packages/serverpod_shared)
- GitHub Check: Flutter unit tests (3.32.1, windows-latest, packages/serverpod_flutter)
- GitHub Check: Serverpod integration tests (3.24.0, packages/serverpod_shared)
- GitHub Check: Module integration tests in modules/new_serverpod_auth/serverpod_auth_profile/serverpod_auth_profile_server with Flutter 3.24.0
- GitHub Check: Module integration tests in modules/new_serverpod_auth/serverpod_auth_session/serverpod_auth_session_server with Flutter 3.24.0
- GitHub Check: Module integration tests in modules/new_serverpod_auth/serverpod_auth_jwt/serverpod_auth_jwt_server with Flutter 3.32.1
- GitHub Check: Module integration tests in modules/new_serverpod_auth/serverpod_auth_profile/serverpod_auth_profile_server with Flutter 3.32.1
- GitHub Check: Module integration tests in modules/new_serverpod_auth/serverpod_auth_session/serverpod_auth_session_server with Flutter 3.32.1
- GitHub Check: Module integration tests in modules/new_serverpod_auth/serverpod_auth_email_account/serverpod_auth_email_account_server with Flutter 3.32.1
- GitHub Check: Module integration tests in tests/serverpod_new_auth_test/serverpod_new_auth_test_server with Flutter 3.32.1
- GitHub Check: Module integration tests in tests/serverpod_test_module/serverpod_test_module_server with Flutter 3.32.1
- GitHub Check: Module integration tests in tests/serverpod_test_nonvector/serverpod_test_nonvector_server with Flutter 3.32.1
- GitHub Check: Module integration tests in modules/new_serverpod_auth/serverpod_auth_email_account/serverpod_auth_email_account_server with Flutter 3.24.0
- GitHub Check: Module integration tests in modules/new_serverpod_auth/serverpod_auth_jwt/serverpod_auth_jwt_server with Flutter 3.24.0
- GitHub Check: Module integration tests in tests/serverpod_new_auth_test/serverpod_new_auth_test_server with Flutter 3.24.0
- GitHub Check: update pubspecs (3.24.0)
- GitHub Check: Module integration tests in tests/serverpod_test_module/serverpod_test_module_server with Flutter 3.24.0
- GitHub Check: Module integration tests in tests/serverpod_test_nonvector/serverpod_test_nonvector_server with Flutter 3.24.0
- GitHub Check: Version change test
- GitHub Check: Migration e2e tests (3.32.1)
- GitHub Check: Serverpod integration tests (server) (3.24.0)
- GitHub Check: Unit tests Web (3.32.1, windows-latest)
- GitHub Check: Serverpod integration tests (flutter)
- GitHub Check: Unit tests Web (3.24.0, windows-latest)
- GitHub Check: Migration e2e tests (3.24.0)
- GitHub Check: Serverpod concurrent integration tests (server) (3.24.0)
- GitHub Check: Serverpod concurrent integration tests (server) (3.32.1)
- GitHub Check: Unit tests Web (3.32.1, ubuntu-latest)
- GitHub Check: Bootstrap tests (3.24.0, ubuntu-latest, tests/bootstrap_project)
- GitHub Check: Unit tests Web (3.24.0, ubuntu-latest)
- GitHub Check: Serverpod integration tests (server) (3.32.1)
- GitHub Check: Bootstrap tests (3.32.1, ubuntu-latest, tests/bootstrap_project)
- GitHub Check: Unit tests IO (3.32.1, windows-latest)
- GitHub Check: Unit tests IO (3.32.1, ubuntu-latest)
- GitHub Check: Bootstrap tests (3.32.1, windows-latest, tests/bootstrap_project)
- GitHub Check: Unit tests IO (3.24.0, ubuntu-latest)
- GitHub Check: Unit tests IO (3.24.0, windows-latest)
- GitHub Check: serverpod generate (3.32.1)
- GitHub Check: Bootstrap tests (3.24.0, windows-latest, tests/bootstrap_project)
- GitHub Check: Serverpod E2E IO tests (3.24.0)
- GitHub Check: serverpod generate (3.24.0)
- GitHub Check: Serverpod E2E web tests (3.24.0)
- GitHub Check: dart analyze downgrade (3.32.1, windows-latest)
- GitHub Check: Serverpod E2E IO tests (3.32.1)
- GitHub Check: Serverpod E2E web tests (3.32.1)
- GitHub Check: dart analyze downgrade (3.24.0, ubuntu-latest)
- GitHub Check: dart analyze downgrade (3.32.1, ubuntu-latest)
- GitHub Check: CLI e2e tests (3.32.1, ubuntu-latest, tests/serverpod_cli_e2e_test)
- GitHub Check: update pubspecs (3.32.1)
- GitHub Check: CLI e2e tests (3.24.0, ubuntu-latest, tests/serverpod_cli_e2e_test)
- GitHub Check: dart analyze (3.24.0, windows-latest)
- GitHub Check: dart analyze downgrade (3.24.0, windows-latest)
- GitHub Check: dart analyze (3.24.0, ubuntu-latest)
- GitHub Check: dart analyze latest (3.32.1, ubuntu-latest)
- GitHub Check: dart analyze latest (3.32.1, windows-latest)
🔇 Additional comments (1)
packages/serverpod_client/lib/src/file_uploader.dart (1)
38-49: Excellent improvement to streaming uploads, but verify async stream handling.This implementation correctly addresses the memory efficiency concern by using
StreamedRequestto stream data directly without buffering the entire content in memory first. The approach aligns perfectly with the retrieved learning about avoidinghttp.ByteStream(stream).toBytes()for large uploads.Key improvements:
- Direct stream piping eliminates memory buffering
- Proper error propagation with
onError: request.sink.addError- Correct sink closure with
onDone: request.sink.close- Appropriate headers for binary uploads
The HTTP package should handle the timing correctly, waiting for the sink to close before completing the request, but it's worth verifying this behavior works as expected in your testing.
Consider running integration tests with large files to ensure the streaming behavior works correctly and doesn't cause any timing issues.
|
Duplicate of #3695 |
|
@vanlooverenkoen Thanks for making an attempt at improving this though. Feel free to give your input on #3695. |
Replace this paragraph with a description of what this PR is changing or adding, and why.
#3691
#3633
This also fixes the issue where the upload would block the UI thread.
Pre-launch Checklist
///), and made sure that the documentation follows the same style as other Serverpod documentation. I checked spelling and grammar.If you need help, consider asking for advice on the discussion board.
Summary by CodeRabbit