-
Notifications
You must be signed in to change notification settings - Fork 16
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
fix: Check the max data size against the final message payload #212
Merged
Merged
Changes from 2 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
This file contains 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,5 +1,6 @@ | ||
use crate::routers::adm::error::AdmError; | ||
use crate::routers::adm::settings::{AdmProfile, AdmSettings}; | ||
use crate::routers::common::message_size_check; | ||
use crate::routers::RouterError; | ||
use autopush_common::util::sec_since_epoch; | ||
use futures::lock::Mutex; | ||
|
@@ -15,6 +16,7 @@ pub struct AdmClient { | |
base_url: Url, | ||
profile: AdmProfile, | ||
timeout: Duration, | ||
max_data: usize, | ||
http: reqwest::Client, | ||
token_info: Mutex<TokenInfo>, | ||
} | ||
|
@@ -53,6 +55,7 @@ impl AdmClient { | |
base_url: settings.base_url.clone(), | ||
profile, | ||
timeout: Duration::from_secs(settings.timeout as u64), | ||
max_data: settings.max_data, | ||
http, | ||
// The default TokenInfo has dummy values to trigger a token fetch | ||
token_info: Mutex::default(), | ||
|
@@ -122,6 +125,10 @@ impl AdmClient { | |
"data": data, | ||
"expiresAfter": ttl, | ||
}); | ||
let message_json = message.to_string(); | ||
message_size_check(message_json.as_bytes(), self.max_data)?; | ||
|
||
// Prepare request data | ||
let access_token = self.get_access_token().await?; | ||
let url = self | ||
.base_url | ||
|
@@ -136,6 +143,7 @@ impl AdmClient { | |
.http | ||
.post(url) | ||
.header("Authorization", format!("Bearer {}", access_token.as_str())) | ||
.header("Content-Type", "application/json") | ||
.header("Accept", "application/json") | ||
.header( | ||
"X-Amzn-Type-Version", | ||
|
@@ -145,7 +153,7 @@ impl AdmClient { | |
"X-Amzn-Accept-Type", | ||
"[email protected]", | ||
) | ||
.json(&message) | ||
.body(message_json) | ||
.timeout(self.timeout) | ||
.send() | ||
.await | ||
|
This file contains 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 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 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 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 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.
Kind of depends on how you want to do this, but for the python side, I determined that the max payload size depends on the encoding type.
aesgcm
requires the header keys, which need to be included with the overall payload and impact the size of the data that can be sent over.aes128gcm
includes the decryption keys in the body payload, and the body and therefore the body can be larger. The other problem is that the body is base64 encoded which basically inflates things by about 1.3x, which may confuse update publishers that don't know that.Of course, key sizes can vary as can the result of base64 encoding, so things are bit fuzzy as far as how big things are or could get.
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.
The payload size error returns the number of excess bytes, so the publishers will have an idea about how much data to cut out. I feel like doing the fuzzy calculations of guessing the raw data size and accounting for base64 adds a lot of unnecessary complexity/uncertainty and is too hand-holding. We should view this from the perspective of the messaging services (like FCM) and just make sure the data we are passing along will fit through the bridge services. If it doesn't fit, then we relay the excess byte count to the publisher. We can talk about this in the docs so the publishers have more insight into why they might be over the payload limit, but guessing the reason in code seems error-prone.
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.
I suppose putting a note in the docs might be as useful. One thing I've discovered is that the more data you can put into error messages, the less customer support and invalid bug triage you have to do in the future. Folks are less inclined to read docs than error messages, but at least we can point them in the right direction.