Auto merge of #24402 - gterzian:remove_thread_spawning_in_ws, r=jdm

Websocket: use ipc router to handle ws dom-action messages

<!-- Please describe your changes on the following line: -->

Use the ipc router as opposed to spawning a dedicated thread.

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `___` with appropriate data: -->
- [ ] `./mach build -d` does not report any errors
- [ ] `./mach test-tidy` does not report any errors
- [ ] These changes fix #___ (GitHub issue number if applicable)

<!-- Either: -->
- [ ] There are tests for these changes OR
- [ ] These changes do not require tests because ___

<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/24402)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2019-10-09 09:16:12 -04:00 committed by GitHub
commit 3d529b1b88
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -12,6 +12,7 @@ use headers::Host;
use http::header::{self, HeaderMap, HeaderName, HeaderValue}; use http::header::{self, HeaderMap, HeaderName, HeaderValue};
use http::uri::Authority; use http::uri::Authority;
use ipc_channel::ipc::{IpcReceiver, IpcSender}; use ipc_channel::ipc::{IpcReceiver, IpcSender};
use ipc_channel::router::ROUTER;
use net_traits::request::{RequestBuilder, RequestMode}; use net_traits::request::{RequestBuilder, RequestMode};
use net_traits::{CookieSource, MessageData}; use net_traits::{CookieSource, MessageData};
use net_traits::{WebSocketDomAction, WebSocketNetworkEvent}; use net_traits::{WebSocketDomAction, WebSocketNetworkEvent};
@ -242,8 +243,10 @@ pub fn init(
let ws_sender = ws.broadcaster(); let ws_sender = ws.broadcaster();
let initiated_close = Arc::new(AtomicBool::new(false)); let initiated_close = Arc::new(AtomicBool::new(false));
thread::spawn(move || { ROUTER.add_route(
while let Ok(dom_action) = dom_action_receiver.recv() { dom_action_receiver.to_opaque(),
Box::new(move |message| {
let dom_action = message.to().expect("Ws dom_action message to deserialize");
match dom_action { match dom_action {
WebSocketDomAction::SendMessage(MessageData::Text(data)) => { WebSocketDomAction::SendMessage(MessageData::Text(data)) => {
ws_sender.send(Message::text(data)).unwrap(); ws_sender.send(Message::text(data)).unwrap();
@ -265,8 +268,8 @@ pub fn init(
} }
}, },
} }
} }),
}); );
if let Err(e) = ws.run() { if let Err(e) = ws.run() {
debug!("Failed to run WebSocket: {:?}", e); debug!("Failed to run WebSocket: {:?}", e);