-
Notifications
You must be signed in to change notification settings - Fork 2
issue-29: Add a consumer to process requests in chunks [fix] #32
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
Merged
spencerwplovie
merged 3 commits into
main
from
issue-29-api-fails-downloading-files-greater-than-1gb
Feb 11, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| classdef ChunkedResponseConsumer < matlab.net.http.io.GenericConsumer | ||
| %% ChunkedResponseConsumer Consumer with an option to control the size of | ||
| % chunks which a response is processed in. | ||
| % | ||
| % Default MATLAB (R2022b) implementation of method `send` in | ||
| % `matlab.net.http.ResponseMeassage` has a size limit of 2^30 bytes. | ||
| % Responses of greater sizes cause an error at the stage of decoding. | ||
| % | ||
| % This consumer allows to mitigate the size limitation by processing | ||
| % responses in parts of a specified size. Although built-in GenericConsumer | ||
| % seems to be able to handle big responses as well, its processing speed is | ||
| % slower since MATLAB built-in consumers process parts of responses when | ||
| % they get them. This consumer accumulates data in a buffer of the given | ||
| % size and processes the whole accumulated chunk in one pass. | ||
|
|
||
| properties (Access=private) | ||
| chunkSize % Size of chunks to process in bytes (int) | ||
| responseBuffer % Buffer to accumulate data bytes (cell array) | ||
| positionInBuffer % Index of next buffer cell to be filled (int) | ||
| accumulatedBytes % Number of bytes accumulated in buffer (int) | ||
| end | ||
|
|
||
| methods | ||
| function obj = ChunkedResponseConsumer(chunkSize) | ||
| %% Initialize a customized GenericConsumer for processing responses in | ||
| % chunks | ||
| % | ||
| % ChunkedResponseConsumer(chunkSize) | ||
| % | ||
| % - chunkSize: (int) Number of bytes to accumulate in a buffer | ||
| % | ||
| % Returns: (ChunkedResponseConsumer) Consumer object | ||
|
|
||
| if nargin < 1 | ||
| chunkSize = 2^29; % default value | ||
| else | ||
| if ~isnumeric(chunkSize) || length(chunkSize) > 1 | ||
| error('MatlabAPI:ChunkedResponseConsumer:BadInputType', ... | ||
| 'chunkSize is expected to be a number.') | ||
| end | ||
| if chunkSize > 2^30 | ||
| error('MatlabAPI:ChunkedResponseConsumer:BadInputValue', ... | ||
| ['Provided chunkSize is too big and will cause an ' ... | ||
| 'error when a response is decoded if its size ' ... | ||
| 'exceeds 2^30 bytes. Provided value: ' num2str(chunkSize)]) | ||
| end | ||
| end | ||
| obj@matlab.net.http.io.GenericConsumer; | ||
| obj.chunkSize = chunkSize; | ||
| end | ||
|
|
||
| function [len, stop] = putData(obj, data) | ||
| %% Process the next block of data | ||
| % | ||
| % putData(data) | ||
| % | ||
| % - data: ([uint8]) Array of bytes to be processed | ||
| % | ||
| % Returns: | ||
| % (int) Number of bytes processed at the pass | ||
| % (logical) Indicator of a response end | ||
|
|
||
| stop = false; | ||
| len = numel(data); | ||
| if ~isempty(data) | ||
| if obj.accumulatedBytes + len > obj.chunkSize | ||
| % process an accumulated chunk | ||
| chunk = cell2mat(obj.responseBuffer); | ||
| obj.PutMethod(chunk); | ||
| obj.responseBuffer = {}; | ||
| obj.accumulatedBytes = 0; | ||
| obj.positionInBuffer = 1; | ||
| end | ||
| % store a data block in responseBuffer | ||
| % vertical structure of responseBuffer is important for | ||
| % stacking cells with cell2mat() into a vertical vector | ||
| obj.responseBuffer{obj.positionInBuffer, 1} = data; | ||
| obj.accumulatedBytes = obj.accumulatedBytes + len; | ||
| obj.positionInBuffer = obj.positionInBuffer + 1; | ||
| else | ||
| if ~isempty(obj.responseBuffer) | ||
| % process the rest of data stored in responseBuffer | ||
| chunk = cell2mat(obj.responseBuffer); | ||
| obj.PutMethod(chunk); | ||
| end | ||
| % extract response data to CurrentDelegate | ||
| obj.PutMethod(uint8.empty); | ||
| obj.CurrentLength = length(obj.CurrentDelegate.Response.Body.Data); | ||
| obj.PutMethod = []; | ||
| % transfer data from CurrentDelegate to obj.Response | ||
| putData@matlab.net.http.io.GenericConsumer(obj, []); | ||
| stop = true; | ||
| end | ||
| end | ||
| end | ||
|
|
||
| methods (Access = protected) | ||
| function buffsize = start(obj) | ||
| %% Call when the response starts | ||
| obj.responseBuffer = {}; | ||
| obj.CurrentLength = 0; | ||
| obj.accumulatedBytes = 0; | ||
| obj.positionInBuffer = 1; | ||
| buffsize = start@matlab.net.http.io.GenericConsumer(obj); | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
It might be handy to have a check when the user passes in a value for chunkSize to make sure it's less than the 2^30 bytes limit
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.
Also, I'm not quite sure how to pass in a value for chunkSize. It's not a parameter, so would the user need to run
chunkSize = int;on the command line before they run a data product order?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.
Good point. Checking the
chunkSizevalue would be helpful. I'll add it.As for how to set this parameter up, it is done when the class is initialized. At the moment when an instance of this class is created, a user can pass input argument to the class like
I might need to add empty brackets to line 28
onc/+util/do_request.mwhere the consumer is initialized. Or, to spare people from guessing, I can make passing of the parameter explicit by doing it in the way it is done in the code snippet above.