Class: OpenTrace::BufferPool
- Inherits:
-
Object
- Object
- OpenTrace::BufferPool
- Defined in:
- lib/opentrace/buffer_pool.rb
Instance Method Summary collapse
-
#checkin(buffer) ⇒ Object
Resets the buffer and returns it to the pool.
-
#checkout ⇒ Object
Returns a RequestBuffer ready for use.
-
#initialize(size: 32, max_buffer_bytes: 1_048_576, max_audit_events: 50) ⇒ BufferPool
constructor
A new instance of BufferPool.
-
#stats ⇒ Object
Returns a snapshot of pool statistics.
Constructor Details
#initialize(size: 32, max_buffer_bytes: 1_048_576, max_audit_events: 50) ⇒ BufferPool
Returns a new instance of BufferPool.
7 8 9 10 11 12 13 14 15 |
# File 'lib/opentrace/buffer_pool.rb', line 7 def initialize(size: 32, max_buffer_bytes: 1_048_576, max_audit_events: 50) @max_buffer_bytes = max_buffer_bytes @max_audit_events = max_audit_events @max_size = size @mutex = Mutex.new @pool = Array.new(size) { RequestBuffer.new(max_buffer_bytes: max_buffer_bytes, max_audit_events: max_audit_events) } @checked_out = 0 end |
Instance Method Details
#checkin(buffer) ⇒ Object
Resets the buffer and returns it to the pool. If the pool is already at max capacity, the buffer is discarded (GC will collect it).
37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/opentrace/buffer_pool.rb', line 37 def checkin(buffer) buffer.reset! @mutex.synchronize do @checked_out -= 1 @checked_out = 0 if @checked_out < 0 # safety clamp if @pool.size < @max_size @pool.push(buffer) end # else: discard — pool is full end end |
#checkout ⇒ Object
Returns a RequestBuffer ready for use. Pulls from pool if available, allocates a new one if pool is empty. Sets id and started_at.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/opentrace/buffer_pool.rb', line 19 def checkout buffer = @mutex.synchronize do buf = @pool.pop @checked_out += 1 buf end # Pool was empty — allocate fresh (outside the lock) buffer ||= RequestBuffer.new(max_buffer_bytes: @max_buffer_bytes, max_audit_events: @max_audit_events) buffer.id = ULID.generate buffer.started_at = Process.clock_gettime(Process::CLOCK_MONOTONIC) buffer end |
#stats ⇒ Object
Returns a snapshot of pool statistics.
52 53 54 55 56 57 58 59 60 |
# File 'lib/opentrace/buffer_pool.rb', line 52 def stats @mutex.synchronize do { pool_size: @max_size, available: @pool.size, checked_out: @checked_out } end end |