Class: UserPattern::Buffer
- Inherits:
-
Object
- Object
- UserPattern::Buffer
- Defined in:
- lib/userpattern/buffer.rb
Overview
Thread-safe in-memory buffer that batches request events before flushing to the database. Minimizes per-request overhead to a single array push.
Constant Summary collapse
- MAX_DRAIN =
1_000
Instance Method Summary collapse
- #flush ⇒ Object
-
#initialize ⇒ Buffer
constructor
A new instance of Buffer.
- #push(event) ⇒ Object
- #shutdown ⇒ Object
- #size ⇒ Object
Constructor Details
#initialize ⇒ Buffer
Returns a new instance of Buffer.
11 12 13 14 15 |
# File 'lib/userpattern/buffer.rb', line 11 def initialize @queue = Concurrent::Array.new @flushing = Concurrent::AtomicBoolean.new(false) start_timer end |
Instance Method Details
#flush ⇒ Object
22 23 24 25 26 27 28 29 |
# File 'lib/userpattern/buffer.rb', line 22 def flush return if @queue.empty? return unless @flushing.make_true persist_events ensure @flushing.make_false end |
#push(event) ⇒ Object
17 18 19 20 |
# File 'lib/userpattern/buffer.rb', line 17 def push(event) @queue << event flush_async if @queue.size >= UserPattern.configuration.buffer_size end |
#shutdown ⇒ Object
31 32 33 34 |
# File 'lib/userpattern/buffer.rb', line 31 def shutdown @timer&.shutdown flush end |
#size ⇒ Object
36 37 38 |
# File 'lib/userpattern/buffer.rb', line 36 def size @queue.size end |