-
Notifications
You must be signed in to change notification settings - Fork 309
/
Copy pathpool.rs
227 lines (213 loc) · 8.46 KB
/
pool.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
//! Original: wasm_bindgen raytrace-parallel example
//!
//! File: https://github.com/rustwasm/wasm-bindgen/blob/main/examples/raytrace-parallel/src/pool.rs
use crate::TransferClosure;
use js_sys::Array;
use std::cell::RefCell;
use std::iter::FromIterator;
use std::rc::Rc;
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
use web_sys::BlobPropertyBag;
use web_sys::ErrorEvent;
use web_sys::MessageEvent;
use web_sys::{Blob, Url};
use web_sys::{Event, Worker};
#[wasm_bindgen]
pub struct WorkerPool {
state: Rc<PoolState>,
script_src: String,
}
struct PoolState {
workers: RefCell<Vec<Worker>>,
callback: Closure<dyn FnMut(Event)>,
}
#[wasm_bindgen]
impl WorkerPool {
/// Creates a new `WorkerPool` which immediately creates `initial` workers.
///
/// The pool created here can be used over a long period of time, and it
/// will be initially primed with `initial` workers. Currently workers are
/// never released or gc'd until the whole pool is destroyed.
///
/// # Errors
///
/// Returns any error that may happen while a JS web worker is created and a
/// message is sent to it.
#[wasm_bindgen(constructor)]
pub fn new(initial: usize, script_src: String) -> Result<WorkerPool, JsValue> {
let pool = WorkerPool {
script_src,
state: Rc::new(PoolState {
workers: RefCell::new(Vec::with_capacity(initial)),
callback: Closure::new(|event: Event| {
if let Some(event) = event.dyn_ref::<MessageEvent>() {
crate::console_error!("Dropped data:: {:?}", event.data());
} else if let Some(event) = event.dyn_ref::<ErrorEvent>() {
crate::console_error!("Failed to initialize: {}", event.message());
}
}),
}),
};
for _ in 0..initial {
let worker = pool.spawn()?;
pool.state.push(worker);
}
Ok(pool)
}
/// Unconditionally spawns a new worker
///
/// The worker isn't registered with this `WorkerPool` but is capable of
/// executing work for this wasm module.
///
/// # Errors
///
/// Returns any error that may happen while a JS web worker is created and a
/// message is sent to it.
fn spawn(&self) -> Result<Worker, JsValue> {
let src = &self.script_src;
let script = format!(
"importScripts('{}');
const FRB_ACTION_PANIC = 3;
onmessage = event => {{
let init = wasm_bindgen(...event.data).catch(err => {{
setTimeout(() => {{ throw err }})
throw err
}})
onmessage = async event => {{
await init
const [payload, ...transfer] = event.data
try {{
wasm_bindgen.receive_transfer_closure(payload, transfer)
}} catch (err) {{
if (transfer[0] && typeof transfer[0].postMessage === 'function') {{
// panic
transfer[0].postMessage([FRB_ACTION_PANIC, err.toString()])
}}
setTimeout(() => {{ throw err }})
postMessage(null)
throw err
}}
}}
}}",
src
);
let blob = Blob::new_with_blob_sequence_and_options(
&Array::from_iter([JsValue::from(script)]).into(),
BlobPropertyBag::new().type_("text/javascript"),
)?;
let url = Url::create_object_url_with_blob(&blob)?;
let worker: Worker = Worker::new(&url)?;
// With a worker spun up send it the module/memory so it can start
// instantiating the wasm module. Later it might receive further
// messages about code to run on the wasm module.
let module = wasm_bindgen::module();
let memory = wasm_bindgen::memory();
worker.post_message(&Array::from_iter([module, memory]))?;
Ok(worker)
}
/// Fetches a worker from this pool, spawning one if necessary.
///
/// This will attempt to pull an already-spawned web worker from our cache
/// if one is available, otherwise it will spawn a new worker and return the
/// newly spawned worker.
///
/// # Errors
///
/// Returns any error that may happen while a JS web worker is created and a
/// message is sent to it.
fn worker(&self) -> Result<Worker, JsValue> {
match self.state.workers.borrow_mut().pop() {
Some(worker) => Ok(worker),
None => self.spawn(),
}
}
/// Executes the work `f` in a web worker, spawning a web worker if
/// necessary.
///
/// This will acquire a web worker and then send the closure `f` to the
/// worker to execute. The worker won't be usable for anything else while
/// `f` is executing, and no callbacks are registered for when the worker
/// finishes.
///
/// ## Errors
///
/// Returns any error that may happen while a JS web worker is created and a
/// message is sent to it.
fn execute(&self, closure: TransferClosure<JsValue>) -> Result<Worker, JsValue> {
let worker = self.worker()?;
closure.apply(&worker).map(|_| worker)
}
/// Configures an `onmessage` callback for the `worker` specified for the
/// web worker to be reclaimed and re-inserted into this pool when a message
/// is received.
///
/// Currently this `WorkerPool` abstraction is intended to execute one-off
/// style work where the work itself doesn't send any notifications and
/// when it's done the worker is ready to execute more work. This method is
/// used for all spawned workers to ensure that when the work is finished
/// the worker is reclaimed back into this pool.
fn reclaim_on_message(&self, worker: &Worker) {
let state = Rc::downgrade(&self.state);
let worker2 = worker.clone();
let reclaim_slot = Rc::new(RefCell::new(None));
let slot2 = reclaim_slot.clone();
let reclaim = Closure::<dyn FnMut(_)>::new(move |_: MessageEvent| {
if let Some(state) = state.upgrade() {
state.push(worker2.clone());
}
*slot2.borrow_mut() = None;
});
worker.set_onmessage(Some(reclaim.as_ref().unchecked_ref()));
*reclaim_slot.borrow_mut() = Some(reclaim);
}
}
impl WorkerPool {
/// Executes `f` in a web worker.
///
/// This pool manages a set of web workers to draw from, and `f` will be
/// spawned quickly into one if the worker is idle. If no idle workers are
/// available then a new web worker will be spawned.
///
/// Once `f` returns the worker assigned to `f` is automatically reclaimed
/// by this `WorkerPool`. This method provides no method of learning when
/// `f` completes, and for that you'll need to use `run_notify`.
///
/// ## Errors
///
/// If an error happens while spawning a web worker or sending a message to
/// a web worker, that error is returned.
///
/// ## Transferrables
/// Items put inside `transfer` will have their ownership transferred from
/// the invoking JS scope to the target, rendering the value unusable in the original
/// scope. (This is similar to a `FnOnce` closure in Rust terms, but does not statically
/// move items out of scope.)
///
/// Certain types in [js_sys] and [web_sys] are transferrable, for which [Send]
/// can be unsafely implemented **only if** they are passed to the transferrables of
/// a `post_message`. Examples are `Buffer`s, `MessagePort`s, etc...
pub fn run(&self, closure: TransferClosure<JsValue>) -> Result<(), JsValue> {
let worker = self.execute(closure)?;
self.reclaim_on_message(&worker);
Ok(())
}
}
impl PoolState {
fn push(&self, worker: Worker) {
worker.set_onmessage(Some(self.callback.as_ref().unchecked_ref()));
worker.set_onerror(Some(self.callback.as_ref().unchecked_ref()));
let mut workers = self.workers.borrow_mut();
for prev in workers.iter() {
let prev: &JsValue = prev;
let worker: &JsValue = &worker;
assert!(prev != worker);
}
workers.push(worker);
}
}
#[cfg(feature = "wasm-start")]
#[wasm_bindgen(start)]
pub fn run_hooks() {
console_error_panic_hook::set_once();
}