a43d183b4f
* Compiles but runtime time fails * wip * Beginning of clean-up - creation of config to keep things together * Removed unused module * Removed hardcoded constants * Easier way of sending binary messages * WIP cleanup before machine switch * Upgrade wasm-bindgen to 0.2.83 * Fixed compilation warnings for wasm client * all clients compiling without warnings * disabling topology refresh in wasm * Added a config option to disable loop cover traffic stream * config changes * Make webassembly work in a web worker - `wasm-timer` modified to work in web worker - add worker target to webpack - add client to call from HTML - update README to build WASM for bundling (this does not build ES modules) * Restored topology refreshing * correctly polling items in the wasm delay_queue * Allow client to read up to 8 messages at once from gateway connection (#1669) * Allow client to read up to 8 messages at once from gateway connection * Importing tokio::select in wasm32 target * Updated changelog * missing imports * Introduced disable_main_poisson_packet_distribution to force real_traffic_stream to disable poisson sending (#1664) * Introduced disable_main_poisson_packet_distribution to force real_traffic_stream to disable poisson sending * Updated changelog * Adjusting default settings * Introduced a client-configurable option to force it to use extended packet size * local adjustments * Removed warning associated with receiving extended packets * Minimal v2-required changes * Updated changelog * explicitly allowing clippy drop_non_drop Co-authored-by: Mark Sinclair <mmsinclair@gmail.com>
102 lines
3.1 KiB
Rust
102 lines
3.1 KiB
Rust
// Copyright 2021 - Nym Technologies SA <contact@nymtech.net>
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
use std::pin::Pin;
|
|
use std::task::{Context, Poll, Waker};
|
|
use std::time::Duration;
|
|
use tokio_stream::Stream;
|
|
|
|
pub use tokio::time::error::Error as TimerError;
|
|
|
|
// this is a copy of tokio-util delay_queue with `Sleep` and `Instant` being replaced with
|
|
// `wasm_timer` equivalents
|
|
#[cfg(target_arch = "wasm32")]
|
|
mod wasm_delay_queue;
|
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
type DelayQueue<T> = tokio_util::time::DelayQueue<T>;
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
pub use tokio_util::time::delay_queue::Expired;
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
pub type QueueKey = tokio_util::time::delay_queue::Key;
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
use tokio::time::Instant;
|
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
type DelayQueue<T> = crate::wasm_delay_queue::DelayQueue<T>;
|
|
#[cfg(target_arch = "wasm32")]
|
|
pub use crate::wasm_delay_queue::delay_queue::Expired;
|
|
#[cfg(target_arch = "wasm32")]
|
|
pub type QueueKey = crate::wasm_delay_queue::delay_queue::Key;
|
|
#[cfg(target_arch = "wasm32")]
|
|
use wasm_timer::Instant;
|
|
|
|
/// A variant of tokio's `DelayQueue`, such that its `Stream` implementation will never return a 'None'.
|
|
pub struct NonExhaustiveDelayQueue<T> {
|
|
inner: DelayQueue<T>,
|
|
waker: Option<Waker>,
|
|
}
|
|
|
|
// more methods of underlying DelayQueue will get exposed as we need them
|
|
impl<T> NonExhaustiveDelayQueue<T> {
|
|
pub fn new() -> Self {
|
|
NonExhaustiveDelayQueue {
|
|
inner: DelayQueue::new(),
|
|
waker: None,
|
|
}
|
|
}
|
|
|
|
pub fn insert(&mut self, value: T, timeout: Duration) -> QueueKey {
|
|
let key = self.inner.insert(value, timeout);
|
|
if let Some(waker) = self.waker.take() {
|
|
// we were waiting for an item - wake the executor!
|
|
waker.wake()
|
|
}
|
|
key
|
|
}
|
|
|
|
pub fn insert_at(&mut self, value: T, when: Instant) -> QueueKey {
|
|
let key = self.inner.insert_at(value, when);
|
|
if let Some(waker) = self.waker.take() {
|
|
// we were waiting for an item - wake the executor!
|
|
waker.wake()
|
|
}
|
|
key
|
|
}
|
|
|
|
// TODO: it seems like this one can cause panic in very rare edge cases, however,
|
|
// I can't seem to be able to reproduce it at all.
|
|
pub fn remove(&mut self, key: &QueueKey) -> Expired<T> {
|
|
self.inner.remove(key)
|
|
}
|
|
}
|
|
|
|
impl<T> Default for NonExhaustiveDelayQueue<T> {
|
|
fn default() -> Self {
|
|
NonExhaustiveDelayQueue::new()
|
|
}
|
|
}
|
|
|
|
impl<T> Stream for NonExhaustiveDelayQueue<T> {
|
|
type Item = <DelayQueue<T> as Stream>::Item;
|
|
|
|
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
|
match Pin::new(&mut self.inner).poll_next(cx) {
|
|
Poll::Pending => Poll::Pending,
|
|
Poll::Ready(Some(item)) => Poll::Ready(Some(item)),
|
|
Poll::Ready(None) => {
|
|
// we'll need to keep the waker to notify the executor once we get new item
|
|
self.waker = Some(cx.waker().clone());
|
|
Poll::Pending
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// #[cfg(test)]
|
|
// mod tests {
|
|
// use super::*;
|
|
//
|
|
//
|
|
// }
|