net: Start reducing number of IPCs channels used for fetch with a FetchThread (#33863)

Instead of creating a `ROUTER` for each fetch, create a fetch thread
which handles all incoming and outcoming fetch requests. Now messages
involving fetches carry a "request id" which indicates which fetch is
being addressed by the message. This greatly reduces the number of file
descriptors used by fetch.

In addition, the interface for kicking off fetches is simplified when
using the `Listener` with `Document`s and the `GlobalScope`.

This does not fix all leaked file descriptors / mach ports, but greatly
eliminates the number used. Now tests can be run without limiting
procesess on modern macOS systems.

Followup work:

1. There are more instances where fetch is done using the old method.
   Some of these require more changes in order to be converted to the
   `FetchThread` approach.
2. Eliminate usage of IPC channels when doing redirects.
3. Also eliminate the IPC channel used for cancel handling.
4. This change opens up the possiblity of controlling the priority of
   fetch requests.

Fixes #29834.

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
This commit is contained in:
Martin Robinson 2024-10-16 09:53:24 -07:00 committed by GitHub
parent 2115267328
commit 036e74524a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
31 changed files with 761 additions and 766 deletions

View file

@ -6,9 +6,9 @@
//!
//! <https://html.spec.whatwg.org/multipage/#the-end>
use ipc_channel::ipc::IpcSender;
use ipc_channel::ipc;
use net_traits::request::RequestBuilder;
use net_traits::{CoreResourceMsg, FetchChannels, FetchResponseMsg, IpcSend, ResourceThreads};
use net_traits::{fetch_async, BoxedFetchCallback, ResourceThreads};
use servo_url::ServoUrl;
use crate::dom::bindings::root::Dom;
@ -111,33 +111,37 @@ impl DocumentLoader {
self.blocking_loads.push(load);
}
/// Initiate a new fetch.
pub fn fetch_async(
/// Initiate a new fetch given a response callback.
pub fn fetch_async_with_callback(
&mut self,
load: LoadType,
request: RequestBuilder,
fetch_target: IpcSender<FetchResponseMsg>,
callback: BoxedFetchCallback,
) {
self.add_blocking_load(load);
self.fetch_async_background(request, fetch_target);
self.fetch_async_background(request, callback, None);
}
/// Initiate a new fetch that does not block the document load event.
pub fn fetch_async_background(
&mut self,
request: RequestBuilder,
fetch_target: IpcSender<FetchResponseMsg>,
callback: BoxedFetchCallback,
cancel_override: Option<ipc::IpcReceiver<()>>,
) {
let mut canceller = FetchCanceller::new();
let cancel_receiver = canceller.initialize();
self.cancellers.push(canceller);
self.resource_threads
.sender()
.send(CoreResourceMsg::Fetch(
request,
FetchChannels::ResponseMsg(fetch_target, Some(cancel_receiver)),
))
.unwrap();
let canceller = cancel_override.unwrap_or_else(|| {
let mut canceller = FetchCanceller::new();
let cancel_receiver = canceller.initialize();
self.cancellers.push(canceller);
cancel_receiver
});
fetch_async(
&self.resource_threads.core_thread,
request,
Some(canceller),
callback,
);
}
/// Mark an in-progress network request complete.