Skip to content

Commit

Permalink
use a threadpool for fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
gterzian committed Apr 12, 2019
1 parent 1e4b42a commit c7c8804
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 36 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions components/net/Cargo.toml
Expand Up @@ -44,6 +44,7 @@ net_traits = {path = "../net_traits"}
openssl = "0.10"
pixels = {path = "../pixels"}
profile_traits = {path = "../profile_traits"}
rayon = "1"
serde = "1.0"
serde_json = "1.0"
servo_allocator = {path = "../allocator"}
Expand Down
75 changes: 39 additions & 36 deletions components/net/resource_thread.rs
Expand Up @@ -398,6 +398,7 @@ pub struct CoreResourceManager {
devtools_chan: Option<Sender<DevtoolsControlMsg>>,
swmanager_chan: Option<IpcSender<CustomResponseMediator>>,
filemanager: FileManager,
fetch_pool: rayon::ThreadPool,
}

impl CoreResourceManager {
Expand All @@ -407,11 +408,16 @@ impl CoreResourceManager {
_profiler_chan: ProfilerChan,
embedder_proxy: EmbedderProxy,
) -> CoreResourceManager {
let pool = rayon::ThreadPoolBuilder::new()
.num_threads(16)
.build()
.unwrap();
CoreResourceManager {
user_agent: user_agent,
devtools_chan: devtools_channel,
swmanager_chan: None,
filemanager: FileManager::new(embedder_proxy),
fetch_pool: pool,
}
}

Expand Down Expand Up @@ -446,42 +452,39 @@ impl CoreResourceManager {
_ => ResourceTimingType::Resource,
};

thread::Builder::new()
.name(format!("fetch thread for {}", request_builder.url))
.spawn(move || {
let mut request = request_builder.build();
// XXXManishearth: Check origin against pipeline id (also ensure that the mode is allowed)
// todo load context / mimesniff in fetch
// todo referrer policy?
// todo service worker stuff
let context = FetchContext {
state: http_state,
user_agent: ua,
devtools_chan: dc,
filemanager: filemanager,
cancellation_listener: Arc::new(Mutex::new(CancellationListener::new(
cancel_chan,
))),
timing: Arc::new(Mutex::new(ResourceFetchTiming::new(request.timing_type()))),
};

match res_init_ {
Some(res_init) => {
let response = Response::from_init(res_init, timing_type);
http_redirect_fetch(
&mut request,
&mut CorsCache::new(),
response,
true,
&mut sender,
&mut None,
&context,
);
},
None => fetch(&mut request, &mut sender, &context),
};
})
.expect("Thread spawning failed");
self.fetch_pool.spawn(move || {
let mut request = request_builder.build();
// XXXManishearth: Check origin against pipeline id (also ensure that the mode is allowed)
// todo load context / mimesniff in fetch
// todo referrer policy?
// todo service worker stuff
let context = FetchContext {
state: http_state,
user_agent: ua,
devtools_chan: dc,
filemanager: filemanager,
cancellation_listener: Arc::new(Mutex::new(CancellationListener::new(
cancel_chan,
))),
timing: Arc::new(Mutex::new(ResourceFetchTiming::new(request.timing_type()))),
};

match res_init_ {
Some(res_init) => {
let response = Response::from_init(res_init, timing_type);
http_redirect_fetch(
&mut request,
&mut CorsCache::new(),
response,
true,
&mut sender,
&mut None,
&context,
);
},
None => fetch(&mut request, &mut sender, &context),
};
});
}

fn websocket_connect(
Expand Down
10 changes: 10 additions & 0 deletions tests/wpt/mozilla/meta/MANIFEST.json
Expand Up @@ -13052,6 +13052,12 @@
{}
]
],
"mozilla/fetch_cannot_overwhelm_system.window.js": [
[
"/_mozilla/mozilla/fetch_cannot_overwhelm_system.window.html",
{}
]
],
"mozilla/first-reflow-sheet-assert.html": [
[
"mozilla/first-reflow-sheet-assert.html",
Expand Down Expand Up @@ -20057,6 +20063,10 @@
"0ba1ce0d5577de68e5e8ff3acbce52043e7dee43",
"testharness"
],
"mozilla/fetch_cannot_overwhelm_system.window.js": [
"989231e9caedd099f5212bd2f9d377c83f929a22",
"testharness"
],
"mozilla/first-reflow-sheet-assert.html": [
"268af6d333f04adc35974ca3f2e9ebb29783fd2e",
"testharness"
Expand Down
@@ -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();
}
});
});

0 comments on commit c7c8804

Please sign in to comment.