Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add own abstraction layer over tokio::time::Interval #2318

Merged
merged 9 commits into from
Jun 3, 2024

Conversation

sisou
Copy link
Member

@sisou sisou commented Mar 16, 2024

Using gloo-timers for WASM. This change gets rid of the unmaintained wasm-timer crate, of which we were already using a fork with a fix from me anyway.

The resulting abstraction layer is very thin and since it exposes the exact same interface for both native and WASM targets, chunks of code that concerned themselves with the differentiation can be removed.

Pull request checklist

  • All tests pass. The project builds and runs.
  • I have resolved any merge conflicts.
  • I have resolved all clippy and rustfmt warnings.

@sisou sisou added refactor Refactoring of some component WASM labels Mar 16, 2024
@sisou sisou requested review from styppo and hrxi March 16, 2024 16:48
@sisou sisou self-assigned this Mar 16, 2024
@sisou
Copy link
Member Author

sisou commented Mar 17, 2024

I looked at the failing test it_can_limit_requests_rate: Before my force-push, which fixed most intervals to the previous behavior of having their first tick only after the first duration, it failed at the first limit reset already with a ConnectionClosed error. After, it only fails at the third limit reset. However, it's still not failing with limit-reset related problem, but because of a disconnecting peer.

I did not yet investigate the other failing test it_can_reset_requests_rate_with_reconnections.

@styppo styppo force-pushed the sisou/nimiq-time branch 3 times, most recently from ea2ef23 to c658661 Compare April 10, 2024 20:04
@sisou
Copy link
Member Author

sisou commented Apr 11, 2024

@styppo you are ignoring two request-response tests in your commits here. Is there no way to enable them again? Or will you create a tracking issue for them?

Copy link
Member

@hrxi hrxi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found a small bug in test code. Plus, as @sisou mentions, tests seem to be ignored.

handel/tests/mod.rs Outdated Show resolved Hide resolved
network-libp2p/src/network.rs Outdated Show resolved Hide resolved
use gloo_timers::future::{IntervalStream, TimeoutFuture};
use send_wrapper::SendWrapper;

pub type Interval = SendWrapper<IntervalStream>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the send_wrapper docs:

This Rust library implements a wrapper type called SendWrapper which allows you to move around non-Send types between threads, as long as you access the contained value only from within the original thread.

This means we're locking our wasm implementation to one thread. I guess that's okay.

time/src/tokio.rs Show resolved Hide resolved
@@ -66,7 +66,9 @@ pub async fn it_can_initialize_with_mock_network() {
}),
);

spawn_local(consensus);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this removed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the NodeJS test runner was not exiting after the test completed successfully, because of pending timeouts or intervals. We tried to work around it by not spawning the consensus, but only polling it once (the added lines after this removed line), but that didn't work really either.

I have now changed the WASM tests to run in headless Chrome and there the test runner exits correctly, so I have added this back in.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After the last rebase of this PR by @styppo the commit reverting this is gone. The relevant changes to the test runner are now in PR #2513. Once that is merged, this PR can be rebased on top and this change here reverted.

@sisou sisou changed the base branch from albatross to styppo/spawn May 8, 2024 22:22
@@ -17,6 +17,7 @@ workspace = true
async-trait = "0.1"
futures = { package = "futures-util", version = "0.3", features = ["sink"] }
log = { package = "tracing", version = "0.1", features = ["log"] }
instant = { version = "0.1", features = [ "wasm-bindgen" ] }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd add the wasm-bindgen feature as a feature of this crate:

[features]
wasm-bindgen = ["instant/wasm-bindgen"]

network-libp2p/src/network.rs Outdated Show resolved Hide resolved
network-libp2p/Cargo.toml Show resolved Hide resolved
network-libp2p/tests/request_response.rs Outdated Show resolved Hide resolved
network-libp2p/tests/request_response.rs Outdated Show resolved Hide resolved

#[cfg(target_family = "wasm")]
pub use gloo::*;
#[cfg(not(target_family = "wasm"))]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd do it differently by putting this into the Cargo.toml such that these dependencies get included only depending on the target. For this to work, the Cargo.toml should look like this:

[dependencies]
[target.'cfg(target_arch = "wasm32")'.dependencies]
gloo-timers = { version = "0.2", features = ["futures"], optional = true }
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
tokio = { version = "1.36", features = ["time"], optional = true }
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
tokio-stream = { version = "0.1", features = ["time"], optional = true }

Then the code here could just use the features tokio, tokio-stream and gloo for enabling code here.

@@ -21,6 +21,7 @@ workspace = true
async-trait = "0.1"
byteorder = "1.5"
futures = { package = "futures-util", version = "0.3" }
instant = { version = "0.1", features = [ "wasm-bindgen" ] }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd add a feature wasm-bindgen that selects this:

[features]
wasm-bindgen = ["instant/wasm-bindgen"]

@styppo styppo added this to the Nimiq PoS Mainnet milestone May 20, 2024
@sisou sisou changed the base branch from styppo/spawn to albatross May 20, 2024 19:24
@sisou
Copy link
Member Author

sisou commented May 20, 2024

@styppo you missed a3fb606 when rebasing.

@styppo styppo force-pushed the sisou/nimiq-time branch 2 times, most recently from e6f05dd to 7c4ad3d Compare May 27, 2024 11:20
@styppo styppo merged commit bdc0721 into albatross Jun 3, 2024
6 checks passed
@styppo styppo deleted the sisou/nimiq-time branch June 3, 2024 12:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
refactor Refactoring of some component WASM
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants