Class: HTTPX::Plugins::StreamBidi::BidiBuffer
- Defined in:
- lib/httpx/plugins/stream_bidi.rb
Overview
BidiBuffer is a thread-safe Buffer which can receive data from any thread.
It uses a dual-buffer strategy with mutex protection:
-
@buffer is the main buffer, protected by @buffer_mutex
-
@oob_buffer receives data when @buffer_mutex is contended
This allows non-blocking writes from any thread while maintaining thread safety.
Instance Attribute Summary
Attributes inherited from Buffer
Instance Method Summary collapse
-
#<<(chunk) ⇒ Object
buffers the
chunkto be sent (thread-safe, non-blocking). -
#initialize ⇒ BidiBuffer
constructor
A new instance of BidiBuffer.
-
#rebuffer ⇒ Object
reconciles the main and secondary buffer (thread-safe, callable from any thread).
Methods inherited from Buffer
Constructor Details
#initialize ⇒ BidiBuffer
Returns a new instance of BidiBuffer.
102 103 104 105 106 107 |
# File 'lib/httpx/plugins/stream_bidi.rb', line 102 def initialize(*) super @buffer_mutex = Thread::Mutex.new @oob_mutex = Thread::Mutex.new @oob_buffer = "".b end |
Instance Method Details
#<<(chunk) ⇒ Object
buffers the chunk to be sent (thread-safe, non-blocking)
110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/httpx/plugins/stream_bidi.rb', line 110 def <<(chunk) if @buffer_mutex.try_lock begin super ensure @buffer_mutex.unlock end else # another thread holds the lock, use OOB buffer to avoid blocking @oob_mutex.synchronize { @oob_buffer << chunk } end end |
#rebuffer ⇒ Object
reconciles the main and secondary buffer (thread-safe, callable from any thread).
124 125 126 127 128 129 130 131 132 133 |
# File 'lib/httpx/plugins/stream_bidi.rb', line 124 def rebuffer @buffer_mutex.synchronize do @oob_mutex.synchronize do return if @oob_buffer.empty? @buffer << @oob_buffer @oob_buffer.clear end end end |