Sha256: bcc2521c6b9c8c26ed72dd1cd966f47de622417f6ca523a10f4f4c367ebdee56

Contents?: true

Size: 1.25 KB

Versions: 20

Compression:

Stored size: 1.25 KB

Contents

# -*- encoding: binary -*-
# :enddoc:
# TODO: handle large responses without having it all in memory
module Rainbows::ReverseProxy::EventMachine
  class Backend < EM::Connection
    include Rainbows::ReverseProxy::EvClient # provides receive_data

    # +addr+ is a packed sockaddr, so it can be either a UNIX or TCP socket
    def initialize(env)
      @env = env
      @rbuf = ""
      @parser = Kcar::Parser.new
      @response = @body = nil
      @headers = Rack::Utils::HeaderHash.new
    end

    # prevents us from sending too much at once and OOM-ing on large uploads
    def stream_input(input)
      if buf = input.read(16384)
        send_data buf
        EM.next_tick { stream_input(input) }
      end
    end

    def on_write_complete
      if @input
        buf = @input.read(16384, @junk) and return write(buf)
        @input = nil
      end
    end

    def unbind
      @env[AsyncCallback].call(@response || Rainbows::ReverseProxy::E502)
    end
  end

  UpstreamSocket = Rainbows::ReverseProxy::UpstreamSocket
  def call(env)
    input = prepare_input!(env)
    io = UpstreamSocket.start(pick_upstream(env))
    sock = EM.attach(io, Backend, env)
    sock.send_data(build_headers(env, input))
    sock.stream_input(input) if input
    throw :async
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
rainbows-4.7.0 lib/rainbows/reverse_proxy/event_machine.rb
rainbows-4.6.2 lib/rainbows/reverse_proxy/event_machine.rb
rainbows-4.6.1 lib/rainbows/reverse_proxy/event_machine.rb
rainbows-4.6.0.4.g4108 lib/rainbows/reverse_proxy/event_machine.rb
rainbows-4.6.0 lib/rainbows/reverse_proxy/event_machine.rb
rainbows-4.5.0 lib/rainbows/reverse_proxy/event_machine.rb
rainbows-4.4.3 lib/rainbows/reverse_proxy/event_machine.rb
rainbows-4.4.2 lib/rainbows/reverse_proxy/event_machine.rb
rainbows-4.4.1.1.gd5c8c lib/rainbows/reverse_proxy/event_machine.rb
rainbows-4.4.1 lib/rainbows/reverse_proxy/event_machine.rb
rainbows-4.4.0 lib/rainbows/reverse_proxy/event_machine.rb
rainbows-4.3.1 lib/rainbows/reverse_proxy/event_machine.rb
rainbows-4.3.0 lib/rainbows/reverse_proxy/event_machine.rb
rainbows-4.2.0 lib/rainbows/reverse_proxy/event_machine.rb
rainbows-4.1.0 lib/rainbows/reverse_proxy/event_machine.rb
rainbows-4.0.0 lib/rainbows/reverse_proxy/event_machine.rb
rainbows-3.4.0 lib/rainbows/reverse_proxy/event_machine.rb
rainbows-3.3.0 lib/rainbows/reverse_proxy/event_machine.rb
rainbows-3.2.0 lib/rainbows/reverse_proxy/event_machine.rb
rainbows-3.1.0 lib/rainbows/reverse_proxy/event_machine.rb