Module resource_manager

Module resource_manager 

Source
Expand description

Resource management for plugins.

This module provides centralized management of shared resources (primarily ML models) that can be expensive to load and should be shared across multiple node instances.

§Key Features

  • Automatic deduplication: Resources are content-addressed by (plugin kind, params hash)
  • Reference counting: Resources are kept alive while any node uses them
  • Configurable lifecycle: Keep loaded until explicit unload, or use LRU eviction
  • Thread-safe: Safe to use from multiple pipelines concurrently
  • Async initialization: Resources can perform async I/O or blocking operations

§Example

use streamkit_core::resource_manager::{
    ResourceManager, Resource, ResourcePolicy, ResourceKey
};
use std::sync::Arc;

// Define a custom resource type
struct MyModel {
    data: Vec<f32>,
}

impl Resource for MyModel {
    fn size_bytes(&self) -> usize {
        self.data.len() * std::mem::size_of::<f32>()
    }

    fn resource_type(&self) -> &str {
        "ml_model"
    }
}

#[tokio::main]
async fn main() {
    // Create resource manager
    let policy = ResourcePolicy {
        keep_loaded: true,
        max_memory_mb: None,
    };
    let manager = ResourceManager::new(policy);

    // Get or create a resource using a ResourceKey
    let key = ResourceKey::new("my_plugin", "param_hash");
    let resource = manager.get_or_create(
        key,
        || async {
            // Load model (runs only once per unique key)
            Ok(Arc::new(MyModel { data: vec![0.0; 1000] }) as Arc<dyn Resource>)
        }
    ).await.unwrap();
}

Structs§

ResourceKey
A unique key identifying a resource based on plugin kind and parameters.
ResourceManager
Centralized manager for shared plugin resources.
ResourcePolicy
Configuration policy for resource lifecycle management.
ResourceStats
Statistics about currently loaded resources.

Enums§

ResourceError
Errors that can occur during resource management.

Traits§

Resource
A resource that can be shared across multiple node instances.

Type Aliases§

ResourceFactory
Type alias for boxed async resource factories.