apollo_router/context/extensions/sync.rs
1use std::sync::Arc;
2
3/// You can use `Extensions` to pass data between plugins that is not serializable. Such data is not accessible from Rhai or co-processoers.
4///
5/// This can be accessed at any point in the request lifecycle and is useful for passing data between services.
6/// Extensions are thread safe, and must be locked for mutation.
7///
8/// For example:
9/// `context.extensions().lock().insert::<MyData>(data);`
10#[derive(Default, Clone, Debug)]
11pub struct ExtensionsMutex {
12 extensions: Arc<parking_lot::Mutex<super::Extensions>>,
13}
14
15impl ExtensionsMutex {
16 /// Locks the extensions for interaction.
17 ///
18 /// The lock will be dropped once the closure completes.
19 pub fn with_lock<T, F: FnOnce(&mut super::Extensions) -> T>(&self, func: F) -> T {
20 let mut locked = self.extensions.lock();
21 func(&mut locked)
22 }
23}