Skip to content

Instantly share code, notes, and snippets.

@aka-bash0r
Created January 22, 2021 11:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aka-bash0r/bf6835ca29c6ddad72daad5d439864bf to your computer and use it in GitHub Desktop.
Save aka-bash0r/bf6835ca29c6ddad72daad5d439864bf to your computer and use it in GitHub Desktop.
async-h1 panics at 1001th request to a Binance API endpoint
[package]
name = "minimal-example"
version = "0.1.0"
authors = ["Nils 'bash0r' Jonsson"]
edition = "2018"
[dependencies]
async-h1 = "2.3.0"
async-dup = "1.2.2"
http-types = "2.9.0"
futures = "0.3.12"
[dependencies.async-std]
version = "1.9.0"
features = ["attributes"]
[dependencies.async-tls]
version = "0.11.0"
default-features = false
features = ["client"]
use std::time::Duration;
use std::ops::Div;
#[async_std::main]
async fn main() -> http_types::Result<()>
{
let host = "api.binance.com";
let port = 443;
let url = http_types::Url::parse(format!("https://{}/api/v3/klines?symbol=ETHBTC&interval=1m&limit=1000", host).as_str())?;
let request = http_types::Request::get(url.clone());
let stream = async_std::net::TcpStream::connect((host, port)).await?;
let stream = async_tls::TlsConnector::new().connect(host, stream).await?;
let stream = async_dup::Arc::new(async_dup::Mutex::new(stream));
let count = 1200u32;
let mut sum = Duration::from_secs(0);
let start = std::time::Instant::now();
for ix in 0..count {
let start = std::time::Instant::now();
let mut response = async_h1::connect(stream.clone(), request.clone()).await?;
let body = response.body_string().await?;
let end = std::time::Instant::now();
let ping = end.duration_since(start);
sum += ping;
println!("finished {} with status {} and ping of {}ms", ix, response.status(), ping.as_millis());
}
let end = std::time::Instant::now();
let ping = sum.div(count);
let duration_total = end.duration_since(start);
println!("{}s in average", ping.as_secs_f64());
println!("{}s in total", duration_total.as_secs_f64());
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment