21个版本

0.9.2 2021年10月26日
0.9.1 2021年7月18日
0.9.0 2021年4月3日
0.8.1 2021年1月10日
0.2.0 2018年10月15日

#spirit 中排名第 1

Download history 31/week @ 2024-03-27 42/week @ 2024-04-03

每月下载量 90
2 个crate 使用

Apache-2.0 OR MIT

410KB
5.5K SLoC

Spirit-tokio

Travis Build Status

提供了一些辅助工具,便于将tokio与由spirit系统管理的配置集成。

请参阅文档示例

许可证

以下任一许可证下发布

任选其一。

贡献

除非你明确声明,否则你有意提交的任何贡献,根据Apache-2.0许可证的定义,均应按上述方式双许可,不附加任何额外条款或条件。


lib.rs:

在Spirit中支持tokio

这提供了tokio运行时的配置和future的安装。

它还提供了一些用于配置网络原语的配置Fragment

注意,这启用了Tokio::FromCfg变体和Config

功能

示例

use std::future::Future;
use std::pin::Pin;
use std::time::Duration;

use err_context::AnyError;
use serde::{Deserialize, Serialize};
use spirit::{Empty, Pipeline, Spirit};
use spirit::prelude::*;
use spirit::fragment::driver::CacheEq;
use spirit_tokio::{FutureInstaller, Tokio};
use spirit_tokio::runtime::Config as TokioCfg;
use structdoc::StructDoc;

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize, StructDoc)]
#[serde(default)]
struct MsgCfg {
    /// A message to print now and then.
    msg: String,
    /// Time between printing the message.
    interval: Duration,
}

impl MsgCfg {
    async fn run(self) {
        loop {
            println!("{}", self.msg);
            tokio::time::sleep(self.interval).await;
        }
    }
}

impl Default for MsgCfg {
    fn default() -> Self {
        MsgCfg {
            msg: "Hello".to_owned(),
            interval: Duration::from_secs(1),
        }
    }
}

spirit::simple_fragment! {
    impl Fragment for MsgCfg {
        type Driver = CacheEq<MsgCfg>;
        type Resource = Pin<Box<dyn Future<Output = ()> + Send>>;
        type Installer = FutureInstaller;
        fn create(&self, _: &'static str) -> Result<Self::Resource, AnyError> {
            let fut = self.clone().run();
            Ok(Box::pin(fut))
        }
    }
}

/// An application.
#[derive(Default, Deserialize, Serialize, StructDoc)]
struct AppConfig {
    #[serde(flatten)]
    msg: MsgCfg,

    /// Configuration of the asynchronous tokio runtime.
    #[serde(default)]
    threadpool: TokioCfg,
}

impl AppConfig {
    fn threadpool(&self) -> TokioCfg {
        self.threadpool.clone()
    }

    fn msg(&self) -> &MsgCfg {
        &self.msg
    }
}

fn main() {
    Spirit::<Empty, AppConfig>::new()
        // Makes sure we have a runtime configured from the config.
        // If we don't do this, the pipeline below would insert a default Tokio runtime to make
        // it work. If you want to customize the runtime (like here), make sure to insert it
        // before any pipelines requiring it (otherwise you get the default one from them).
        .with_singleton(Tokio::from_cfg(AppConfig::threadpool))
        // Will install and possibly cancel and replace the future if the config changes.
        .with(Pipeline::new("Msg").extract_cfg(AppConfig::msg))
        // Just an empty body here.
        .run(|spirit| {
            // Usually, one would terminate by CTRL+C, but we terminate from here to make sure
            // the example finishes.
            spirit.terminate();
            Ok(())
        })
}

另一种方法可以参见 handlers::ToFutureUnconfigured

依赖项

~5–15MB
~172K SLoC