Class: SemanticLogger::QueueProcessor::ThrottledCounter
- Inherits:
-
Object
- Object
- SemanticLogger::QueueProcessor::ThrottledCounter
- Defined in:
- lib/semantic_logger/queue_processor/throttled_counter.rb
Overview
Internal: counts occurrences of a problem, such as a dropped log message, and has it reported at most once per interval instead of once per occurrence.
It knows the total number of occurrences, how many there have been since the previous report, and when that report was made. What to report, and where, is up to the caller.
Thread-safe: occurrences may be recorded from any thread, and from a signal trap, such as one that logs while the queue is full. Ruby does not allow a Mutex to be locked in a trap, so the occurrences recorded there wait on a Queue, which it does allow, until a thread started for them, or the next read or record where the Mutex can be locked, adds them in.
Class Method Summary collapse
-
.now ⇒ Object
Returns [Float] the seconds on the monotonic clock, which the intervals between reports are measured with, so that a change of the system clock, such as NTP stepping it back, neither holds back a report nor makes one early.
Instance Method Summary collapse
-
#initialize(reported_at: ThrottledCounter.now) ⇒ ThrottledCounter
constructor
Parameters: reported_at: [Float|nil] When counting starts, on the monotonic clock (see .now), so that the first report is due one interval later.
-
#record(count = 1, interval:) {|report| ... } ⇒ Object
Record one or more occurrences.
-
#total ⇒ Object
Returns [Integer] the total number of occurrences recorded.
Constructor Details
#initialize(reported_at: ThrottledCounter.now) ⇒ ThrottledCounter
Parameters:
reported_at: [Float|nil]
When counting starts, on the monotonic clock (see .now), so that the first report is due
one interval later.
nil: Report the first occurrence straight away.
Default: ThrottledCounter.now
25 26 27 28 29 30 31 |
# File 'lib/semantic_logger/queue_processor/throttled_counter.rb', line 25 def initialize(reported_at: ThrottledCounter.now) @reported_at = reported_at @total = 0 @unreported = 0 @mutex = Mutex.new @waiting = Queue.new end |
Class Method Details
.now ⇒ Object
Returns [Float] the seconds on the monotonic clock, which the intervals between reports are measured with, so that a change of the system clock, such as NTP stepping it back, neither holds back a report nor makes one early.
17 |
# File 'lib/semantic_logger/queue_processor/throttled_counter.rb', line 17 def self.now = Process.clock_gettime(Process::CLOCK_MONOTONIC) |
Instance Method Details
#record(count = 1, interval:) {|report| ... } ⇒ Object
Record one or more occurrences.
When the previous report was at least interval seconds ago, yields the number of
occurrences since then (including these) and the seconds elapsed since that report (nil
when there has been no report yet), then starts counting afresh for the next report.
In a signal trap the occurrences are only counted, and are added in and reported, when a report is due, by a thread started for them, just after the trap (see #wait).
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/semantic_logger/queue_processor/throttled_counter.rb', line 48 def record(count = 1, interval:, &report_block) report = nil counted = TrapSafe.try_synchronize(@mutex) do add(count) now = ThrottledCounter.now elapsed = @reported_at && (now - @reported_at) unless elapsed && elapsed < interval report = [@unreported, elapsed] @unreported = 0 @reported_at = now end end return wait(count, interval, &report_block) unless counted # Reported without holding the lock, so that the report can record occurrences too. yield(*report) if report end |
#total ⇒ Object
Returns [Integer] the total number of occurrences recorded. Read in a signal trap, it leaves out those recorded in a trap that have not been added in yet.
35 36 37 38 |
# File 'lib/semantic_logger/queue_processor/throttled_counter.rb', line 35 def total TrapSafe.try_synchronize(@mutex) { add(0) } @total end |