Module: HTTPX::Plugins::StreamBidi::RequestMethods

Defined in:
lib/httpx/plugins/stream_bidi.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#headers_sentObject

Returns the value of attribute headers_sent.



203
204
205
# File 'lib/httpx/plugins/stream_bidi.rb', line 203

def headers_sent
  @headers_sent
end

Instance Method Details

#<<(chunk) ⇒ Object



245
246
247
248
249
250
251
252
253
254
255
# File 'lib/httpx/plugins/stream_bidi.rb', line 245

def <<(chunk)
  @mutex.synchronize do
    if @drainer
      @body.clear if @body.respond_to?(:clear)
      @drainer = nil
    end
    @body << chunk

    transition(:body)
  end
end

#can_buffer?Boolean

Returns:

  • (Boolean)


216
217
218
# File 'lib/httpx/plugins/stream_bidi.rb', line 216

def can_buffer?
  super && @state != :waiting_for_chunk
end

#closeObject



257
258
259
260
261
262
263
264
265
266
# File 'lib/httpx/plugins/stream_bidi.rb', line 257

def close
  @mutex.synchronize do
    return if @closed

    @closed = true
  end

  # last chunk to send which ends the stream
  self << ""
end

#closed?Boolean

Returns:

  • (Boolean)


212
213
214
# File 'lib/httpx/plugins/stream_bidi.rb', line 212

def closed?
  @closed
end

#initializeObject



205
206
207
208
209
210
# File 'lib/httpx/plugins/stream_bidi.rb', line 205

def initialize(*)
  super
  @headers_sent = false
  @closed = false
  @mutex = Thread::Mutex.new
end

#transition(nextstate) ⇒ Object

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



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/httpx/plugins/stream_bidi.rb', line 223

def transition(nextstate)
  headers_sent = @headers_sent

  case nextstate
  when :waiting_for_chunk
    return unless @state == :body
  when :body
    case @state
    when :headers
      headers_sent = true
    when :waiting_for_chunk
      # HACK: to allow super to pass through
      @state = :headers
    end
  end

  super.tap do
    # delay setting this up until after the first transition to :body
    @headers_sent = headers_sent
  end
end