module HTTPX::Plugins::StreamBidi::RequestMethods

  1. lib/httpx/plugins/stream_bidi.rb

Adds synchronization to request operations which may buffer payloads from different threads.

Attributes

Public Class methods

new(*)
[show source]
    # File lib/httpx/plugins/stream_bidi.rb
265 def initialize(*)
266   super
267   @headers_sent = false
268   @closed = false
269   @flush_buffer_on_body_cb = nil
270   @mutex = Thread::Mutex.new
271 end

Public Instance methods

<<(chunk)
[show source]
    # File lib/httpx/plugins/stream_bidi.rb
323 def <<(chunk)
324   @mutex.synchronize do
325     if @drainer
326       @body.clear if @body.respond_to?(:clear)
327       @drainer = nil
328     end
329     @body << chunk
330 
331     transition(:body)
332   end
333 end
can_buffer?()
[show source]
    # File lib/httpx/plugins/stream_bidi.rb
283 def can_buffer?
284   return super unless @options.stream
285 
286   super && @state != :waiting_for_chunk
287 end
close()
[show source]
    # File lib/httpx/plugins/stream_bidi.rb
335 def close
336   return super unless @options.stream
337 
338   @mutex.synchronize do
339     return if @closed
340 
341     @closed = true
342   end
343 
344   # last chunk to send which ends the stream
345   self << ""
346 end
closed?()
[show source]
    # File lib/httpx/plugins/stream_bidi.rb
277 def closed?
278   return super unless @options.stream
279 
280   @closed
281 end
flush_buffer_on_body(&cb)
[show source]
    # File lib/httpx/plugins/stream_bidi.rb
273 def flush_buffer_on_body(&cb)
274   @flush_buffer_on_body_cb = on(:body, &cb)
275 end
transition(nextstate)

overrides state management transitions to introduce an intermediate :waiting_for_chunk state, which the request transitions to once payload is buffered.

[show source]
    # File lib/httpx/plugins/stream_bidi.rb
292 def transition(nextstate)
293   return super unless @options.stream
294 
295   headers_sent = @headers_sent
296 
297   case nextstate
298   when :idle
299     headers_sent = false
300 
301     if @flush_buffer_on_body_cb
302       callbacks(:body).delete(@flush_buffer_on_body_cb)
303       @flush_buffer_on_body_cb = nil
304     end
305   when :waiting_for_chunk
306     return unless @state == :body
307   when :body
308     case @state
309     when :headers
310       headers_sent = true
311     when :waiting_for_chunk
312       # HACK: to allow super to pass through
313       @state = :headers
314     end
315   end
316 
317   super.tap do
318     # delay setting this up until after the first transition to :body
319     @headers_sent = headers_sent
320   end
321 end