From 5a23417499418c45ab44953d2c1e2df15e4d7cc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Renault?= Date: Thu, 17 Dec 2020 10:00:46 +0100 Subject: [PATCH] Compress updates content using gzip --- http-ui/Cargo.lock | 14 ++++++++++++++ http-ui/Cargo.toml | 1 + http-ui/src/main.rs | 11 ++++++++--- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/http-ui/Cargo.lock b/http-ui/Cargo.lock index 44a418d8c..6f54aae6c 100644 --- a/http-ui/Cargo.lock +++ b/http-ui/Cargo.lock @@ -80,6 +80,19 @@ dependencies = [ "warp", ] +[[package]] +name = "async-compression" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb1ff21a63d3262af46b9f33a826a8d134e2d0d9b2179c86034948b732ea8b2a" +dependencies = [ + "flate2", + "futures-core", + "memchr", + "pin-project-lite", + "tokio", +] + [[package]] name = "atty" version = "0.2.14" @@ -745,6 +758,7 @@ dependencies = [ "anyhow", "askama", "askama_warp", + "async-compression", "bytes", "flate2", "futures", diff --git a/http-ui/Cargo.toml b/http-ui/Cargo.toml index 2b80d851e..4983b2a77 100644 --- a/http-ui/Cargo.toml +++ b/http-ui/Cargo.toml @@ -7,6 +7,7 @@ edition = "2018" [dependencies] anyhow = "1.0.28" +async-compression = { version = "0.3.6", features = ["gzip", "tokio-02"] } grenad = { git = "https://github.com/Kerollmops/grenad.git", rev = "3adcb26" } heed = "0.10.5" memmap = "0.7.0" diff --git a/http-ui/src/main.rs b/http-ui/src/main.rs index b14e7f892..bb407fd39 100644 --- a/http-ui/src/main.rs +++ b/http-ui/src/main.rs @@ -10,6 +10,7 @@ use std::time::Instant; use std::{mem, io}; use askama_warp::Template; +use async_compression::tokio_02::write::GzipEncoder; use flate2::read::GzDecoder; use futures::stream; use futures::{FutureExt, StreamExt}; @@ -340,7 +341,7 @@ async fn main() -> anyhow::Result<()> { otherwise => panic!("invalid indexing method {:?}", otherwise), }; - let gzipped = false; + let gzipped = true; let reader = if gzipped { Box::new(GzDecoder::new(content)) } else { @@ -704,13 +705,17 @@ async fn main() -> anyhow::Result<()> { ) -> Result { let file = tokio::task::block_in_place(tempfile::tempfile).unwrap(); - let mut file = TFile::from_std(file); + let file = TFile::from_std(file); + let mut encoder = GzipEncoder::new(file); while let Some(result) = stream.next().await { let bytes = result.unwrap().to_bytes(); - file.write_all(&bytes[..]).await.unwrap(); + encoder.write_all(&bytes[..]).await.unwrap(); } + encoder.shutdown().await.unwrap(); + let mut file = encoder.into_inner(); + file.sync_all().await.unwrap(); let file = file.into_std().await; let mmap = unsafe { memmap::Mmap::map(&file).unwrap() };