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

@ -2,6 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
use base::id::PipelineId;
@ -17,6 +18,17 @@ use servo_url::{ImmutableOrigin, ServoUrl};
use crate::response::HttpsState;
use crate::{ReferrerPolicy, ResourceTimingType};
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, MallocSizeOf, PartialEq, Serialize)]
/// An id to differeniate one network request from another.
pub struct RequestId(usize);
impl RequestId {
pub fn new() -> Self {
static NEXT_REQUEST_ID: AtomicUsize = AtomicUsize::new(0);
Self(NEXT_REQUEST_ID.fetch_add(1, Ordering::Relaxed))
}
}
/// An [initiator](https://fetch.spec.whatwg.org/#concept-request-initiator)
#[derive(Clone, Copy, Debug, Deserialize, MallocSizeOf, PartialEq, Serialize)]
pub enum Initiator {
@ -223,6 +235,7 @@ impl RequestBody {
#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
pub struct RequestBuilder {
pub id: RequestId,
#[serde(
deserialize_with = "::hyper_serde::deserialize",
serialize_with = "::hyper_serde::serialize"
@ -272,6 +285,7 @@ pub struct RequestBuilder {
impl RequestBuilder {
pub fn new(url: ServoUrl, referrer: Referrer) -> RequestBuilder {
RequestBuilder {
id: RequestId::new(),
method: Method::GET,
url,
headers: HeaderMap::new(),
@ -396,6 +410,11 @@ impl RequestBuilder {
self
}
pub fn csp_list(mut self, csp_list: Option<CspList>) -> RequestBuilder {
self.csp_list = csp_list;
self
}
pub fn crash(mut self, crash: Option<String>) -> Self {
self.crash = crash;
self
@ -403,6 +422,7 @@ impl RequestBuilder {
pub fn build(self) -> Request {
let mut request = Request::new(
self.id,
self.url.clone(),
Some(Origin::Origin(self.origin)),
self.referrer,
@ -443,6 +463,9 @@ impl RequestBuilder {
/// the Fetch spec.
#[derive(Clone, MallocSizeOf)]
pub struct Request {
/// The id of this request so that the task that triggered it can route
/// messages to the correct listeners.
pub id: RequestId,
/// <https://fetch.spec.whatwg.org/#concept-request-method>
#[ignore_malloc_size_of = "Defined in hyper"]
pub method: Method,
@ -514,6 +537,7 @@ pub struct Request {
impl Request {
pub fn new(
id: RequestId,
url: ServoUrl,
origin: Option<Origin>,
referrer: Referrer,
@ -521,6 +545,7 @@ impl Request {
https_state: HttpsState,
) -> Request {
Request {
id,
method: Method::GET,
local_urls_only: false,
sandboxed_storage_area_urls: false,