Class: LcpRuby::Metrics::RateLimiter
- Inherits:
-
Object
- Object
- LcpRuby::Metrics::RateLimiter
- Defined in:
- lib/lcp_ruby/metrics/rate_limiter.rb
Constant Summary collapse
- MAX_ENTRIES =
10_000
Class Method Summary collapse
- .clear! ⇒ Object
-
.drain_count(fingerprint) ⇒ Object
Returns and resets the pending count for a fingerprint.
-
.flush_all ⇒ Object
Flushes all pending counts — called at_exit.
-
.increment(fingerprint) ⇒ Object
Increments the pending count for a throttled fingerprint.
-
.mark_written(fingerprint) ⇒ Object
Records that a write happened for this fingerprint.
-
.should_write?(fingerprint) ⇒ Boolean
Checks if this fingerprint should be written to the DB now.
Class Method Details
.clear! ⇒ Object
73 74 75 76 77 |
# File 'lib/lcp_ruby/metrics/rate_limiter.rb', line 73 def clear! monitor.synchronize do @entries = {} end end |
.drain_count(fingerprint) ⇒ Object
Returns and resets the pending count for a fingerprint.
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/lcp_ruby/metrics/rate_limiter.rb', line 49 def drain_count(fingerprint) monitor.synchronize do entry = entries[fingerprint] return 0 unless entry count = entry[:pending_count] entry[:pending_count] = 0 count end end |
.flush_all ⇒ Object
Flushes all pending counts — called at_exit. Returns a hash of { fingerprint => pending_count } for non-zero entries.
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/lcp_ruby/metrics/rate_limiter.rb', line 62 def flush_all monitor.synchronize do result = {} entries.each do |fp, entry| result[fp] = entry[:pending_count] if entry[:pending_count] > 0 end @entries = {} result end end |
.increment(fingerprint) ⇒ Object
Increments the pending count for a throttled fingerprint.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/lcp_ruby/metrics/rate_limiter.rb', line 33 def increment(fingerprint) monitor.synchronize do entry = entries[fingerprint] if entry entry[:pending_count] += 1 else evict_if_needed entries[fingerprint] = { last_written_at: Time.at(0), pending_count: 1 } end end end |
.mark_written(fingerprint) ⇒ Object
Records that a write happened for this fingerprint.
22 23 24 25 26 27 28 29 30 |
# File 'lib/lcp_ruby/metrics/rate_limiter.rb', line 22 def mark_written(fingerprint) monitor.synchronize do evict_if_needed entries[fingerprint] = { last_written_at: Time.now, pending_count: 0 } end end |
.should_write?(fingerprint) ⇒ Boolean
Checks if this fingerprint should be written to the DB now. Returns true on first occurrence, then throttles based on rate_limit config.
11 12 13 14 15 16 17 18 19 |
# File 'lib/lcp_ruby/metrics/rate_limiter.rb', line 11 def should_write?(fingerprint) monitor.synchronize do entry = entries[fingerprint] return true unless entry rate_limit = LcpRuby.configuration.error_log_rate_limit (Time.now - entry[:last_written_at]) >= rate_limit end end |