Class: CpuInspectCore::Store

Inherits:
Object
  • Object
show all
Includes:
MonitorMixin
Defined in:
lib/cpu_inspect_core/store.rb

Overview

Thread-safe ring buffer holding the most recent CPU samples. Uses MonitorMixin (reentrant) rather than Mutex so that calling CpuInspectCore.status from within a synchronized block doesn’t deadlock.

Instance Method Summary collapse

Constructor Details

#initialize(capacity: 1) ⇒ Store

Returns a new instance of Store.



12
13
14
15
16
# File 'lib/cpu_inspect_core/store.rb', line 12

def initialize(capacity: 1)
  super()
  @capacity = capacity
  @samples  = []
end

Instance Method Details

#latestObject



25
26
27
# File 'lib/cpu_inspect_core/store.rb', line 25

def latest
  synchronize { @samples.last }
end

#push(data) ⇒ Object



18
19
20
21
22
23
# File 'lib/cpu_inspect_core/store.rb', line 18

def push(data)
  synchronize do
    @samples << data
    @samples.shift if @samples.size > @capacity
  end
end