Class: HTTP_Spew::InputSpray

Inherits:
Object
  • Object
show all
Defined in:
lib/http_spew/input_spray.rb

Overview

Use this to wrap and replace your input object for spraying to multiple servers.

Instance Method Summary collapse

Constructor Details

#initialize(env, nr, input = env["rack.input"]) ⇒ InputSpray



6
7
8
9
10
11
12
13
14
# File 'lib/http_spew/input_spray.rb', line 6

def initialize(env, nr, input = env["rack.input"])
  @input = input
  @pipes = {}.compare_by_identity
  nr.times do
    r, w = HTTP_Spew::ChunkyPipe.new
    @pipes[r] = w
  end
  start_write_driver
end

Instance Method Details

#readersObject



16
17
18
# File 'lib/http_spew/input_spray.rb', line 16

def readers
  @pipes.keys
end

#start_write_driverObject

TODO: splice(2) if @input is an IO



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/http_spew/input_spray.rb', line 30

def start_write_driver
  Thread.new do
    begin
      buf = ""
      while @input.read(0x4000, buf)
        @pipes.delete_if { |rd, wr| write_fail?(rd, wr, buf) }.empty? and
          raise HTTP_Spew::NoWritersError, "all writers have died", []
      end
      buf.clear
    rescue => e
      @pipes.each { |rd, _| rd.error = e }
    ensure
      @pipes.each { |_, wr| wr.close unless wr.closed? }
    end
  end
end

#write_fail?(rd, wr, buf) ⇒ Boolean



20
21
22
23
24
25
26
27
# File 'lib/http_spew/input_spray.rb', line 20

def write_fail?(rd, wr, buf)
  wr.write(buf)
  false
  rescue => e
    rd.error = e
    wr.close
    true
end