Sha256: 4f419cdec773b535953134039c70787cb8f91e438c268eb69f85802234e950a2

Contents?: true

Size: 1.53 KB

Versions: 13

Compression:

Stored size: 1.53 KB

Contents

# -*- encoding: binary -*-
# :enddoc:
# TODO: handle large responses without having it all in memory
module Rainbows::ReverseProxy::Coolio
  LOOP = Cool.io::Loop.default

  class Backend < Cool.io::IO
    include Rainbows::ReverseProxy::EvClient

    def initialize(env, addr, input)
      @env = env
      @input = input
      @junk, @rbuf = "", ""
      @parser = Kcar::Parser.new
      @response = @body = nil
      @headers = Rack::Utils::HeaderHash.new
      super(UpstreamSocket.start(addr)) # kgio-enabled socket
    end

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

    def on_readable
      # avoiding IO#read_nonblock since that's expensive in 1.9.2
      case buf = @_io.kgio_tryread(16384, @junk)
      when String
        receive_data(buf)
      when :wait_readable
        return
      when nil
        @env[AsyncCallback].call(@response)
        return close
      end while true # we always read until EAGAIN or EOF

      rescue => e
        case e
        when Errno::ECONNRESET
          @env[AsyncCallback].call(@response)
          return close
        when SystemCallError
        else
          Unicorn.log_error(@env["rack.logger"], "on_readable", e)
        end
        @env[AsyncCallback].call(Rainbows::ReverseProxy::E502)
        close
    end
  end

  def call(env)
    input = prepare_input!(env)
    sock = Backend.new(env, pick_upstream(env), input).attach(LOOP)
    sock.write(build_headers(env, input))
    throw :async
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
rainbows-4.7.0 lib/rainbows/reverse_proxy/coolio.rb
rainbows-4.6.2 lib/rainbows/reverse_proxy/coolio.rb
rainbows-4.6.1 lib/rainbows/reverse_proxy/coolio.rb
rainbows-4.6.0.4.g4108 lib/rainbows/reverse_proxy/coolio.rb
rainbows-4.6.0 lib/rainbows/reverse_proxy/coolio.rb
rainbows-4.5.0 lib/rainbows/reverse_proxy/coolio.rb
rainbows-4.4.3 lib/rainbows/reverse_proxy/coolio.rb
rainbows-4.4.2 lib/rainbows/reverse_proxy/coolio.rb
rainbows-4.4.1.1.gd5c8c lib/rainbows/reverse_proxy/coolio.rb
rainbows-4.4.1 lib/rainbows/reverse_proxy/coolio.rb
rainbows-4.4.0 lib/rainbows/reverse_proxy/coolio.rb
rainbows-4.3.1 lib/rainbows/reverse_proxy/coolio.rb
rainbows-4.3.0 lib/rainbows/reverse_proxy/coolio.rb