Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

open_ports_by_range() waits for each TCP connection synchronously #4

Open
nokola opened this issue Dec 25, 2019 · 2 comments
Open

open_ports_by_range() waits for each TCP connection synchronously #4

nokola opened this issue Dec 25, 2019 · 2 comments

Comments

@nokola
Copy link

nokola commented Dec 25, 2019

The following code from open_ports_by_range will .await each is_port_open() instead of executing the is_port_open()-s in parallel. Ideally the .awaiting will be done for multiple future-s at a time using something like join_all. see: https://users.rust-lang.org/t/joinall-and-async-await/31051/9.

        for port in from..to {
            let is_open = self.is_port_open(port).await;
            if is_open {
                open_ports.push(port)
            }
        }

Note: if you decide to .await all ports in parallel, there might be an issue in case there's restriction of max TCP connections at the same time for some reason - not sure if that would be a real issue on some machines or not an issue at all.

@ndelvalle ndelvalle mentioned this issue Jan 12, 2020
@ndelvalle
Copy link
Owner

@nokola do you mind taking a look at my PR and let me know what you think?
I notice that changing the argument value from buffer_unordered method does not make things faster. I should maybe allow the user to change that value or update it with a sane default.

@nokola
Copy link
Author

nokola commented Jan 21, 2020

I don't mind but unfortunately I'm not qualified enough (I'm Rust noob still) to say for sure whether this code would result in the open_ports_by_range executing asynchronously or not.

pub async fn is_port_open(ip: IpAddr, port: u16) -> bool {
   TcpStream::connect((ip, port)).await.is_ok()
}

One way to verify is to replace the is_port_open body with a dummy wait code and print out the port # like this:

pub async fn is_port_open(ip: IpAddr, port: u16) -> bool {
   // TODO: async_wait_2_seconds.await()
   println!("{}", port);
}

Then, run open_ports_by_range(...) for say 100 ports and see if the whole app completes in ~2 seconds or ~200 seconds. If it completes fast, then all good.

Please let me know how this goes if/when you try the experiment - interested to see the results. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants