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

@ -5,19 +5,18 @@
use std::borrow::{Borrow, ToOwned};
use std::cell::Cell;
use std::default::Default;
use std::sync;
use cssparser::{Parser as CssParser, ParserInput};
use dom_struct::dom_struct;
use embedder_traits::EmbedderMsg;
use html5ever::{local_name, namespace_url, ns, LocalName, Prefix};
use ipc_channel::ipc;
use ipc_channel::router::ROUTER;
use js::rust::HandleObject;
use net_traits::request::{CorsSettings, Destination, Initiator, Referrer, RequestBuilder};
use net_traits::request::{
CorsSettings, Destination, Initiator, Referrer, RequestBuilder, RequestId,
};
use net_traits::{
CoreResourceMsg, FetchChannels, FetchMetadata, FetchResponseListener, IpcSend, NetworkError,
ReferrerPolicy, ResourceFetchTiming, ResourceTimingType,
FetchMetadata, FetchResponseListener, NetworkError, ReferrerPolicy, ResourceFetchTiming,
ResourceTimingType,
};
use servo_arc::Arc;
use servo_atoms::Atom;
@ -56,7 +55,7 @@ use crate::dom::stylesheet::StyleSheet as DOMStyleSheet;
use crate::dom::virtualmethods::VirtualMethods;
use crate::fetch::create_a_potential_cors_request;
use crate::links::LinkRelations;
use crate::network_listener::{submit_timing, NetworkListener, PreInvoke, ResourceTimingListener};
use crate::network_listener::{submit_timing, PreInvoke, ResourceTimingListener};
use crate::stylesheet_loader::{StylesheetContextSource, StylesheetLoader, StylesheetOwner};
#[derive(Clone, Copy, JSTraceable, MallocSizeOf, PartialEq)]
@ -374,42 +373,14 @@ impl HTMLLinkElement {
// (Step 7, firing load/error events is handled in the FetchResponseListener impl for PrefetchContext)
// Step 8. The user agent should fetch request, with processResponseConsumeBody set to processPrefetchResponse.
let (action_sender, action_receiver) = ipc::channel().unwrap();
let document = self.upcast::<Node>().owner_doc();
let window = document.window();
let (task_source, canceller) = window
.task_manager()
.networking_task_source_with_canceller();
let fetch_context = sync::Arc::new(sync::Mutex::new(PrefetchContext {
let fetch_context = PrefetchContext {
url,
link: Trusted::new(self),
resource_timing: ResourceFetchTiming::new(ResourceTimingType::Resource),
}));
let listener = NetworkListener {
context: fetch_context,
task_source,
canceller: Some(canceller),
};
ROUTER.add_route(
action_receiver.to_opaque(),
Box::new(move |message| {
listener.notify_fetch(message.to().unwrap());
}),
);
window
.upcast::<GlobalScope>()
.resource_threads()
.sender()
.send(CoreResourceMsg::Fetch(
request,
FetchChannels::ResponseMsg(action_sender, None),
))
.unwrap();
document.fetch_background(request, fetch_context, None);
}
/// <https://html.spec.whatwg.org/multipage/#concept-link-obtain>
@ -713,20 +684,28 @@ struct PrefetchContext {
}
impl FetchResponseListener for PrefetchContext {
fn process_request_body(&mut self) {}
fn process_request_body(&mut self, _: RequestId) {}
fn process_request_eof(&mut self) {}
fn process_request_eof(&mut self, _: RequestId) {}
fn process_response(&mut self, fetch_metadata: Result<FetchMetadata, NetworkError>) {
fn process_response(
&mut self,
_: RequestId,
fetch_metadata: Result<FetchMetadata, NetworkError>,
) {
_ = fetch_metadata;
}
fn process_response_chunk(&mut self, chunk: Vec<u8>) {
fn process_response_chunk(&mut self, _: RequestId, chunk: Vec<u8>) {
_ = chunk;
}
// Step 7 of `fetch and process the linked resource` in https://html.spec.whatwg.org/multipage/#link-type-prefetch
fn process_response_eof(&mut self, response: Result<ResourceFetchTiming, NetworkError>) {
fn process_response_eof(
&mut self,
_: RequestId,
response: Result<ResourceFetchTiming, NetworkError>,
) {
if response.is_err() {
// Step 1. If response is a network error, fire an event named error at el.
self.link