-
Notifications
You must be signed in to change notification settings - Fork 58
fix: read body in chunks #164
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
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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 |
---|---|---|
@@ -1,8 +1,17 @@ | ||
pub(crate) const STATUS_ENDPOINT_TAG: &str = "status"; | ||
pub(crate) const REGISTER_VALIDATOR_ENDPOINT_TAG: &str = "register_validator"; | ||
pub(crate) const SUBMIT_BLINDED_BLOCK_ENDPOINT_TAG: &str = "submit_blinded_block"; | ||
pub(crate) const GET_HEADER_ENDPOINT_TAG: &str = "get_header"; | ||
pub const STATUS_ENDPOINT_TAG: &str = "status"; | ||
pub const REGISTER_VALIDATOR_ENDPOINT_TAG: &str = "register_validator"; | ||
pub const SUBMIT_BLINDED_BLOCK_ENDPOINT_TAG: &str = "submit_blinded_block"; | ||
pub const GET_HEADER_ENDPOINT_TAG: &str = "get_header"; | ||
|
||
/// For metrics recorded when a request times out | ||
pub(crate) const TIMEOUT_ERROR_CODE: u16 = 555; | ||
pub(crate) const TIMEOUT_ERROR_CODE_STR: &str = "555"; | ||
pub const TIMEOUT_ERROR_CODE: u16 = 555; | ||
pub const TIMEOUT_ERROR_CODE_STR: &str = "555"; | ||
|
||
/// 20 MiB to cover edge cases for heavy blocks and also add a bit of slack for | ||
/// any Ethereum upgrades in the near future | ||
pub const MAX_SIZE_SUBMIT_BLOCK: usize = 20 * 1024 * 1024; | ||
|
||
/// 10 KiB, headers are around 700 bytes + buffer for encoding | ||
pub const MAX_SIZE_GET_HEADER: usize = 10 * 1024; | ||
|
||
pub const MAX_SIZE_DEFAULT: usize = 1024; |
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
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
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
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
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
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,27 @@ | ||
use cb_common::pbs::error::PbsError; | ||
use futures::StreamExt; | ||
use reqwest::Response; | ||
|
||
pub async fn read_chunked_body_with_max( | ||
res: Response, | ||
max_size: usize, | ||
) -> Result<Vec<u8>, PbsError> { | ||
let mut stream = res.bytes_stream(); | ||
let mut response_bytes = Vec::new(); | ||
|
||
while let Some(chunk) = stream.next().await { | ||
let chunk = chunk?; | ||
if response_bytes.len() + chunk.len() > max_size { | ||
// avoid spamming logs if the message is too large | ||
response_bytes.truncate(1024); | ||
return Err(PbsError::PayloadTooLarge { | ||
max: max_size, | ||
raw: String::from_utf8_lossy(&response_bytes).into_owned(), | ||
}); | ||
} | ||
|
||
response_bytes.extend_from_slice(&chunk); | ||
} | ||
|
||
Ok(response_bytes) | ||
} |
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
wonder if we can do something like so we avoid variables
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.
think this could work but not sure it's more readable?