Commit 68d0f0c3 authored by lorenzo's avatar lorenzo

Http optimizations, less ram usage, Code cleanup

parent 6b06c444
......@@ -1057,6 +1057,8 @@ dependencies = [
"lazy_static",
"log",
"mime",
"serde",
"serde_json",
"slab",
"sluice",
"tracing",
......
......@@ -30,7 +30,7 @@ futures = "0.3.1"
futures-io = "0.3.1"
futures-util = "0.3.1"
async-std = "1.4.0"
isahc = "0.9"
isahc = {version = "0.9", features = ["json"]}
regex = "1.3.4"
gstreamer = { version = "0.16.1", features = ["v1_10"] }
......
......@@ -20,6 +20,23 @@ use url::Url;
use crate::api::*;
use crate::config;
use crate::database::StationIdentifier;
use isahc::config::RedirectPolicy;
use std::time::Duration;
lazy_static! {
pub static ref USER_AGENT: String = format!("{}/{}-{}", config::PKGNAME, config::VERSION, config::PROFILE);
pub static ref HTTP_CLIENT: isahc::HttpClient = isahc::HttpClientBuilder::new()
// Limit to reduce ram usage. We don't need 250 concurrent connections
.max_connections(8)
// Icons are fetched from different urls.
// There's a lot of probability we aren't going to reuse the same connection
.connection_cache_size(8)
.timeout(Duration::from_secs(15))
.redirect_policy(RedirectPolicy::Follow)
.default_header("User-Agent", USER_AGENT.as_str())
.build()
.unwrap();
}
#[derive(Clone)]
pub struct Client {
......@@ -34,10 +51,7 @@ impl Client {
pub async fn send_station_request(self, request: StationRequest) -> Result<Vec<Station>, Error> {
let url = self.build_url(STATION_SEARCH, Some(&request.url_encode()))?;
debug!("Station request URL: {}", url);
let data = self.send_message(url).await?;
// Parse text to Vec<Station>
let stations: Vec<Station> = serde_json::from_str(data.as_str())?;
let stations: Vec<Station> = HTTP_CLIENT.get_async(url.as_ref()).await?.json()?;
debug!("Found {} station(s)!", stations.len());
Ok(stations)
......@@ -47,11 +61,9 @@ impl Client {
let url = self.build_url(&format!("{}{}", STATION_BY_UUID, identifier.stationuuid), None)?;
debug!("Request station by UUID URL: {}", url);
let data = self.send_message(url).await?;
let mut data: Vec<Station> = HTTP_CLIENT.get_async(url.as_ref()).await?.json()?;
// Parse text to Vec<Station>
let mut s: Vec<Station> = serde_json::from_str(data.as_str())?;
match s.pop() {
match data.pop() {
Some(station) => Ok(station),
None => {
warn!("API: No station for identifier \"{}\" found", &identifier.stationuuid);
......@@ -60,16 +72,6 @@ impl Client {
}
}
// Create and send message, return the received data.
async fn send_message(&self, url: Url) -> Result<String, Error> {
let useragent = format!("{}/{}-{}", config::PKGNAME, config::VERSION, config::PROFILE);
let request = Request::builder().uri(url.to_string()).header("User-Agent", useragent);
let response = isahc::send_async(request.body(()).unwrap()).await?.text_async().await?;
Ok(response)
}
fn build_url(&self, param: &str, options: Option<&str>) -> Result<Url, Error> {
let mut url = self.server.join(param)?;
if let Some(options) = options {
......
......@@ -14,17 +14,17 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use async_std::io::ReadExt;
use gdk_pixbuf::Pixbuf;
use gio::prelude::*;
use gio::DataInputStream;
use isahc::config::RedirectPolicy;
use isahc::prelude::*;
use url::Url;
use std::collections::hash_map::DefaultHasher;
use std::hash::Hash;
use std::hash::Hasher;
use crate::api::client::HTTP_CLIENT;
use crate::api::Error;
use crate::path;
......@@ -44,10 +44,8 @@ impl FaviconDownloader {
}
// Download favicon
let mut response = Request::get(url.to_string()).redirect_policy(RedirectPolicy::Follow).body(()).unwrap().send_async().await?;
let mut body = response.body_mut();
let mut bytes = vec![];
async_std::io::copy(&mut body, &mut bytes).await.unwrap();
HTTP_CLIENT.get_async(url.as_str()).await?.into_body().read_to_end(&mut bytes).await?;
let input_stream = gio::MemoryInputStream::from_bytes(&glib::Bytes::from(&bytes));
let pixbuf = Pixbuf::from_stream_at_scale_async_future(&input_stream, size, size, true).await?;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment