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

Use Reqwest instead of custom HTTP handler #510

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Kakapio
Copy link
Contributor

@Kakapio Kakapio commented Mar 28, 2024

As discussed in #490

I intend to recreate the Fetch API using Reqwest to add support for the HTTP crate.

Copy link
Contributor

@kflansburg kflansburg left a comment

Choose a reason for hiding this comment

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

Let's make sure to keep API with http disabled unchanged.

match self {
Fetch::Url(url) => fetch_with_str(url.as_ref(), Some(signal)).await,
Fetch::Request(req) => fetch_with_request(req, Some(signal)).await,
}
}
}

//#[cfg(feature = "http")]
async fn fetch_with_str(url: &str, signal: Option<&AbortSignal>) -> Result<Response> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

@Kakapio Kakapio Mar 28, 2024

Choose a reason for hiding this comment

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

This might be a bad question, but why does the reqwest repo contain .signal functionality and the Cargo package doesn't? I don't see it in the docs either when it's clearly in the repository.
https://docs.rs/reqwest/latest/reqwest/struct.Client.html

Is there a WIP branch or something of the sort to get at it?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is because the Wasm support just hooks up to fetch in the same way we do so, its only possible on Wasm.

https://github.com/seanmonstar/reqwest/blob/master/src/wasm/client.rs#L185

But you see it isn't plumbed through from the client or request object.

Copy link
Contributor Author

@Kakapio Kakapio Mar 29, 2024

Choose a reason for hiding this comment

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

Would it be acceptable to just use Drop and provide a handler? I am not sure if we necessarily need direct access to the signal.

Something like:

// Asynchronous loop to continuously check for cancellation signal
    loop {
        tokio::select! {
            _ = sleep(Duration::from_secs(1)) => {
                println!("Async task running...");
            }
            _ = cancel_receiver.recv() => {
                println!("Cancellation signal received. Dropping task...");
                break; // Break out of the loop when cancellation signal is received
            }
        }
    }

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure what the tradeoffs are here. Using a JavaScript Abort Signal lets the runtime cancel the request rather which I think might be more efficient, but if there is no way to plumb this through it may be ok to go this route.

match self {
Fetch::Url(url) => fetch_with_str(url.as_ref(), None).await,
Fetch::Request(req) => fetch_with_request(req, None).await,
}
}

/// Execute a Fetch call and receive a Response.
pub async fn send_with_signal(&self, signal: &AbortSignal) -> Result<WorkerResponse> {
pub async fn send_with_signal(&self, signal: &AbortSignal) -> Result<Response> {
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if we want to convert reqwest::Response to http::Response<Body>. I think its not totally straightforward, but possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What would be the purpose of that? Would it help with keeping the data format more flexible? I'm curious.

Copy link
Contributor

Choose a reason for hiding this comment

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

Well, right now if they wanted to call fetch and then return the value, it would be the wrong type unless it was converted to http::Response.

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