Class: PostHog::Rails::Logs::RateLimiter Private
- Inherits:
-
Object
- Object
- PostHog::Rails::Logs::RateLimiter
- Defined in:
- lib/posthog/rails/logs/rate_limiter.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Fixed-window rate limiter protecting the PostHog Logs ingestion quota from runaway log volume (PostHog Logs bills by data ingested).
Thread-safe: the counter is the one piece of shared mutable state in the logs pipeline, guarded by a mutex scoped to a counter bump.
Constant Summary collapse
- WINDOW_SECONDS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
60
Instance Attribute Summary collapse
-
#limit ⇒ Integer
readonly
private
Maximum records allowed per window.
Instance Method Summary collapse
-
#initialize(limit) ⇒ RateLimiter
constructor
private
A new instance of RateLimiter.
-
#record ⇒ Symbol
private
Records one attempt and returns the verdict.
Constructor Details
#initialize(limit) ⇒ RateLimiter
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of RateLimiter.
20 21 22 23 24 25 |
# File 'lib/posthog/rails/logs/rate_limiter.rb', line 20 def initialize(limit) @limit = limit @mutex = Mutex.new @window = nil @count = 0 end |
Instance Attribute Details
#limit ⇒ Integer (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns Maximum records allowed per window.
17 18 19 |
# File 'lib/posthog/rails/logs/rate_limiter.rb', line 17 def limit @limit end |
Instance Method Details
#record ⇒ Symbol
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Records one attempt and returns the verdict.
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/posthog/rails/logs/rate_limiter.rb', line 32 def record @mutex.synchronize do window = Process.clock_gettime(Process::CLOCK_MONOTONIC).to_i / WINDOW_SECONDS if window != @window @window = window @count = 0 end @count += 1 next :allow if @count <= @limit @count == @limit + 1 ? :reject_first : :reject end end |