maf 0.1.0-alpha.6

MAF is an authoritative realtime framework for writing simple, secure, and scalable apps.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
use std::{
    any::{Any, TypeId},
    sync::{atomic::AtomicBool, Arc},
};

#[cfg(feature = "typed")]
use schemars::{JsonSchema, SchemaGenerator};
use serde::Serialize;
use tokio::sync::{
    OwnedRwLockReadGuard, OwnedRwLockWriteGuard, RwLock, RwLockReadGuard, RwLockWriteGuard,
};

use crate::{
    callable::{CallableFetch, CallableParam, SupportsAsync},
    store::pointers::OwnedStoreWriteLock,
    App, User,
};

use super::pointers::StoreWriteLock;

#[repr(transparent)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct StoreId(pub(crate) TypeId);

impl StoreId {
    pub fn of<T: 'static>() -> Self {
        StoreId(TypeId::of::<T>())
    }
}

/// A type-erased store that can hold any data implementing [`StoreData`].
#[derive(Clone)]
pub struct AnyStore {
    /// A unique identifier for the store. This is the [`TypeId`] of the store's data type.
    pub(crate) id: StoreId,
    /// The name of the store, used to identify it to clients.
    pub(crate) name: Arc<str>,
    /// Indicates whether the store's data has been modified and needs to be synchronized with
    /// clients.
    pub(crate) dirty: Arc<AtomicBool>,
    /// The store's data is stored as a type-erased [`RwLock`].
    pub(crate) data: Arc<RwLock<dyn Any + Send + Sync>>,
    /// Function to serialize the store's data for a given user. If serialization fails, returns a
    /// [`StoreSerializeError`].
    pub(crate) serializer: Arc<
        dyn Fn(&dyn Any, &User) -> Result<serde_json::Value, StoreSerializeError>
            + Send
            + Sync
            + 'static,
    >,

    #[cfg(feature = "typed")]
    pub(crate) desc:
        Arc<dyn Fn(&mut SchemaGenerator) -> crate::typed::StoreDesc + Send + Sync + 'static>,
}

impl std::fmt::Debug for AnyStore {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("AnyStore")
            .field("id", &self.id)
            .field("dirty", &self.dirty)
            .field("data", &self.data)
            .finish_non_exhaustive()
    }
}

#[derive(Debug, thiserror::Error)]
pub enum StoreSerializeError {
    #[error("failed to serialize store data: {0}")]
    Serialize(#[from] serde_json::Error),
}

/// [`Store`] is a wrapper around shared data that can be accessed throughout the app and
/// **synchronized with connected clients**.
///
/// The data stored in [`Store`] must implement [`StoreData`], describing initialization and access
/// methods.
///
/// ## Usage
/// ```rust
/// use maf::prelude::*;
///
/// // [1]: Define struct to hold store data
/// struct Counter {
///     count: u32,
/// }
///
/// // [2]: Implement StoreData for the struct
/// impl StoreData for Counter {
///     // ... see StoreData docs for more info ...
/// }
///
/// // [3]: Use your store in a callable function
/// fn increment_counter(mut counter: StoreMut<Counter>) {
///     counter.count += 1;
/// }
///
/// fn build() -> App {
///     App::builder()
///         .rpc("increment_counter", increment_counter)
///         // [4]: Create and register the store in your app
///         .store::<Counter>()
///         .build()
/// }
///
/// maf::register!(build);
/// ```
///
/// ## [`super::StoreRef`] and [`super::StoreMut`]
/// [`super::StoreRef`] and [`super::StoreMut`] provide read-only and mutable access to the store's
/// data within callable functions (e.g. RPC methods, `on_connect` handlers, etc). When using these
/// types, no async functions or locking is needed--MAF handles acquiring and releasing locks for
/// you. **[super::StoreRef] and [super::StoreMut]** cannot be used in async callables as an effort
/// to prevent lock contention and deadlocks.
///
/// ```rust
/// use maf::prelude::*;
///
/// struct Counter {
///     count: u32,
/// }
///
/// impl StoreData for Counter {
///     // ...
/// }
///
/// // No async function or locking is needed!
/// fn add_count(mut counter: StoreMut<Counter>) {
///     counter.count += 1;
/// }
///
/// // ...
/// ```
///
/// Under the hood, [`super::StoreRef`] acquires a read lock on the store's data for the duration of
/// the callable, while [`super::StoreMut`] acquires a write lock.
///
/// ## 🔗🔗🔗 Beware of Lock Contention!
/// [`Store`] uses [`RwLock`] internally to allow concurrent read access and exclusive write access.
/// However, if a callable function (e.g. an RPC method or an `on_connect` handler) holds a lock
/// on a store and then awaits an async operation, it may lead to deadlocks and other slowness if
/// other callables try to access the same store.
///
/// As an example of what **NOT** to do:
/// ```rust
/// struct Game {
///     is_powerup_active: bool,
///     health: u32,
/// }
///
/// impl StoreData for Game {
///     // ... implementation left out for brevity ...
/// }
///
/// /// Set `is_powerup_active` to true for 30 seconds, then back to false.
/// async fn bad_trigger_powerup(game: Store<Game>) {
///     // Acquires a write lock on the store's data
///     let mut game_data = game.write().await;
///     game_data.is_powerup_active = true;
///     
///     // The powerup lasts for 30 seconds
///     tasks::sleep(std::time::Duration::from_secs(30)).await;
///     game_data.is_powerup_active = false;
/// }
///
/// async fn heal_player(game: Store<Game>) {
///     // This will wait for a very long time if called while `bad_trigger_powerup` is sleeping!
///     let mut game_data = game.write().await;
///     // NOTE: Although this example uses a write lock, even a read lock or StoreRef/StoreMut
///     // would be blocked while `bad_trigger_powerup` holds the write lock.
///     game_data.health += 10;
/// }
///
/// // ... register RPCs, stores, etc.
/// ```
///
/// The problem with the above example is that `bad_trigger_powerup` holds a write lock on the
/// store's data while it awaits the 30-second sleep. During this time, any other callable that
/// tries to access the store (like `heal_player`) will be blocked, leading to potential deadlocks
/// or significant delays. To avoid this, ensure that locks are held for the shortest
/// time possible and avoid awaiting while holding locks.
///
/// A better approach would be to minimize the locked section:
/// ```rust
/// async fn good_trigger_powerup(game: Store<Game>) {
///     game_data.write().await.is_powerup_active = true;
///     tasks::sleep(std::time::Duration::from_secs(30)).await;
///     game_data.write().await.is_powerup_active = false;
/// }
/// ```
///
/// In doing this, the lock is only held while updating the `is_powerup_active` field, and not
/// during the sleep period, allowing other callables to access the store without unnecessary
/// delays.
///
/// Another strategy is to use interior mutability patterns within the store's data (e.g., using
/// `AtomicBool`, `Mutex`, etc.) to allow safe concurrent modifications without holding locks. A
/// third strategy is to split commonly locked/updating data into separate stores to reduce the
/// amount of sharing that needs to occur.
pub struct Store<T: StoreData> {
    app: App,
    pub(crate) inner: AnyStore,
    _phantom: std::marker::PhantomData<T>,
}

/// Describes the data stored in a [`Store`].
pub trait StoreData: Send + Sync + 'static {
    #[cfg(not(feature = "typed"))]
    /// The type of data selected to be serialized and sent to clients. This type must implement
    /// [`serde::Serialize`].
    type Select<'this>: Serialize;
    #[cfg(feature = "typed")]
    /// The type of data selected to be serialized and sent to clients. This type must implement
    /// [`serde::Serialize`] and [`schemars::JsonSchema`].
    type Select<'this>: Serialize + JsonSchema;

    /// Initializes the store data with a default value.
    fn init() -> Self;

    /// Returns the name of the store to be used by clients to identify it.
    ///
    /// If not specified, the default is the Rust type name of `Self`.
    fn name() -> impl AsRef<str> + Send {
        std::any::type_name::<Self>()
    }

    /// Selects the portion of the store data to be serialized and sent to the given user.
    ///
    /// The lifetime `'this` enables returning references into `self`.
    ///
    /// ## Example
    /// ```rust
    /// use maf::prelude::*;
    ///
    /// struct Message {
    ///     text: String,
    ///     secret: String,
    /// }
    ///
    /// impl StoreData for Message {
    ///     // The 'this lifetime refers to the lifetime of the data being selected and it allows
    ///     // zero-copy serialization by returning references into self. This is an optional
    ///     // feature--you can also return owned data if preferred.
    ///     type Select<'this> = &'this String;
    ///
    ///     fn init() -> Self {
    ///         Message {
    ///             text: "Hello, world!".to_string(),
    ///             secret: "This is a secret.".to_string(),
    ///         }
    ///     }
    ///
    ///     fn select(&self, user: &User) -> Self::Select<'_> {
    ///         // Send only the public text, not the secret to the client
    ///         &self.text
    ///     }
    ///
    ///     fn name() -> impl AsRef<str> {
    ///         "message"
    ///     }
    /// }
    ///
    /// ```
    #[allow(unused_variables)]
    fn select(&self, user: &User) -> Self::Select<'_>;
}

impl AnyStore {
    pub fn new<T: StoreData>() -> Self {
        Self {
            id: StoreId::of::<T>(),
            name: Arc::from(T::name().as_ref()),
            dirty: Arc::new(AtomicBool::new(false)),
            data: Arc::new(RwLock::new(T::init())),
            serializer: Arc::new(|data, user| {
                let data = data.downcast_ref::<T>().expect(&std::format!(
                    "store data is not of expected type {}",
                    std::any::type_name::<T>()
                ));

                serde_json::to_value(T::select(&data, user)).map_err(Into::into)
            }),
            #[cfg(feature = "typed")]
            desc: Arc::new(|generator| crate::typed::StoreDesc::new::<T>(generator)),
        }
    }
}

impl<T: StoreData> Store<T> {
    /// Creates a new [`Store<T>`] instance for the given app.
    ///
    /// ## Panics
    /// The type of the store should have already been registered with the app using
    /// [`crate::AppBuilder::store`]. If the store is not found, this function will panic.
    pub async fn new(app: App) -> Self {
        let id = StoreId::of::<T>();
        let inner = app
            .inner
            .state
            .stores
            .read()
            .await
            .get(&id)
            .cloned()
            .expect("store not found");

        Store {
            app,
            inner,
            _phantom: std::marker::PhantomData,
        }
    }

    /// Acquires a read lock on the store's data. Multiple read locks can be held concurrently.
    ///
    /// This creates a lock with a lifetime tied to the `Store` instance. If you need an owned lock
    /// that can outlive the `Store`, consider using [`Store::read_owned`].
    pub async fn read(&self) -> RwLockReadGuard<'_, T> {
        RwLockReadGuard::map(self.inner.data.read().await, |inner| {
            inner
                .downcast_ref::<T>()
                .expect("failed to downcast store (is the store of the right type?)")
        })
    }

    /// Acquires a write lock on the store's data. Only one write lock can be held at a time, and
    /// no read locks can be held while a write lock is active.
    ///
    /// This creates a lock with a lifetime tied to the `Store` instance. If you need an owned lock
    /// that can outlive the `Store`, consider using [`Store::write_owned`].
    pub async fn write(&self) -> StoreWriteLock<'_, T> {
        StoreWriteLock::new(
            &self.app,
            &self.inner,
            RwLockWriteGuard::map(self.inner.data.write().await, |inner| {
                inner
                    .downcast_mut::<T>()
                    .expect("failed to downcast store (is the store of the right type?)")
            }),
        )
    }

    /// Acquires an owned read lock on the store's data. Multiple read locks can be held
    /// concurrently.
    ///
    /// This creates a lock that is owned and can outlive the `Store` instance.
    pub async fn read_owned(&self) -> OwnedRwLockReadGuard<dyn Any + Send + Sync, T> {
        OwnedRwLockReadGuard::map(self.inner.data.clone().read_owned().await, |inner| {
            inner
                .downcast_ref::<T>()
                .expect("failed to downcast store (is the store of the right type?)")
        })
    }

    /// Acquires an owned write lock on the store's data. Only one write lock can be held at a
    /// time, and no read locks can be held while a write lock is active.
    ///
    /// This creates a lock that is owned and can outlive the `Store` instance.
    pub async fn write_owned(&self) -> OwnedStoreWriteLock<T> {
        OwnedStoreWriteLock {
            app: self.app.clone(),
            store: self.clone(),
            guard: OwnedRwLockWriteGuard::map(
                self.inner.data.clone().write_owned().await,
                |inner| {
                    inner
                        .downcast_mut::<T>()
                        .expect("failed to downcast store (is the store of the right type?)")
                },
            ),
        }
    }
}

/// Implement [`CallableParam`] for [`Store<T>`] to allow it to be used as a parameter in **async
/// AND sync** callables (e.g. [`crate::rpc`] methods, [`crate::AppBuilder::on_connect`] handlers,
/// etc).
impl<T: StoreData, Ctx: CallableFetch<App> + Send + Sync, Init: Send + Sync>
    CallableParam<Ctx, Init> for Store<T>
{
    type Error = std::convert::Infallible;

    async fn extract(ctx: &mut Ctx, _init: &Init) -> Result<Self, Self::Error> {
        let app = ctx.fetch();

        let id = StoreId::of::<T>();
        let existing_store = app.inner.state.stores.read().await.get(&id).cloned();

        let store = match existing_store {
            Some(store) => store,
            None => panic!("store not found when trying to extract parameter: {:?}", id),
        };

        Ok(Store {
            app: app.clone(),
            inner: store,
            _phantom: std::marker::PhantomData,
        })
    }
}

impl<T: StoreData> SupportsAsync for Store<T> {}

impl<T: StoreData> Clone for Store<T> {
    fn clone(&self) -> Self {
        Self {
            app: self.app.clone(),
            inner: self.inner.clone(),
            _phantom: std::marker::PhantomData,
        }
    }
}