Class: PromptWarden::Buffer
- Inherits:
-
Object
- Object
- PromptWarden::Buffer
- Defined in:
- lib/prompt_warden/buffer.rb
Instance Method Summary collapse
-
#flush! ⇒ Object
Manual flush.
-
#initialize(cfg = PromptWarden.configuration) ⇒ Buffer
constructor
A new instance of Buffer.
-
#push(event) ⇒ Object
Enqueue an Event (or Hash).
Constructor Details
#initialize(cfg = PromptWarden.configuration) ⇒ Buffer
Returns a new instance of Buffer.
8 9 10 11 12 13 14 |
# File 'lib/prompt_warden/buffer.rb', line 8 def initialize(cfg = PromptWarden.configuration) @cfg = cfg @events = [] @bytes = 0 @mutex = Mutex.new start_timer end |
Instance Method Details
#flush! ⇒ Object
Manual flush
33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/prompt_warden/buffer.rb', line 33 def flush! batch = nil @mutex.synchronize do return if @events.empty? batch = @events.join("\n") @events.clear @bytes = 0 end compressed = Zlib.gzip(batch) Uploader.instance.enqueue(compressed) end |
#push(event) ⇒ Object
Enqueue an Event (or Hash). Flushes automatically when batch_bytes hit.
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/prompt_warden/buffer.rb', line 19 def push(event) json = JSON.generate(event.to_h) should_flush = false @mutex.synchronize do @events << json @bytes += json.bytesize should_flush = @bytes >= @cfg.batch_bytes end flush! if should_flush end |