Skip to content

Commit 92e5fe0

Browse files
committed
feat: working coordinator architecture
1 parent e10ae8b commit 92e5fe0

26 files changed

Lines changed: 769 additions & 181 deletions

File tree

.cargo/config.toml

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
11
[target.wasm32-unknown-unknown]
22
rustflags = [
33
"--cfg", "getrandom_backend=\"wasm_js\"",
4-
"-C", "target-feature=+atomics,+bulk-memory",
5-
"-C", "link-arg=--shared-memory",
6-
"-C", "link-arg=--max-memory=1073741824",
7-
"-C", "link-arg=--import-memory",
8-
"-C", "link-arg=--export=__wasm_init_tls",
9-
"-C", "link-arg=--export=__tls_size",
10-
"-C", "link-arg=--export=__tls_align",
11-
"-C", "link-arg=--export=__tls_base"
124
]
135
runner = "wasm-bindgen-test-runner"
14-
15-
[unstable]
16-
build-std = ["std", "panic_abort"]

.vscode/settings.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"rust-analyzer.cargo.target": "wasm32-unknown-unknown",
3+
"rust-analyzer.check.targets": "wasm32-unknown-unknown",
4+
"rust-analyzer.cargo.features": "all",
5+
"rust-analyzer.procMacro.enable": true
6+
}

Cargo.lock

Lines changed: 13 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[target.wasm32-unknown-unknown]
2+
rustflags = [
3+
"--cfg", "getrandom_backend=\"wasm_js\"",
4+
"-C", "target-feature=+atomics,+bulk-memory",
5+
"-C", "link-arg=--shared-memory",
6+
"-C", "link-arg=--max-memory=1073741824",
7+
"-C", "link-arg=--import-memory",
8+
"-C", "link-arg=--export=__wasm_init_tls",
9+
"-C", "link-arg=--export=__tls_size",
10+
"-C", "link-arg=--export=__tls_align",
11+
"-C", "link-arg=--export=__tls_base"
12+
]
13+
runner = "wasm-bindgen-test-runner"
14+
15+
[unstable]
16+
build-std = ["std", "panic_abort"]

crates/wasmi-plugin-coordinator/Cargo.toml

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,18 @@ keywords.workspace = true
1212
categories.workspace = true
1313
exclude.workspace = true
1414

15-
[dependencies]
15+
[lib]
16+
crate-type = ["cdylib"]
1617

17-
[target.'cfg(all(target_arch = "wasm32", target_os = "unknown"))'.dependencies]
18-
gloo-timers = { workspace = true }
19-
20-
[target.'cfg(not(all(target_arch = "wasm32", target_os = "unknown")))'.dependencies]
21-
tokio = { workspace = true }
18+
[target.'cfg(target_arch = "wasm32")'.dependencies]
19+
futures = { workspace = true }
20+
thiserror = { workspace = true }
21+
tracing = { workspace = true }
22+
wasm-bindgen = { workspace = true }
23+
wasm-bindgen-futures = { workspace = true }
24+
web-sys = { workspace = true, features = ["Worker", "WorkerOptions", "DedicatedWorkerGlobalScope", "WorkerType", "MessageEvent", "ErrorEvent", "Blob", "BlobPropertyBag", "Url", "console", "TextDecoder"] }
25+
serde = { workspace = true }
26+
serde-wasm-bindgen = { workspace = true }
27+
wasmi-plugin-hdk = { workspace = true }
28+
wasmer = { workspace = true, features = ["js-default", "js-serializable-module"] }
29+
tracing-wasm = { workspace = true }

crates/wasmi-plugin-coordinator/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ Essentially a workarround for 3's issues. By having a coordinator worker that co
7373

7474
The primary issue here is software architecture. Deciding how to split responsibilities between the main thread and coordinator.
7575
- If `host` is entirely in the coordinator, then the main thread must make a postMessage call for every host interaction, even those that don't involve compute workers. This probably won't be too slow, but will be annoying to setup (wasm-specific proxy that'll pass calls back and forth).
76-
- If `host` is in the main thread, and only sends requests to the coordinator when compute workers are involved, then the coordinator will need to query the host whenever any computer worker needs to interact with the host. However, this will introduce similar problems to those outlined in the asyncify section and so is probably unacceptable.
76+
- If `host` is in the main thread, and only sends requests to the coordinator when compute workers are involved, then the coordinator will need to query the host whenever any computer worker needs to interact with the host.
7777

78-
Looks like we have our winner - Seperate coordinator with the host entirely in the coordinator! ~Yay to middleware~
78+
I'm going to go with the latter approach since it seems cleaner. There is some significant added latency in messages vs shared memory for inter-plugin communication, but it also results in a much much much simpler architecture and UX for consumers of the library.
79+
- Namely - with the former approach, it would require placing consumer code in the coordinator worker. That means consumers of the library would need to write and compile code for the coordinator, which means setting up rust nightly, atomics + shared memory, and having a seperate crate. With the latter I can just bundle the compiled generic `.wasm` coordinator with the library for use as-is.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#![cfg(target_arch = "wasm32")]
2+
3+
mod worker;
4+
mod worker_pool;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// console.log = function (...args) {
2+
// self.postMessage({ type: 'Log', message: `${args.join(' ')}` });
3+
// };
4+
5+
// console.error = function (...args) {
6+
// self.postMessage({ type: 'Log', message: `${args.join(' ')}` });
7+
// };
8+
9+
console.log("Worker JS initialized");
10+
11+
import init, { start_coordinator } from "{sdk_url}";
12+
13+
console.log("JS Glue loaded, initializing wasm...");
14+
15+
init('{wasm_url}').then(() => {
16+
start_coordinator();
17+
console.log("Wasm module loaded, starting worker...");
18+
}).catch((err) => {
19+
console.error("Worker Init Failed:", err);
20+
});

0 commit comments

Comments
 (0)