Skip to content

Use builder pattern and channels (fixes #12) - #33

Merged
mre merged 25 commits into
masterfrom
builder
Nov 24, 2020
Merged

mre merged 25 commits into
masterfrom
builder

Conversation

@mre

@mre mre commented Nov 18, 2020 •

Copy link
Copy Markdown
Member

This implements a basic builder for the Checker struct as discussed in #12.

  • It is using derive_builder and uses a custom build method to instantiate the more elaborate fields like reqwest::Client.
  • I ran into some lifetime issues with the builder in combination with the progress_bar. After some tinkering I realized that it's much easier if we decouple the link checking from the progress reporting. So I used async_channel in between the two. There is a new Worker struct, which reads from the channel and runs the checker. This way the checker does not even know that the channel exists, which allows us to put the checker into its own crate eventually for use as a library.

TODO:

  • The channel is blocking on each request at the moment. I think we need to spawn all the check futures at once and then join them in the end. Don't know how this is done yet. Perhaps with futures::select? Ideas very welcome.
  • Clarify if tokio::spawn(async move { woker.listen().await } ); is really idiomatic. I get a warning that I'm not using the Result, but I don't know how. This can probably be simplified to tokio::spawn(worker.listen());.
  • Fix CLI tests.

TBD:

  • Shall we rename Checker and CheckerBuilder? I really don't like those names anymore. Was considering Lychee, but it has nothing to do with the functionality. Request sounds too generic. What do people think of Validator?

@mre
mre requested a review from pawroman November 18, 2020 23:06
@mre

mre commented Nov 19, 2020

Copy link
Copy Markdown
Member Author

Update: I renamed Checker to Client, which is in line with reqwest::Client. Users of the library can use lychee::ClientBuilder (which is the equivalent to request::ClientBuilder) to construct their client in the future.

mre added 2 commits November 19, 2020 23:18
- Rename Checker to Client (in line with reqwest::Client)
- Rename CheckerBuilder to ClientBuilder (in line with reqwest::ClientBuilder)
- Remove crate visibility for types that will be exposed in the library
Turns out reqwest also accepts any request method and
dutifully makes a request to the server.
The servers might respond with an HTTP status code > 2xx,
but apart from that, these requests are totally valid.

I don't know why this was done, but I assume because
of compatibility with potential future versions of the
HTTP spec. In any case, I'm planning to lift the
restrictrions on accepting only `GET` and `HEAD` requests
because of this and removing the test.
@mre

mre commented Nov 19, 2020

Copy link
Copy Markdown
Member Author

Removed test for invalid method

Turns out reqwest also accepts any request method and dutifully makes a request
to the server. The servers might respond with an HTTP status code > 2xx, but
apart from that, these requests are totally valid.

For example, the following code runs without any problems:

use reqwest;

#[tokio::main]
async fn main() {
    let client = reqwest::Client::new();
    let fake_method = reqwest::Method::from_bytes(b"LOL").unwrap();
    let req = client
        .request(fake_method, "https://example.com")
        .build()
        .unwrap();
    println!("{:?}", client.execute(req).await);
}

The response fails with a 501, but it is a valid request nonetheless.

Ok(Response { url: Url { scheme: "https", host: Some(Domain("example.com")),
port: None, path: "/", query: None, fragment: None }, status: 501, headers:
{"content-type": "text/html", "content-length": "357", "connection": "close",
"date": "Thu, 19 Nov 2020 23:33:23 GMT", "server": "ECSF (nyb/1D25)"} })

I don't know why this was done, but I assume because of compatibility with
potential future versions of the HTTP spec. In any case, I'm lifting
the restrictions on accepting only GET and HEAD requests because of this
and removing the test.

mre added 2 commits November 20, 2020 01:50
This code certainly be improved

@pawroman pawroman left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good overall, great work! Left a few (mostly minor) remarks.

After some tinkering I realized that it's much easier if we decouple the link checking from the progress reporting.

Great idea 👍

Shall we rename Checker and CheckerBuilder? I really don't like those names anymore. Was considering Lychee, but it has nothing to do with the functionality. Request sounds too generic. What do people think of Validator?

No strong opinions here, Validator is as good a name as any 😉

Comment thread src/main.rs Resolved
Comment thread src/main.rs Resolved
Comment thread src/options.rs Resolved
Comment thread src/stats.rs Resolved
Comment thread src/stats.rs Resolved
Comment thread src/main.rs Resolved
Comment thread src/main.rs Resolved
Comment thread src/client.rs Resolved
Comment thread src/client.rs Resolved
Comment thread src/client.rs Resolved
@mre

mre commented Nov 24, 2020

Copy link
Copy Markdown
Member Author

Ticked off the last points on the todo list:

The channel is blocking on each request at the moment. I think we need to spawn all the check futures at once and then join them in the end. Don't know how this is done yet. Perhaps with futures::select? Ideas very welcome.

futures::select is not needed. An mpsc channel plus Deadpool took care of it. Added a --max-concurrency flag and tried a couple of values. It flies with 256 concurrent requests on my computer (1MB/s).

If someone wants to try, check
cargo run --release -- --progress fixtures/top-1000000-domains.txt --max-concurrency 256.

Right now, the bootup is slow with a lot of URLs. The URL parsing on the file top-1000000-domains.txt takes 5 seconds on my machine. The URLs are parsed sequentially at the moment. In the future, we could have a pool of extractors that push new requests onto the queue. This would offset the URL parsing costs and parse the URLs lazily.
But that's for a future change.

Clarify if tokio::spawn(async move { woker.listen().await } ); is really idiomatic. I get a warning that I'm not using the Result, but I don't know how. This can probably be simplified to tokio::spawn(worker.listen());.

It's not idiomatic. We use tokio::spawn now.

@pawroman: No strong opinions here, Validator is as good a name as any 😉

I'd say lychee::Client and lychee::ClientBuilder is what I'd like to go with because it's symmetric to reqwest::Client and reqwest::ClientBuilder. Sounds good?

Thanks for the review @pawroman. 🚀
From my side, this is ready to merge.

@mre
mre marked this pull request as ready for review November 24, 2020 00:25
@mre mre changed the title WIP: Use builder pattern and channels (fixes #12) Use builder pattern and channels (fixes #12) Nov 24, 2020

@pawroman pawroman left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good 👍

I just have some doubts whether we need the top 1 million domains file which is 22.5 MB in size. This is going to negatively effect contribution experience. Maybe we could shorten and/or compress it?

Also, since it seems not to be used, maybe we could move it to a separate repository for now?

@mre

mre commented Nov 24, 2020

Copy link
Copy Markdown
Member Author

I just have some doubts whether we need the top 1 million domains file which is 22.5 MB in size. This is going to negatively effect contribution experience. Maybe we could shorten and/or compress it?

Oh yeah absolutely. I was using it for a bigger test locally just to see if performance was okay.
I removed it now. In the future we could have a lychee-bench repo with some benchmark tests to track performance over time if we like.

Fun fact: The internet is a weird place and many URLs in that list are either not available anymore (DNS error), don't support HTTPS or trigger redirect loops. I was kinda surprised and thought I hit bugs in lychee but actually those errors turned out to be valid — and that's for the top 1 million sites on the web; I don't even want to know about the rest of them.

Did some final cleanup for the status reporting based on your last comments.
Thanks for the thorough review. Merging now. :shipit:

@mre
mre merged commit b0f7a80 into master Nov 24, 2020
@mre
mre deleted the builder branch November 24, 2020 20:30
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

Successfully merging this pull request may close these issues.

2 participants