wisespace-io / binance-rs

Rust Library for the Binance API
Other
688 stars 305 forks source link

Timeout Error on loop of fetching future trade data #194

Open ktjd123 opened 1 year ago

ktjd123 commented 1 year ago

Hi,

I'm having error with FutureMarket.get_trades.

below is reproduce code

    let start = SystemTime::now();
    let future_market = binance::futures::market::FuturesMarket::new(None, None);
    while true {
        match future_market.get_trades("BTCBUSD") {
            Ok(_) => {
                println!(
                    "FETCH DONE, Duration from start {}s",
                    SystemTime::now().duration_since(start).unwrap().as_secs()
                );
                std::thread::sleep(Duration::from_millis(100));
                continue;
            }
            Err(e) => {
                println!(
                    "{:?}, Duration from start {}s",
                    e,
                    SystemTime::now().duration_since(start).unwrap().as_secs()
                );
                std::thread::sleep(Duration::from_millis(100));
                continue;
            }
        }
    }

after about 20s ~ 50s from start, the function keep gives Timeout error, detailed error is

FETCH DONE, Duration from start 21s
FETCH DONE, Duration from start 21s
FETCH DONE, Duration from start 21s
FETCH DONE, Duration from start 21s
FETCH DONE, Duration from start 21s
FETCH DONE, Duration from start 21s
FETCH DONE, Duration from start 22s
Error(ReqError(reqwest::Error { kind: Request, url: Url { scheme: "https", cannot_be_a_base: false, username: "", password: None, host: Some(Domain("fapi.binance.com")), port: None, path: "/fapi/v1/trades", query: Some("symbol=BTCBUSD"), fragment: None }, source: TimedOut }), State { next_error: None, backtrace: InternalBacktrace }), Duration from start 52s
Error(ReqError(reqwest::Error { kind: Request, url: Url { scheme: "https", cannot_be_a_base: false, username: "", password: None, host: Some(Domain("fapi.binance.com")), port: None, path: "/fapi/v1/trades", query: Some("symbol=BTCBUSD"), fragment: None }, source: TimedOut }), State { next_error: None, backtrace: InternalBacktrace }), Duration from start 82s

If I restart the program the code succeeds to fetch again so it doesn't seem to be rate limit related problem.

Thank you, Appreciate your works.

ktjd123 commented 1 year ago

Might be related with https://github.com/seanmonstar/reqwest/issues/1215

Using below code works like charm (async reqwest)

    let start = SystemTime::now();
    while true {
        let client = reqwest::ClientBuilder::new()
            .timeout(Duration::from_secs(5))
            .tcp_keepalive(None)
            .build()
            .unwrap();
        let result = match client
            .get("https://fapi.binance.com/fapi/v1/trades?symbol=BTCBUSD")
            .send()
            .await
        {
            Ok(o) => o,
            Err(e) => {
                println!("{:?} :  {:?}", e.status(), e);
                continue;
            }
        };
        println!(
            "{} : {}, time duration is {}",
            result.status(),
            result.text().await.unwrap().len(),
            SystemTime::now().duration_since(start).unwrap().as_secs()
        );
    }