diff options
author | tannal <149947508+tannal@users.noreply.github.com> | 2024-03-21 14:13:03 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-21 06:13:03 +0000 |
commit | 5c0199b5687a5c0c1b2fe82235ca609c7d9ea614 (patch) | |
tree | 87b9ede2b821c0c651d5b803358ae2c177330849 /components/net | |
parent | 2789e9887666a05695778bd9d822616985b40dbc (diff) | |
download | servo-5c0199b5687a5c0c1b2fe82235ca609c7d9ea614.tar.gz servo-5c0199b5687a5c0c1b2fe82235ca609c7d9ea614.zip |
Net: fold websocket and http tokio runtime into one (#31771)
* net: use the same tokio runtime in websocket loader
#31648
* readability
* license
Diffstat (limited to 'components/net')
-rw-r--r-- | components/net/async_runtime.rs | 12 | ||||
-rw-r--r-- | components/net/connector.rs | 2 | ||||
-rw-r--r-- | components/net/http_loader.rs | 7 | ||||
-rw-r--r-- | components/net/lib.rs | 1 | ||||
-rw-r--r-- | components/net/resource_thread.rs | 3 | ||||
-rw-r--r-- | components/net/websocket_loader.rs | 13 |
6 files changed, 19 insertions, 19 deletions
diff --git a/components/net/async_runtime.rs b/components/net/async_runtime.rs new file mode 100644 index 00000000000..c06d570d9ae --- /dev/null +++ b/components/net/async_runtime.rs @@ -0,0 +1,12 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * 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::Mutex; + +use lazy_static::lazy_static; +use tokio::runtime::Runtime; + +lazy_static! { + pub static ref HANDLE: Mutex<Option<Runtime>> = Mutex::new(Some(Runtime::new().unwrap())); +} diff --git a/components/net/connector.rs b/components/net/connector.rs index 003576d1462..f1e210b9304 100644 --- a/components/net/connector.rs +++ b/components/net/connector.rs @@ -18,8 +18,8 @@ use log::warn; use rustls::client::WebPkiVerifier; use rustls::{Certificate, ClientConfig, OwnedTrustAnchor, RootCertStore, ServerName}; +use crate::async_runtime::HANDLE; use crate::hosts::replace_host; -use crate::http_loader::HANDLE; pub const BUF_SIZE: usize = 32768; diff --git a/components/net/http_loader.rs b/components/net/http_loader.rs index 7f25e3a561f..26d886375d4 100644 --- a/components/net/http_loader.rs +++ b/components/net/http_loader.rs @@ -31,7 +31,6 @@ use hyper::{Body, Client, Response as HyperResponse}; use hyper_serde::Serde; use ipc_channel::ipc::{self, IpcSender}; use ipc_channel::router::ROUTER; -use lazy_static::lazy_static; use log::{debug, error, info, log_enabled, warn}; use msg::constellation_msg::{HistoryStateId, PipelineId}; use net_traits::pub_domains::reg_suffix; @@ -50,13 +49,13 @@ use net_traits::{ }; use servo_arc::Arc; use servo_url::{ImmutableOrigin, ServoUrl}; -use tokio::runtime::Runtime; use tokio::sync::mpsc::{ channel, unbounded_channel, Receiver as TokioReceiver, Sender as TokioSender, UnboundedReceiver, UnboundedSender, }; use tokio_stream::wrappers::ReceiverStream; +use crate::async_runtime::HANDLE; use crate::connector::{ create_http_client, create_tls_config, CACertificates, CertificateErrorOverrideManager, Connector, @@ -70,10 +69,6 @@ use crate::hsts::HstsList; use crate::http_cache::{CacheKey, HttpCache}; use crate::resource_thread::AuthCache; -lazy_static! { - pub static ref HANDLE: Mutex<Option<Runtime>> = Mutex::new(Some(Runtime::new().unwrap())); -} - /// The various states an entry of the HttpCache can be in. #[derive(Clone, Debug, Eq, PartialEq)] pub enum HttpCacheEntryState { diff --git a/components/net/lib.rs b/components/net/lib.rs index 69a800489bc..4d67dba4203 100644 --- a/components/net/lib.rs +++ b/components/net/lib.rs @@ -4,6 +4,7 @@ #![deny(unsafe_code)] +pub mod async_runtime; pub mod connector; pub mod cookie; pub mod cookie_storage; diff --git a/components/net/resource_thread.rs b/components/net/resource_thread.rs index 032cfcb313b..52c9cb1dac4 100644 --- a/components/net/resource_thread.rs +++ b/components/net/resource_thread.rs @@ -39,6 +39,7 @@ use serde::{Deserialize, Serialize}; use servo_arc::Arc as ServoArc; use servo_url::{ImmutableOrigin, ServoUrl}; +use crate::async_runtime::HANDLE; use crate::connector::{ create_http_client, create_tls_config, CACertificates, CertificateErrorOverrideManager, }; @@ -48,7 +49,7 @@ use crate::fetch::methods::{fetch, CancellationListener, FetchContext}; use crate::filemanager_thread::FileManager; use crate::hsts::HstsList; use crate::http_cache::HttpCache; -use crate::http_loader::{http_redirect_fetch, HttpState, HANDLE}; +use crate::http_loader::{http_redirect_fetch, HttpState}; use crate::storage_thread::StorageThreadFactory; use crate::{cookie, websocket_loader}; diff --git a/components/net/websocket_loader.rs b/components/net/websocket_loader.rs index 1fd2b4178cb..f50670ed914 100644 --- a/components/net/websocket_loader.rs +++ b/components/net/websocket_loader.rs @@ -12,7 +12,7 @@ //! the need for a dedicated thread per websocket. use std::sync::atomic::{AtomicBool, Ordering}; -use std::sync::{Arc, Mutex}; +use std::sync::Arc; use async_tungstenite::tokio::{client_async_tls_with_connector_and_config, ConnectStream}; use async_tungstenite::WebSocketStream; @@ -23,13 +23,11 @@ use futures::stream::StreamExt; use http::header::{self, HeaderName, HeaderValue}; use ipc_channel::ipc::{IpcReceiver, IpcSender}; use ipc_channel::router::ROUTER; -use lazy_static::lazy_static; use log::{debug, trace, warn}; use net_traits::request::{RequestBuilder, RequestMode}; use net_traits::{CookieSource, MessageData, WebSocketDomAction, WebSocketNetworkEvent}; use servo_url::ServoUrl; use tokio::net::TcpStream; -use tokio::runtime::Runtime; use tokio::select; use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver}; use tokio_rustls::TlsConnector; @@ -39,19 +37,12 @@ use tungstenite::protocol::CloseFrame; use tungstenite::Message; use url::Url; +use crate::async_runtime::HANDLE; use crate::connector::{create_tls_config, CACertificates, TlsConfig}; use crate::cookie::Cookie; use crate::fetch::methods::should_be_blocked_due_to_bad_port; use crate::hosts::replace_host; use crate::http_loader::HttpState; - -// Websockets get their own tokio runtime that's independent of the one used for -// HTTP connections, otherwise a large number of websockets could occupy all workers -// and starve other network traffic. -lazy_static! { - pub static ref HANDLE: Mutex<Option<Runtime>> = Mutex::new(Some(Runtime::new().unwrap())); -} - /// Create a tungstenite Request object for the initial HTTP request. /// This request contains `Origin`, `Sec-WebSocket-Protocol`, `Authorization`, /// and `Cookie` headers as appropriate. |