Adds synchronization to request operations which may buffer payloads from different threads.
Attributes
headers_sent | [RW] |
Public Class methods
new(*)
[show source]
# File lib/httpx/plugins/stream_bidi.rb 187 def initialize(*) 188 super 189 @headers_sent = false 190 @closed = false 191 @mutex = Thread::Mutex.new 192 end
Public Instance methods
<<(chunk)
[show source]
# File lib/httpx/plugins/stream_bidi.rb 227 def <<(chunk) 228 @mutex.synchronize do 229 if @drainer 230 @body.clear if @body.respond_to?(:clear) 231 @drainer = nil 232 end 233 @body << chunk 234 235 transition(:body) 236 end 237 end
can_buffer?()
[show source]
# File lib/httpx/plugins/stream_bidi.rb 198 def can_buffer? 199 super && @state != :waiting_for_chunk 200 end
close()
[show source]
# File lib/httpx/plugins/stream_bidi.rb 239 def close 240 @mutex.synchronize do 241 return if @closed 242 243 @closed = true 244 end 245 246 # last chunk to send which ends the stream 247 self << "" 248 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 205 def transition(nextstate) 206 headers_sent = @headers_sent 207 208 case nextstate 209 when :waiting_for_chunk 210 return unless @state == :body 211 when :body 212 case @state 213 when :headers 214 headers_sent = true 215 when :waiting_for_chunk 216 # HACK: to allow super to pass through 217 @state = :headers 218 end 219 end 220 221 super.tap do 222 # delay setting this up until after the first transition to :body 223 @headers_sent = headers_sent 224 end 225 end