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

@ -3,15 +3,12 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::sync::atomic::AtomicBool;
use std::sync::Mutex;
use base::id::PipelineId;
use cssparser::SourceLocation;
use encoding_rs::UTF_8;
use ipc_channel::ipc;
use ipc_channel::router::ROUTER;
use mime::{self, Mime};
use net_traits::request::{CorsSettings, Destination, Referrer, RequestBuilder};
use net_traits::request::{CorsSettings, Destination, Referrer, RequestBuilder, RequestId};
use net_traits::{
FetchMetadata, FetchResponseListener, FilteredMetadata, Metadata, NetworkError, ReferrerPolicy,
ResourceFetchTiming, ResourceTimingType,
@ -43,7 +40,7 @@ use crate::dom::node::{containing_shadow_root, document_from_node, window_from_n
use crate::dom::performanceresourcetiming::InitiatorType;
use crate::dom::shadowroot::ShadowRoot;
use crate::fetch::create_a_potential_cors_request;
use crate::network_listener::{self, NetworkListener, PreInvoke, ResourceTimingListener};
use crate::network_listener::{self, PreInvoke, ResourceTimingListener};
use crate::script_runtime::CanGc;
pub trait StylesheetOwner {
@ -94,11 +91,11 @@ pub struct StylesheetContext {
impl PreInvoke for StylesheetContext {}
impl FetchResponseListener for StylesheetContext {
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, metadata: Result<FetchMetadata, NetworkError>) {
fn process_response(&mut self, _: RequestId, metadata: Result<FetchMetadata, NetworkError>) {
if let Ok(FetchMetadata::Filtered {
filtered: FilteredMetadata::Opaque | FilteredMetadata::OpaqueRedirect(_),
..
@ -113,11 +110,15 @@ impl FetchResponseListener for StylesheetContext {
});
}
fn process_response_chunk(&mut self, mut payload: Vec<u8>) {
fn process_response_chunk(&mut self, _: RequestId, mut payload: Vec<u8>) {
self.data.append(&mut payload);
}
fn process_response_eof(&mut self, status: Result<ResourceFetchTiming, NetworkError>) {
fn process_response_eof(
&mut self,
_: RequestId,
status: Result<ResourceFetchTiming, NetworkError>,
) {
let elem = self.elem.root();
let document = self.document.root();
let mut successful = false;
@ -279,7 +280,7 @@ impl<'a> StylesheetLoader<'a> {
.elem
.downcast::<HTMLLinkElement>()
.map(HTMLLinkElement::get_request_generation_id);
let context = ::std::sync::Arc::new(Mutex::new(StylesheetContext {
let context = StylesheetContext {
elem: Trusted::new(self.elem),
source,
url: url.clone(),
@ -290,24 +291,7 @@ impl<'a> StylesheetLoader<'a> {
origin_clean: true,
request_generation_id: gen,
resource_timing: ResourceFetchTiming::new(ResourceTimingType::Resource),
}));
let (action_sender, action_receiver) = ipc::channel().unwrap();
let (task_source, canceller) = document
.window()
.task_manager()
.networking_task_source_with_canceller();
let listener = NetworkListener {
context,
task_source,
canceller: Some(canceller),
};
ROUTER.add_route(
action_receiver.to_opaque(),
Box::new(move |message| {
listener.notify_fetch(message.to().unwrap());
}),
);
let owner = self
.elem
@ -331,8 +315,9 @@ impl<'a> StylesheetLoader<'a> {
referrer_policy,
integrity_metadata,
);
let request = document.prepare_request(request);
document.fetch_async(LoadType::Stylesheet(url), request, action_sender);
document.fetch(LoadType::Stylesheet(url), request, context);
}
}