Class: Logtide::Transport::Buffer

Inherits:
Object
  • Object
show all
Defined in:
lib/logtide/transport/buffer.rb

Overview

A bounded FIFO buffer (spec 002 section 5). When full it rejects the new entry (drop-newest); the caller records the drop. Thread-safe.

Instance Method Summary collapse

Constructor Details

#initialize(max_size:) ⇒ Buffer

Returns a new instance of Buffer.



8
9
10
11
12
# File 'lib/logtide/transport/buffer.rb', line 8

def initialize(max_size:)
  @max_size = max_size
  @entries = []
  @mutex = Mutex.new
end

Instance Method Details

#drain(limit) ⇒ Object



24
25
26
# File 'lib/logtide/transport/buffer.rb', line 24

def drain(limit)
  @mutex.synchronize { @entries.shift(limit) }
end

#empty?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/logtide/transport/buffer.rb', line 32

def empty?
  @mutex.synchronize { @entries.empty? }
end

#push(entry) ⇒ Object

Returns false when the buffer is full and the entry was dropped.



15
16
17
18
19
20
21
22
# File 'lib/logtide/transport/buffer.rb', line 15

def push(entry)
  @mutex.synchronize do
    return false if @entries.size >= @max_size

    @entries << entry
    true
  end
end

#sizeObject



28
29
30
# File 'lib/logtide/transport/buffer.rb', line 28

def size
  @mutex.synchronize { @entries.size }
end