-
Notifications
You must be signed in to change notification settings - Fork 19
feat: Add option to enable compression of event payloads #102
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
Changes from 5 commits
e4516eb
0f4760a
acc99b7
02f7b77
02501df
156c203
efd8ba6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,10 +2,17 @@ use crate::{ | |
| reqwest::is_http_error_recoverable, LAUNCHDARKLY_EVENT_SCHEMA_HEADER, | ||
| LAUNCHDARKLY_PAYLOAD_ID_HEADER, | ||
| }; | ||
| use std::collections::HashMap; | ||
|
|
||
| use chrono::DateTime; | ||
| use crossbeam_channel::Sender; | ||
| use std::collections::HashMap; | ||
|
|
||
| #[cfg(feature = "compress")] | ||
| use flate2::write::GzEncoder; | ||
| #[cfg(feature = "compress")] | ||
| use flate2::Compression; | ||
| #[cfg(feature = "compress")] | ||
| use std::io::Write; | ||
|
|
||
| use futures::future::BoxFuture; | ||
| use hyper::{client::connect::Connection, service::Service, Uri}; | ||
| use tokio::{ | ||
|
|
@@ -36,6 +43,10 @@ pub struct HyperEventSender<C> { | |
| sdk_key: String, | ||
| http: hyper::Client<C>, | ||
| default_headers: HashMap<&'static str, String>, | ||
|
|
||
| // used with compress feature | ||
| #[allow(dead_code)] | ||
| compress_events: bool, | ||
| } | ||
|
|
||
| impl<C> HyperEventSender<C> | ||
|
|
@@ -50,12 +61,14 @@ where | |
| url: hyper::Uri, | ||
| sdk_key: &str, | ||
| default_headers: HashMap<&'static str, String>, | ||
| compress_events: bool, | ||
| ) -> Self { | ||
| Self { | ||
| url, | ||
| sdk_key: sdk_key.to_owned(), | ||
| http: hyper::Client::builder().build(connector), | ||
| default_headers, | ||
| compress_events, | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -96,7 +109,9 @@ where | |
| serde_json::to_string_pretty(&events).unwrap_or_else(|e| e.to_string()) | ||
| ); | ||
|
|
||
| let json = match serde_json::to_vec(&events) { | ||
| // mut is needed for compress feature | ||
| #[allow(unused_mut)] | ||
| let mut payload = match serde_json::to_vec(&events) { | ||
| Ok(json) => json, | ||
| Err(e) => { | ||
| error!( | ||
|
|
@@ -107,6 +122,21 @@ where | |
| } | ||
| }; | ||
|
|
||
| // mut is needed for compress feature | ||
| #[allow(unused_mut)] | ||
| let mut additional_headers = self.default_headers.clone(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be not worth it in terms of readability, but possibly this could conditionally be
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have to do the same thing with the json bit above, so the readability doesn't seem good. I'm inclined to leave it. |
||
|
|
||
| #[cfg(feature = "compress")] | ||
| if self.compress_events { | ||
| let mut e = GzEncoder::new(Vec::new(), Compression::default()); | ||
| if e.write_all(payload.as_slice()).is_ok() { | ||
| if let Ok(compressed) = e.finish() { | ||
| payload = compressed; | ||
| additional_headers.insert("Content-Encoding", "gzip".into()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| for attempt in 1..=2 { | ||
| if attempt == 2 { | ||
| sleep(Duration::from_secs(1)).await; | ||
|
|
@@ -124,11 +154,12 @@ where | |
| ) | ||
| .header(LAUNCHDARKLY_PAYLOAD_ID_HEADER, uuid.to_string()); | ||
|
|
||
| for default_header in &self.default_headers { | ||
| for default_header in &additional_headers { | ||
| error!("Adding header: {} = {}", default_header.0, default_header.1); | ||
cwaldren-ld marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| request_builder = | ||
| request_builder.header(*default_header.0, default_header.1.as_str()); | ||
| } | ||
| let request = request_builder.body(hyper::Body::from(json.clone())); | ||
| let request = request_builder.body(hyper::Body::from(payload.clone())); | ||
|
|
||
| let result = self.http.request(request.unwrap()).await; | ||
|
|
||
|
|
@@ -334,6 +365,7 @@ mod tests { | |
| url, | ||
| "sdk-key", | ||
| HashMap::<&str, String>::new(), | ||
| false, | ||
| ) | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.