Class: Async::Utilization::Metric
- Inherits:
-
Object
- Object
- Async::Utilization::Metric
- Defined in:
- lib/async/utilization/metric.rb
Overview
A cached metric reference that avoids hash lookups on the fast path.
This class caches all the details needed to write directly to shared memory, including the buffer, offset, and type. When the observer changes, the cache is invalidated and rebuilt on the next access.
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
-
.for(field, observer) ⇒ Object
Create a new metric for the given field and observer.
Instance Method Summary collapse
-
#decrement ⇒ Object
Decrement the metric value.
-
#increment ⇒ Object
Increment the metric value.
-
#initialize(name) ⇒ Metric
constructor
Initialize a new metric.
-
#observer=(observer) ⇒ Object
Set the observer and rebuild cache.
-
#set(value) ⇒ Object
Set the metric value.
- #The field name for this metric.=(fieldname) ⇒ Object
-
#track { ... } ⇒ Object
Track an operation: increment before the block, decrement after it completes.
-
#value ⇒ Object
Get the current in-memory metric value.
Constructor Details
#initialize(name) ⇒ Metric
Initialize a new metric.
28 29 30 31 32 33 34 35 36 |
# File 'lib/async/utilization/metric.rb', line 28 def initialize(name) @name = name.to_sym @value = 0 @observer = nil @cached_field = nil @cached_buffer = nil @guard = Mutex.new end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
39 40 41 |
# File 'lib/async/utilization/metric.rb', line 39 def name @name end |
Class Method Details
.for(field, observer) ⇒ Object
Create a new metric for the given field and observer.
19 20 21 22 23 |
# File 'lib/async/utilization/metric.rb', line 19 def self.for(field, observer) self.new(field).tap do |metric| metric.observer = observer end end |
Instance Method Details
#decrement ⇒ Object
Decrement the metric value.
Uses the fast path (direct buffer write) when cache is valid and observer is available.
112 113 114 115 116 117 118 119 |
# File 'lib/async/utilization/metric.rb', line 112 def decrement @guard.synchronize do @value -= 1 write_direct(@value) end @value end |
#increment ⇒ Object
Increment the metric value.
80 81 82 83 84 85 86 87 |
# File 'lib/async/utilization/metric.rb', line 80 def increment @guard.synchronize do @value += 1 write_direct(@value) end @value end |
#observer=(observer) ⇒ Object
Set the observer and rebuild cache.
This is called when the registry assigns a new observer (or removes it). The cache is invalidated and then immediately recomputed so that the fast write path doesn’t need to re-check the observer on the first write.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/async/utilization/metric.rb', line 57 def observer=(observer) @guard.synchronize do @observer = observer if @observer if field = @observer.schema[@name] if buffer = @observer.buffer @cached_field = field @cached_buffer = buffer return write_direct(@value) end end end @cached_field = nil @cached_buffer = nil end end |
#set(value) ⇒ Object
Set the metric value.
Uses the fast path (direct buffer write) when cache is valid and observer is available.
126 127 128 129 130 131 |
# File 'lib/async/utilization/metric.rb', line 126 def set(value) @guard.synchronize do @value = value write_direct(@value) end end |
#The field name for this metric.=(fieldname) ⇒ Object
39 |
# File 'lib/async/utilization/metric.rb', line 39 attr :name |
#track { ... } ⇒ Object
Track an operation: increment before the block, decrement after it completes.
Returns the block’s return value. Use for active/count metrics that should reflect the number of operations currently in progress.
96 97 98 99 100 101 102 103 104 105 |
# File 'lib/async/utilization/metric.rb', line 96 def track(&block) raise ArgumentError, "block required" unless block_given? increment begin yield ensure decrement end end |
#value ⇒ Object
Get the current in-memory metric value.
44 45 46 47 48 |
# File 'lib/async/utilization/metric.rb', line 44 def value @guard.synchronize do @value end end |