pub struct ResourceManager { /* private fields */ }Expand description
Centralized manager for shared plugin resources.
The ResourceManager maintains a cache of resources that can be shared across multiple node instances. It handles lifecycle management, deduplication, and optional memory-based eviction.
Implementations§
Source§impl ResourceManager
impl ResourceManager
Sourcepub fn new(policy: ResourcePolicy) -> Self
pub fn new(policy: ResourcePolicy) -> Self
Create a new ResourceManager with the specified policy.
Sourcepub async fn get_or_create<F, Fut>(
&self,
key: ResourceKey,
factory: F,
) -> Result<Arc<dyn Resource>, ResourceError>
pub async fn get_or_create<F, Fut>( &self, key: ResourceKey, factory: F, ) -> Result<Arc<dyn Resource>, ResourceError>
Get an existing resource or create it using the provided factory.
If a resource with the given key already exists, it is returned immediately. Otherwise, the factory is called to create a new resource, which is then cached for future use.
§Arguments
key- Unique identifier for the resourcefactory- Async function that creates the resource if needed
§Errors
Returns an error if the factory function fails to create the resource.
§Example
use streamkit_core::resource_manager::{
ResourceManager, Resource, ResourcePolicy, ResourceKey, ResourceError
};
use std::sync::Arc;
struct MyModel { data: Vec<f32> }
impl Resource for MyModel {
fn size_bytes(&self) -> usize { self.data.len() * 4 }
fn resource_type(&self) -> &str { "ml_model" }
}
async fn example() -> Result<(), ResourceError> {
let manager = ResourceManager::new(ResourcePolicy::default());
let resource = manager.get_or_create(
ResourceKey::new("my_plugin", "model_v1"),
|| async {
Ok(Arc::new(MyModel { data: vec![0.0; 1000] }) as Arc<dyn Resource>)
}
).await?;
Ok(())
}Sourcepub async fn unload(&self, key: &ResourceKey) -> Result<(), ResourceError>
pub async fn unload(&self, key: &ResourceKey) -> Result<(), ResourceError>
Explicitly unload a resource by key.
This removes the resource from the cache. If other node instances still hold references to the resource, it will remain in memory until they drop their references.
§Errors
Returns an error if the resource key is not found in the cache.
Sourcepub async fn stats(&self) -> ResourceStats
pub async fn stats(&self) -> ResourceStats
Get statistics about currently loaded resources.