dyn-timeout

Crates.iodyn-timeout
lib.rsdyn-timeout
version0.3.3
sourcesrc
created_at2022-01-03 12:39:55.297891
updated_at2022-04-04 15:20:16.57007
descriptionDynamic timeout, cancel, add and remove time before a callback execution
homepage
repositoryhttps://github.com/adrien-zinger/dyn-timeout
max_upload_size
id507091
size61,105
Adrien Zinger (adrien-zinger)

documentation

README

CI

Dynamic Timeout

Execute a function after a mutable duration.

use std::time::Duration;
use dyn_timeout::std_thread::DynTimeout;

const TWENTY: Duration = Duration::from_millis(20);

let dyn_timeout = DynTimeout::new(TWENTY, || {
   println!("after forty milliseconds");
});
dyn_timeout.add(TWENTY).unwrap();
// .sub...
// .cancel

This library was initially implemented to be used as a raft like election timeout.

Tokio version

This crate include a std with threads and a tokio implementation, usefull if you're already using this async library.

use tokio::runtime::Runtime;
use dyn_timeout::tokio_impl::DynTimeout;
use std::time::Duration;
const TWENTY: Duration = Duration::from_millis(20);

let mut rt = Runtime::new().unwrap(); 
rt.spawn(async {
   let dyn_timeout = DynTimeout::new(TWENTY, || {
       println!("after forty milliseconds");
   });
   dyn_timeout.add(TWENTY).await.unwrap();
});

Benchmark

Here is the bench with 40 milliseconds to wait with the standard implementation, under the nanoseconds the time precision decrease. (Using tokio decrease also the precision)

test test::simple_bench ... bench:  40,641,475 ns/iter (+/- 68,064)

Contribute

  • All increases of the time precision and code architecture are welcomes.
  • Usage examples, documentation, typo and comments, unit tests.
  • All interestings ideas are also good to know in an issue.
  • Give your feedback, report errors and bugs in a github issue.

All development contribution, please, has to pass the currents unit tests and it's a must to include a new test!


License GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007

This lirary is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

Commit count: 24

cargo fmt