-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Make rs-wnfs work in multithreaded contexts (#372)
The main goal of this PR is to enable using rs-wnfs in multithreaded contexts, e.g. in axum webservers. We have a test repo to check whether that works in an MVP: https://github.com/fabricedesre/wnfs-mtload/ Previously that wasn't possible, since the async futures from rs-wnfs weren't `Send`, so you couldn't have a multi-threaded work-stealing async runtime work on them. The reasons they weren't sync were: - Futures would capture an `impl BlockStore`, and it's not necessarily known to be `Send` - Futures would capture an `impl PrivateForest` with the same problem - Some functions would produce `LocalBoxFuture` or `LocalBoxStream`, which aren't `Send` - We'd use `async_trait(?Send)` and `async_recursion(?Send)` which opt-in to not having `Send` bounds, since that's what we need for wasm - Futures would capture internal WNFS data structures like `PrivateNode`, which would use `Rc` internally instead of `Arc`, see also #250 Some of this work was already addressed in #366. This PR *should* cover the rest. --- There's a complication with Wasm, where we're e.g. using an external type `extern "C" { type BlockStore; ... }`, which isn't `Send` or `Sync`, and as such can't ever implement a `trait BlockStore: Send + Sync`. To fix this, we're conditionally compiling in `Send` and `Sync` bounds (and `Arc` and `Rc` and similar) based on the target (See `send_sync_poly.rs`). This is pretty much just copying what noosphere is doing: https://github.com/subconsciousnetwork/noosphere/blob/main/rust/noosphere-common/src/sync.rs I'm hoping eventually we just fix this and thus enable multi-threaded Wasm, too. But for now this works. --- * wip - still need to fix the SnapshotBlockStore implementation * Fix SnapshotBlockStore impl * Fix wnfs=hamt * fix: `Send` bounds & `BytesToIpld` implementations Also: fix formatting * feat: Also make `PrivateForest` trait `Send + Sync` Also: Remove unneeded `Sync` bounds left over from previous commits. * feat: Use `BoxFuture` instead of `LocalBoxFuture` in `PrivateForest` fn * feat: Remove `(?Send)` annotations everywhere * feat: Conditionally compile `Send + Sync` bounds This relaxes the requirement if you're not on the `wasm32` target. The problem is that foreign types in Wasm don't implement `Sync`. * chore: Fix all tests & doctests to use thread-safe RNGs --------- Co-authored-by: Fabrice Desré <fabrice@desre.org>
- Loading branch information
1 parent
30ed01b
commit 98d43cb
Showing
54 changed files
with
764 additions
and
491 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.