Skip to content

Commit 65582a1

Browse files
committed
feat: first draft for gzip compresison
1 parent 4a092e2 commit 65582a1

File tree

3 files changed

+44
-8
lines changed

3 files changed

+44
-8
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

88
## [Unreleased]
9+
### Feat
10+
11+
- GZIP Compression
912

1013
## [0.3.6]
1114

Cargo.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@ keywords = ["async_graphql", "async", "graphql", "apollo", "studio"]
1111
categories = ["network-programming", "asynchronous"]
1212
edition = "2018"
1313

14+
[features]
15+
default = []
16+
compression = ["libflate"]
17+
1418
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1519
[dependencies]
1620
protobuf = { version = "2.18.1", features = ["bytes"] }
17-
libflate = "1"
1821
async-graphql = {version = "2.9.*", features = ["tracing"] }
1922
tracing = "0.1.*"
2023
tracing-futures = { version = "0.2.5", default-features = false, features = ["tokio", "futures-03", "std"] }
@@ -27,3 +30,6 @@ serde_json = "1.0.*" # A JSON serialization file format
2730
sha2 = "0.9.*" # Pure Rust implementation of the SHA-2 hash function family including SHA-224, SHA-256, SHA-384, a…
2831
anyhow = "1.0.*" # Flexible concrete Error type built on std::error::Error
2932
uuid = { version = "0.8.*", features = ["v4"] } # A library to generate and parse UUIDs.
33+
34+
# Non-feature optional dependencies
35+
libflate = { version = "1", optional = true }

src/lib.rs

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,40 @@ impl ApolloTracing {
203203
}
204204
};
205205

206-
let result = client
207-
.post(REPORTING_URL)
208-
.body(msg)
209-
.header("content-type", "application/protobuf")
210-
.header("X-Api-Key", &authorization_token)
211-
.send()
212-
.await;
206+
let result = if cfg!(features = "compression") {
207+
let mut encoder = libflate::gzip::Encoder::new(Vec::new()).unwrap();
208+
let mut msg = std::io::Cursor::new(msg);
209+
match std::io::copy(&mut msg, &mut encoder) {
210+
Ok(_) => {}
211+
Err(e) => {
212+
error!(target: TARGET_LOG, message = "An issue happened while GZIP compression", err = ?e);
213+
continue;
214+
}
215+
};
216+
match encoder.finish().into_result() {
217+
Ok(msg) => {
218+
client
219+
.post(REPORTING_URL)
220+
.body(msg)
221+
.header("content-type", "application/protobuf")
222+
.header("X-Api-Key", &authorization_token)
223+
.send()
224+
.await
225+
}
226+
Err(e) => {
227+
error!(target: TARGET_LOG, message = "An issue happened while GZIP compression", err = ?e);
228+
continue;
229+
}
230+
}
231+
} else {
232+
client
233+
.post(REPORTING_URL)
234+
.body(msg)
235+
.header("content-type", "application/protobuf")
236+
.header("X-Api-Key", &authorization_token)
237+
.send()
238+
.await
239+
};
213240

214241
match result {
215242
Ok(data) => {

0 commit comments

Comments
 (0)