ポリシー間のデータ共有

StreamProperties​ Injectable は、以下の目的のインターフェースを提供しています。

  • 同じ要求を処理している他のポリシーによって設定されたプロパティをコンシュームする。

  • プロパティをブロードキャストして、他のポリシーがコンシュームできるようにする。

データを共有するためには、​StreamProperties​ で ​PropertyAccessor​ trait を実装します。

pub trait PropertyAccessor {
    fn read_property(&self, path: &[&str]) -> Option<Bytes>;
    fn set_property(&self, path: &[&str], value: Option<&[u8]>);
}

次の例は、ストリームからプロパティを読み取り、別のパラメーターを書き込む単純な検索条件を示しています。

async fn request_filter(stream: StreamProperties) -> Flow<()> {
    let incoming = String::from_utf8(stream.read_property(&["incoming_property"]).unwrap_or_default()).unwrap_or_default();

    logger::info!("Recieved incoming prop {}", incoming);

    let outgoing = "outgoing".as_bytes();

    stream.set_property(&["outgoing_property"], Some(outgoing));

    Flow::Continue(())
}

#[entrypoint]
async fn configure(launcher: Launcher) -> Result<()> {
    let filter = on_request(|stream| request_filter(stream));
    launcher.launch(filter).await?;
    Ok(())
}