Class: HTTPX::Plugins::StreamBidi::BidiBuffer

Inherits:
Buffer
  • Object
show all
Defined in:
lib/httpx/plugins/stream_bidi.rb

Overview

BidiBuffer is a Buffer which can be receive data from threads othr than the thread of the corresponding Connection/Session.

It synchronizes access to a secondary internal @oob_buffer, which periodically is reconciled to the main internal @buffer.

Instance Attribute Summary

Attributes inherited from Buffer

#limit

Instance Method Summary collapse

Methods inherited from Buffer

#capacity, #full?, #shift!

Constructor Details

#initializeBidiBuffer

Returns a new instance of BidiBuffer.



90
91
92
93
94
95
# File 'lib/httpx/plugins/stream_bidi.rb', line 90

def initialize(*)
  super
  @parent_thread = Thread.current
  @oob_mutex = Thread::Mutex.new
  @oob_buffer = "".b
end

Instance Method Details

#<<(chunk) ⇒ Object

buffers the chunk to be sent



98
99
100
101
102
# File 'lib/httpx/plugins/stream_bidi.rb', line 98

def <<(chunk)
  return super if Thread.current == @parent_thread

  @oob_mutex.synchronize { @oob_buffer << chunk }
end

#rebufferObject

reconciles the main and secondary buffer (which receives data from other threads).

Raises:



105
106
107
108
109
110
111
112
# File 'lib/httpx/plugins/stream_bidi.rb', line 105

def rebuffer
  raise Error, "can only rebuffer while waiting on a response" unless Thread.current == @parent_thread

  @oob_mutex.synchronize do
    @buffer << @oob_buffer
    @oob_buffer.clear
  end
end