如何从函数内部向actix SyncArbiter发送消息?

时间:2020-02-24 22:10:35

标签: rust rust-actix actix-web

我正在尝试实现将请求发送到Actix r2d2池(DbExecutor)单例的功能,该函数处理请求并返回可在应用程序(音乐应用程序)中使用的响应。

因为我要在整个应用程序中使用此DBExecutioner,所以对我来说,实现与模型表示形式不同的响应没有任何意义。 我正在尝试从数据库池中获得直接响应。

我已经为DbExecutor实现了一个处理程序,如下所示。

首先,我创建数据库池的单例表示形式:

db / mod.rs

pub fn shared_store() -> Store {
    let store = init_store();
    let store = match store {
        Some(store) => store,
        None => shared_store() // TODO wait for period
    };
    store
}

fn init_store() -> Option<Store> {
  ...
}

然后,我为此DbExecutor实例隐式创建处理程序。即当消息发送到DbExecutor时,它将提供从查询数据库中得出的匹配响应。

db/instruments.rs

#[derive(Debug)]
pub struct GetInstrumentList {}

#[derive(Debug)]
pub struct InstrumentListResponse {
    pub instruments: Vec<Instrument>,
}

impl Message for GetInstrumentList {
    type Result = Result<InstrumentListResponse>;
}

impl Handler<GetInstrumentList> for DbExecutor {
    type Result = Result<InstrumentListResponse>;

    fn handle(&mut self, msg: GetInstrumentList, _: &mut Self::Context) -> Self::Result {
       ... ommitted for brevity
    }
}

实现以下功能时出现我的问题。它应该从Instrument返回一个SyncArbiter响应,但是它返回不合格的结果,如以下代码所示的响应所示:

service/instrument.rs

pub fn get_instruments_list(limit: Option<usize>, offset: Option<usize>) -> InstrumentListResponse {
    let db = shared_store().db.clone();
    let res = db.send(GetInstrumentList{
        params: InstrumentListParams{
            limit:limit,
            offset: offset
        }
    })
    .flatten().wait();

    res
}

45 | RES | ^^^预期结构 db::instruments::InstrumentListResponse,已找到枚举 std::result::Result | =注意:预期结构 db::instruments::InstrumentListResponse 找到枚举std::result::Result<db::instruments::InstrumentListResponse, error::Error>

如何以解决此错误的方式实现上述功能?

0 个答案:

没有答案