generated from fast/template
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: tison <[email protected]>
- Loading branch information
Showing
11 changed files
with
303 additions
and
269 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright 2024 FastLabs Developers | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use std::future::Future; | ||
use std::sync::Arc; | ||
use std::time::Duration; | ||
|
||
use fastimer::tokio::MakeTokioDelay; | ||
use mea::latch::Latch; | ||
use mea::waitgroup::WaitGroup; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct Shutdown { | ||
latch: Arc<Latch>, | ||
wg: WaitGroup, | ||
} | ||
|
||
impl Shutdown { | ||
pub fn new() -> Self { | ||
Shutdown { | ||
latch: Arc::new(Latch::new(1)), | ||
wg: WaitGroup::new(), | ||
} | ||
} | ||
|
||
pub fn shutdown(&self) { | ||
self.latch.count_down(); | ||
} | ||
|
||
pub async fn is_shutdown(&self) { | ||
self.latch.wait().await; | ||
} | ||
|
||
pub async fn await_shutdown(self) { | ||
self.wg.await; | ||
} | ||
} | ||
|
||
pub async fn timeout(fut: impl Future) { | ||
const T: Duration = Duration::from_secs(10); | ||
fastimer::timeout(T, fut, MakeTokioDelay).await.unwrap(); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Copyright 2024 FastLabs Developers | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use std::future::Future; | ||
use std::time::Duration; | ||
use std::time::Instant; | ||
|
||
use fastimer::make_instant_from_now; | ||
use fastimer::schedule::ArbitraryDelayAction; | ||
use fastimer::schedule::ArbitraryDelayActionExt; | ||
use fastimer::schedule::BaseAction; | ||
use fastimer::tokio::MakeTokioDelay; | ||
use fastimer::tokio::TokioSpawn; | ||
|
||
use crate::common::Shutdown; | ||
|
||
mod common; | ||
|
||
#[derive(Debug)] | ||
struct TickAction { | ||
count: u32, | ||
shutdown: Shutdown, | ||
} | ||
|
||
impl BaseAction for TickAction { | ||
fn name(&self) -> &str { | ||
"tick-arbitrary" | ||
} | ||
|
||
fn is_shutdown(&self) -> impl Future<Output = ()> + Send { | ||
self.shutdown.is_shutdown() | ||
} | ||
} | ||
|
||
impl ArbitraryDelayAction for TickAction { | ||
async fn run(&mut self) -> Instant { | ||
self.count += 1; | ||
log::info!("tick: {}", self.count); | ||
make_instant_from_now(Duration::from_secs(self.count as u64)) | ||
} | ||
} | ||
|
||
fn main() { | ||
logforth::stderr().apply(); | ||
|
||
let shutdown = Shutdown::new(); | ||
let tick = TickAction { | ||
count: 0, | ||
shutdown: shutdown.clone(), | ||
}; | ||
|
||
let rt = tokio::runtime::Runtime::new().unwrap(); | ||
rt.block_on(async move { | ||
tick.schedule( | ||
&TokioSpawn::current(), | ||
MakeTokioDelay, | ||
Some(Duration::from_secs(1)), | ||
); | ||
|
||
tokio::time::sleep(Duration::from_secs(10)).await; | ||
shutdown.shutdown(); | ||
common::timeout(shutdown.await_shutdown()).await; | ||
}); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Copyright 2024 FastLabs Developers | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use std::future::Future; | ||
use std::time::Duration; | ||
|
||
use fastimer::schedule::BaseAction; | ||
use fastimer::schedule::SimpleAction; | ||
use fastimer::schedule::SimpleActionExt; | ||
use fastimer::tokio::MakeTokioDelay; | ||
use fastimer::tokio::TokioSpawn; | ||
|
||
use crate::common::Shutdown; | ||
|
||
mod common; | ||
|
||
#[derive(Debug)] | ||
struct TickAction { | ||
name: String, | ||
count: u32, | ||
sleep: Duration, | ||
shutdown: Shutdown, | ||
} | ||
|
||
impl BaseAction for TickAction { | ||
fn name(&self) -> &str { | ||
&self.name | ||
} | ||
|
||
fn is_shutdown(&self) -> impl Future<Output = ()> + Send { | ||
self.shutdown.is_shutdown() | ||
} | ||
} | ||
|
||
impl SimpleAction for TickAction { | ||
async fn run(&mut self) { | ||
self.count += 1; | ||
log::info!("[{}] tick start: {}", self.name(), self.count); | ||
tokio::time::sleep(self.sleep).await; | ||
log::info!("[{}] tick end: {}", self.name(), self.count); | ||
} | ||
} | ||
|
||
async fn do_schedule_at_fixed_rate(sleep: u64, period: u64) { | ||
let shutdown = Shutdown::new(); | ||
let tick = TickAction { | ||
name: format!("fixed-rate-{sleep}/{period}"), | ||
count: 0, | ||
sleep: Duration::from_secs(sleep), | ||
shutdown: shutdown.clone(), | ||
}; | ||
tick.schedule_at_fixed_rate( | ||
&TokioSpawn::current(), | ||
MakeTokioDelay, | ||
None, | ||
Duration::from_secs(period), | ||
); | ||
tokio::time::sleep(Duration::from_secs(10)).await; | ||
shutdown.shutdown(); | ||
common::timeout(shutdown.await_shutdown()).await; | ||
} | ||
|
||
fn main() { | ||
logforth::stderr().apply(); | ||
|
||
let rt = tokio::runtime::Runtime::new().unwrap(); | ||
rt.block_on(do_schedule_at_fixed_rate(1, 2)); | ||
rt.block_on(do_schedule_at_fixed_rate(3, 2)); | ||
rt.block_on(do_schedule_at_fixed_rate(5, 2)); | ||
} |
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.