use a threadpool for fetch

This commit is contained in:
Gregory Terzian 2019-01-27 20:20:32 +08:00
parent 1e4b42a90b
commit c52cfda957
6 changed files with 73 additions and 35 deletions

1
Cargo.lock generated
View file

@ -2791,6 +2791,7 @@ dependencies = [
"openssl 0.10.11 (registry+https://github.com/rust-lang/crates.io-index)", "openssl 0.10.11 (registry+https://github.com/rust-lang/crates.io-index)",
"pixels 0.0.1", "pixels 0.0.1",
"profile_traits 0.0.1", "profile_traits 0.0.1",
"rayon 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.80 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.80 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.13 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.13 (registry+https://github.com/rust-lang/crates.io-index)",
"servo_allocator 0.0.1", "servo_allocator 0.0.1",

View file

@ -44,6 +44,7 @@ net_traits = {path = "../net_traits"}
openssl = "0.10" openssl = "0.10"
pixels = {path = "../pixels"} pixels = {path = "../pixels"}
profile_traits = {path = "../profile_traits"} profile_traits = {path = "../profile_traits"}
rayon = "1"
serde = "1.0" serde = "1.0"
serde_json = "1.0" serde_json = "1.0"
servo_allocator = {path = "../allocator"} servo_allocator = {path = "../allocator"}

View file

@ -398,6 +398,7 @@ pub struct CoreResourceManager {
devtools_chan: Option<Sender<DevtoolsControlMsg>>, devtools_chan: Option<Sender<DevtoolsControlMsg>>,
swmanager_chan: Option<IpcSender<CustomResponseMediator>>, swmanager_chan: Option<IpcSender<CustomResponseMediator>>,
filemanager: FileManager, filemanager: FileManager,
fetch_pool: rayon::ThreadPool,
} }
impl CoreResourceManager { impl CoreResourceManager {
@ -407,11 +408,16 @@ impl CoreResourceManager {
_profiler_chan: ProfilerChan, _profiler_chan: ProfilerChan,
embedder_proxy: EmbedderProxy, embedder_proxy: EmbedderProxy,
) -> CoreResourceManager { ) -> CoreResourceManager {
let pool = rayon::ThreadPoolBuilder::new()
.num_threads(16)
.build()
.unwrap();
CoreResourceManager { CoreResourceManager {
user_agent: user_agent, user_agent: user_agent,
devtools_chan: devtools_channel, devtools_chan: devtools_channel,
swmanager_chan: None, swmanager_chan: None,
filemanager: FileManager::new(embedder_proxy), filemanager: FileManager::new(embedder_proxy),
fetch_pool: pool,
} }
} }
@ -446,42 +452,37 @@ impl CoreResourceManager {
_ => ResourceTimingType::Resource, _ => ResourceTimingType::Resource,
}; };
thread::Builder::new() self.fetch_pool.spawn(move || {
.name(format!("fetch thread for {}", request_builder.url)) let mut request = request_builder.build();
.spawn(move || { // XXXManishearth: Check origin against pipeline id (also ensure that the mode is allowed)
let mut request = request_builder.build(); // todo load context / mimesniff in fetch
// XXXManishearth: Check origin against pipeline id (also ensure that the mode is allowed) // todo referrer policy?
// todo load context / mimesniff in fetch // todo service worker stuff
// todo referrer policy? let context = FetchContext {
// todo service worker stuff state: http_state,
let context = FetchContext { user_agent: ua,
state: http_state, devtools_chan: dc,
user_agent: ua, filemanager: filemanager,
devtools_chan: dc, cancellation_listener: Arc::new(Mutex::new(CancellationListener::new(cancel_chan))),
filemanager: filemanager, timing: Arc::new(Mutex::new(ResourceFetchTiming::new(request.timing_type()))),
cancellation_listener: Arc::new(Mutex::new(CancellationListener::new( };
cancel_chan,
))),
timing: Arc::new(Mutex::new(ResourceFetchTiming::new(request.timing_type()))),
};
match res_init_ { match res_init_ {
Some(res_init) => { Some(res_init) => {
let response = Response::from_init(res_init, timing_type); let response = Response::from_init(res_init, timing_type);
http_redirect_fetch( http_redirect_fetch(
&mut request, &mut request,
&mut CorsCache::new(), &mut CorsCache::new(),
response, response,
true, true,
&mut sender, &mut sender,
&mut None, &mut None,
&context, &context,
); );
}, },
None => fetch(&mut request, &mut sender, &context), None => fetch(&mut request, &mut sender, &context),
}; };
}) });
.expect("Thread spawning failed");
} }
fn websocket_connect( fn websocket_connect(

View file

@ -13052,6 +13052,19 @@
{} {}
] ]
], ],
"mozilla/fetch_cannot_overwhelm_system.window.js": [
[
"mozilla/fetch_cannot_overwhelm_system.window.html",
{
"script_metadata": [
[
"title",
"Ensure multiple fetch do not crash the browser."
]
]
}
]
],
"mozilla/first-reflow-sheet-assert.html": [ "mozilla/first-reflow-sheet-assert.html": [
[ [
"mozilla/first-reflow-sheet-assert.html", "mozilla/first-reflow-sheet-assert.html",
@ -20057,6 +20070,10 @@
"0ba1ce0d5577de68e5e8ff3acbce52043e7dee43", "0ba1ce0d5577de68e5e8ff3acbce52043e7dee43",
"testharness" "testharness"
], ],
"mozilla/fetch_cannot_overwhelm_system.window.js": [
"989231e9caedd099f5212bd2f9d377c83f929a22",
"testharness"
],
"mozilla/first-reflow-sheet-assert.html": [ "mozilla/first-reflow-sheet-assert.html": [
"268af6d333f04adc35974ca3f2e9ebb29783fd2e", "268af6d333f04adc35974ca3f2e9ebb29783fd2e",
"testharness" "testharness"

View file

@ -0,0 +1,3 @@
[fetch_cannot_overwhelm_system.window.html]
expected:
if os == "linux": CRASH

View file

@ -0,0 +1,15 @@
// META: title=Ensure multiple fetch do not crash the browser.
async_test(function(t) {
onload = t.step_func(function() {
var step;
var xhr
var url = '/';
t.step_timeout(t.step_func_done(), 10);
for (step = 0; step < 5000; step++) {
xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.send();
}
});
});