Class: BreakerMachines::Storage::Native

Inherits:
Base
  • Object
show all
Defined in:
lib/breaker_machines/storage/native.rb

Overview

Native extension storage backend with graceful fallback to pure Ruby

This backend provides identical functionality to Memory storage but with significantly better performance for sliding window calculations when the native extension is available. If the native extension isn’t available (e.g., on JRuby or if Rust wasn’t installed), it automatically falls back to the pure Ruby Memory storage backend.

Performance: ~63x faster than Memory storage when native extension is available

Usage:

BreakerMachines.configure do |config|
  config.default_storage = :native
end

FFI Hybrid Pattern: This class is only loaded when native extension is available Fallback happens at load time (native_speedup.rb), not at runtime

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ Native

Returns a new instance of Native.



23
24
25
26
27
# File 'lib/breaker_machines/storage/native.rb', line 23

def initialize(**options)
  super
  # Native extension is guaranteed to be available when this class is loaded
  @backend = BreakerMachinesNative::Storage.new
end

Instance Method Details

#clear(circuit_name) ⇒ Object



62
63
64
# File 'lib/breaker_machines/storage/native.rb', line 62

def clear(circuit_name)
  @backend.clear(circuit_name.to_s)
end

#clear_allObject



66
67
68
# File 'lib/breaker_machines/storage/native.rb', line 66

def clear_all
  @backend.clear_all
end

#event_log(circuit_name, limit) ⇒ Object



83
84
85
# File 'lib/breaker_machines/storage/native.rb', line 83

def event_log(circuit_name, limit)
  @backend.event_log(circuit_name.to_s, limit)
end

#failure_count(circuit_name, window_seconds) ⇒ Object



58
59
60
# File 'lib/breaker_machines/storage/native.rb', line 58

def failure_count(circuit_name, window_seconds)
  @backend.failure_count(circuit_name.to_s, window_seconds)
end

#get_status(_circuit_name) ⇒ Object



35
36
37
38
39
# File 'lib/breaker_machines/storage/native.rb', line 35

def get_status(_circuit_name)
  # Status is still managed by Ruby layer
  # This storage backend only handles event tracking
  nil
end

#native?Boolean

Check if using native backend

Returns:

  • (Boolean)

    always true - this class only exists when native is available



31
32
33
# File 'lib/breaker_machines/storage/native.rb', line 31

def native?
  true
end

#record_event_with_details(circuit_name, type, duration, error: nil, new_state: nil) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/breaker_machines/storage/native.rb', line 70

def record_event_with_details(circuit_name, type, duration, error: nil, new_state: nil)
  # Basic event recording (native extension handles type and duration)
  case type
  when :success
    record_success(circuit_name, duration)
  when :failure
    record_failure(circuit_name, duration)
  end

  # NOTE: Error and state details not tracked in native backend
  # This is intentional for performance - use Memory backend if you need full event details
end

#record_failure(circuit_name, duration) ⇒ Object



50
51
52
# File 'lib/breaker_machines/storage/native.rb', line 50

def record_failure(circuit_name, duration)
  @backend.record_failure(circuit_name.to_s, duration)
end

#record_success(circuit_name, duration) ⇒ Object



46
47
48
# File 'lib/breaker_machines/storage/native.rb', line 46

def record_success(circuit_name, duration)
  @backend.record_success(circuit_name.to_s, duration)
end

#set_status(circuit_name, status, opened_at = nil) ⇒ Object



41
42
43
44
# File 'lib/breaker_machines/storage/native.rb', line 41

def set_status(circuit_name, status, opened_at = nil)
  # Status management delegated to Ruby layer
  # This backend focuses on high-performance event counting
end

#success_count(circuit_name, window_seconds) ⇒ Object



54
55
56
# File 'lib/breaker_machines/storage/native.rb', line 54

def success_count(circuit_name, window_seconds)
  @backend.success_count(circuit_name.to_s, window_seconds)
end

#with_timeout(_timeout_ms) ⇒ Object



87
88
89
90
# File 'lib/breaker_machines/storage/native.rb', line 87

def with_timeout(_timeout_ms)
  # Native operations should be instant
  yield
end