Class: VectorMCP::Middleware::Manager
- Inherits:
-
Object
- Object
- VectorMCP::Middleware::Manager
- Defined in:
- lib/vector_mcp/middleware/manager.rb
Overview
Central manager for middleware hooks and execution Thread-safe registry and execution engine for all middleware
Instance Method Summary collapse
-
#clear! ⇒ Object
Clear all registered hooks (useful for testing).
-
#execute_hooks(hook_type, context) ⇒ VectorMCP::Middleware::Context
Execute all hooks for a specific hook type with timing.
-
#initialize ⇒ Manager
constructor
A new instance of Manager.
-
#register(middleware_class, hooks, priority: Hook::DEFAULT_PRIORITY, conditions: {}) ⇒ Object
Register a middleware for specific hook types.
-
#stats ⇒ Hash
Get statistics about registered middleware.
-
#unregister(middleware_class) ⇒ Object
Remove all hooks for a specific middleware class.
Constructor Details
#initialize ⇒ Manager
Returns a new instance of Manager.
10 11 12 13 |
# File 'lib/vector_mcp/middleware/manager.rb', line 10 def initialize @hooks = Concurrent::Map.new { |h, k| h[k] = Concurrent::Array.new } @logger = VectorMCP.logger_for("middleware.manager") end |
Instance Method Details
#clear! ⇒ Object
Clear all registered hooks (useful for testing)
92 93 94 95 |
# File 'lib/vector_mcp/middleware/manager.rb', line 92 def clear! @hooks.clear @logger.debug("Cleared all middleware hooks") end |
#execute_hooks(hook_type, context) ⇒ VectorMCP::Middleware::Context
Execute all hooks for a specific hook type with timing
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/vector_mcp/middleware/manager.rb', line 59 def execute_hooks(hook_type, context) hook_type_str = hook_type.to_s hooks = get_sorted_hooks(hook_type_str) return context if hooks.empty? execution_state = initialize_execution_state(hook_type_str, hooks, context) execute_hook_chain(hooks, context, execution_state) finalize_execution(context, execution_state) context end |
#register(middleware_class, hooks, priority: Hook::DEFAULT_PRIORITY, conditions: {}) ⇒ Object
Register a middleware for specific hook types
23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/vector_mcp/middleware/manager.rb', line 23 def register(middleware_class, hooks, priority: Hook::DEFAULT_PRIORITY, conditions: {}) Array(hooks).each do |hook_type| hook = Hook.new(middleware_class, hook_type, priority: priority, conditions: conditions) add_hook(hook) end @logger.debug("Registered middleware") do { middleware: middleware_class.name, hooks: Array(hooks), priority: priority } end end |
#stats ⇒ Hash
Get statistics about registered middleware
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/vector_mcp/middleware/manager.rb', line 74 def stats hook_counts = {} total_hooks = 0 @hooks.each do |hook_type, hook_array| count = hook_array.size hook_counts[hook_type] = count total_hooks += count end { total_hooks: total_hooks, hook_types: hook_counts.keys.sort, hooks_by_type: hook_counts } end |
#unregister(middleware_class) ⇒ Object
Remove all hooks for a specific middleware class
40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/vector_mcp/middleware/manager.rb', line 40 def unregister(middleware_class) removed_count = 0 @hooks.each_value do |hook_array| removed_count += hook_array.delete_if { |hook| hook.middleware_class == middleware_class }.size end @logger.debug("Unregistered middleware") do { middleware: middleware_class.name, hooks_removed: removed_count } end end |