Skip to main content

vmgs_broker/
lib.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! A task + RPC client for interacting with a shared VMGS instance.
5
6#![forbid(unsafe_code)]
7
8mod broker;
9mod client;
10pub mod non_volatile_store;
11pub mod resolver;
12
13pub use broker::VmgsBrokerError;
14pub use client::VmgsClient;
15pub use client::VmgsClientError;
16
17use crate::broker::VmgsBrokerTask;
18use pal_async::task::Spawn;
19use pal_async::task::Task;
20
21/// Given a fully-initialized VMGS instance, return a VMGS broker task +
22/// clonable VmgsClient
23pub fn spawn_vmgs_broker(spawner: impl Spawn, vmgs: vmgs::Vmgs) -> (VmgsClient, Task<()>) {
24    let (control_send, control_recv) = mesh::mpsc_channel();
25
26    let process_loop_handle = spawner.spawn("vmgs-broker", async move {
27        VmgsBrokerTask::new(vmgs).run(control_recv).await
28    });
29
30    (
31        VmgsClient {
32            control: control_send,
33        },
34        process_loop_handle,
35    )
36}
37
38/// A wrapper around [`VmgsClient`] that restricts its API down to operations
39/// that perform no storage IO.
40///
41/// This types is useful for keeping performance-sensitive code "honest" by
42/// making it harder for future refactors to accidentally introduce VMGS IO into
43/// performance hotpaths.
44#[derive(inspect::Inspect)]
45#[inspect(transparent)]
46pub struct VmgsThinClient(VmgsClient);
47
48impl VmgsThinClient {
49    /// Restrict an existing [`VmgsClient`] to only non-IO operations.
50    pub fn new(vmgs_client: VmgsClient) -> Self {
51        Self(vmgs_client)
52    }
53
54    /// See [`VmgsClient::save`]
55    pub async fn save(&self) -> Result<vmgs::save_restore::state::SavedVmgsState, VmgsClientError> {
56        self.0.save().await
57    }
58}