actix / actix-web

Actix Web is a powerful, pragmatic, and extremely fast web framework for Rust.
https://actix.rs
Apache License 2.0
21.75k stars 1.68k forks source link

actix::client first request extremely slow unless you put a static dns in hosts #599

Closed aohan237 closed 5 years ago

aohan237 commented 5 years ago

it may be the dns issue. yes,if you put a static dns in hosts. it will be normal.

but other tools, such as curl will not have the problem. why?

url: https://cn.bing.com

curl is normal.but in actix::client ,is extremely slow

fn main() {
    let now = Instant::now();
    println!("{:?}", now);
    actix::run(
        || client::get("https://cn.bing.com")   // <- Create request builder
            .header("User-Agent", "Actix-web")
            .finish()
            .unwrap()
            .send()                               // <- Send http request
            .map_err(|_| ())
            .and_then(move |response| {                // <- server http response
                println!("Response: {:?}", response);
                println!("{:?}",Instant::now()- now);
                Ok(())
            })
    );
}

it will wait for about 5sec, until it get the first response.

aohan237 commented 5 years ago

or actix has a inner dns loopup function,which use a different dns lookup server rather than a local dns server?

ghost commented 5 years ago

I also noticed that https is much slower than http

DoumanAsh commented 5 years ago

Actix uses trust-dns-resolver and on first lookup it is going to take a while to build its caches

aohan237 commented 5 years ago

can I disable it ,just use local dns?

aohan237 commented 5 years ago

it is very slow on first, what's the cause you decided to use that?

DoumanAsh commented 5 years ago

Not at the moment, it would require changing the way actix-web uses connector to allow custom one. https://actix.rs/actix-web/actix_web/client/struct.ClientConnector.html#method.resolver

I had some ideas for changing time, but I didn't have time to work on it.

it is very slow on first, what's the cause you decided to use that?

It is async

DoumanAsh commented 5 years ago

@aohan237 my new PR will allow you to provide address of your own resolver, you'd need to implement own actor that could handle https://actix.rs/actix/actix/actors/resolver/struct.Connect.html

fafhrd91 commented 5 years ago

Actually, you should ask trust-dns author how to fix this problem

DoumanAsh commented 5 years ago

I'm actually not sure if it possible to fix per se, you just need to build DNS cache before making requests, but rising issue on this problem would be good idea

tuxzz commented 5 years ago

Yes it's really slow, takes about 1 minute for first request on my environment.

damody commented 5 years ago

still really slow now.

tuxzz commented 5 years ago

@damody I have a workaround for my situation: Create request builder with IP address and add Host Http header when send request. It works perfect if you have only few domains to request and can ensure the IP address of requested domain won't change.

damody commented 5 years ago

@tuxzz thank you, I will try.