Class: KubeMQ::Transport::MessageBuffer Private
- Inherits:
-
Object
- Object
- KubeMQ::Transport::MessageBuffer
- Defined in:
- lib/kubemq/transport/message_buffer.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.
This class is thread-safe. All operations are mutex-protected.
Thread-safe bounded buffer for queuing messages during reconnection.
When the transport is disconnected, outbound messages are buffered here. If the buffer reaches capacity, the oldest message is dropped to make room -- consistent with other KubeMQ SDKs.
Constant Summary collapse
- DEFAULT_CAPACITY =
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.
Default buffer capacity.
1000
Instance Attribute Summary collapse
-
#capacity ⇒ Integer
readonly
private
Maximum number of messages this buffer can hold.
Instance Method Summary collapse
-
#clear ⇒ nil
private
Removes all messages from the buffer.
-
#drain {|message| ... } ⇒ Array?
private
Drains all buffered messages.
-
#empty? ⇒ Boolean
private
Returns whether the buffer is empty.
-
#full? ⇒ Boolean
private
Returns whether the buffer has reached capacity.
-
#initialize(capacity: DEFAULT_CAPACITY) ⇒ MessageBuffer
constructor
private
A new instance of MessageBuffer.
-
#push(message) ⇒ void
private
Adds a message to the buffer.
-
#size ⇒ Integer
private
Returns the current number of buffered messages.
-
#to_a ⇒ Array<Object>
private
Drains all messages and returns them as an array.
Constructor Details
#initialize(capacity: DEFAULT_CAPACITY) ⇒ MessageBuffer
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 MessageBuffer.
24 25 26 27 28 |
# File 'lib/kubemq/transport/message_buffer.rb', line 24 def initialize(capacity: DEFAULT_CAPACITY) @capacity = capacity @queue = Thread::SizedQueue.new(capacity) @mutex = Mutex.new end |
Instance Attribute Details
#capacity ⇒ 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 number of messages this buffer can hold.
21 22 23 |
# File 'lib/kubemq/transport/message_buffer.rb', line 21 def capacity @capacity end |
Instance Method Details
#clear ⇒ nil
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.
Removes all messages from the buffer.
99 100 101 102 |
# File 'lib/kubemq/transport/message_buffer.rb', line 99 def clear drain nil end |
#drain {|message| ... } ⇒ Array?
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.
Drains all buffered messages. If a block is given, yields each message; otherwise returns them as an array via #to_a.
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/kubemq/transport/message_buffer.rb', line 55 def drain(&block) return to_a unless block loop do msg = @queue.pop(true) block.call(msg) rescue ThreadError break end end |
#empty? ⇒ Boolean
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 whether the buffer is empty.
85 86 87 |
# File 'lib/kubemq/transport/message_buffer.rb', line 85 def empty? @queue.empty? end |
#full? ⇒ Boolean
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 whether the buffer has reached capacity.
92 93 94 |
# File 'lib/kubemq/transport/message_buffer.rb', line 92 def full? @queue.size >= @capacity end |
#push(message) ⇒ void
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.
This method returns an undefined value.
Adds a message to the buffer. If full, the oldest message is dropped.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/kubemq/transport/message_buffer.rb', line 34 def push() @mutex.synchronize do begin @queue.pop(true) if @queue.size >= @capacity rescue ThreadError # queue was emptied concurrently end begin @queue.push(, true) rescue ThreadError # queue full; discard end end end |
#size ⇒ Integer
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 the current number of buffered messages.
78 79 80 |
# File 'lib/kubemq/transport/message_buffer.rb', line 78 def size @queue.size end |
#to_a ⇒ Array<Object>
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.
Drains all messages and returns them as an array.
69 70 71 72 73 |
# File 'lib/kubemq/transport/message_buffer.rb', line 69 def to_a = [] drain { |m| << m } end |