Class: Heroku::Client::Rendezvous

Inherits:
Object
  • Object
show all
Defined in:
lib/rib-heroku/monkey_patch.rb

Instance Method Summary collapse

Instance Method Details

#startObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rib-heroku/monkey_patch.rb', line 5

def start
  uri = URI.parse(rendezvous_url)
  host, port, secret = uri.host, uri.port, uri.path[1..-1]

  ssl_socket = Timeout.timeout(connect_timeout) do
    ssl_context = OpenSSL::SSL::SSLContext.new
    if ((host =~ /heroku\.com$/) && !(ENV["HEROKU_SSL_VERIFY"] == "disable"))
      ssl_context.ca_file = File.expand_path("../../../../data/cacert.pem", __FILE__)
      ssl_context.verify_mode = OpenSSL::SSL::VERIFY_PEER
    end
    tcp_socket = TCPSocket.open(host, port)
    ssl_socket = OpenSSL::SSL::SSLSocket.new(tcp_socket, ssl_context)
    ssl_socket.connect
    ssl_socket.puts(secret)
    ssl_socket.readline
    ssl_socket
  end

  on_connect.call if on_connect

  readables = [input, ssl_socket].compact

  # begin patch
  Rib::Heroku.load(ssl_socket, output, activity_timeout)
  # end patch

  begin
    loop do
      if o = IO.select(readables, nil, nil, activity_timeout)
        if (input && (o.first.first == input))
          begin
            data = input.readpartial(10000)
          rescue EOFError
            readables.delete(input)
            next
          end
          ssl_socket.write(data)
          ssl_socket.flush
        elsif (o.first.first == ssl_socket)
          begin
            data = ssl_socket.readpartial(10000)
          rescue EOFError
            break
          end
          output.write(fixup(data))
        end
      else
        raise(Timeout::Error.new)
      end
    end
  rescue Interrupt
    ssl_socket.write(3.chr)
    ssl_socket.flush
    retry
  rescue SignalException => e
    if Signal.list["QUIT"] == e.signo
      ssl_socket.write(28.chr)
      ssl_socket.flush
      retry
    end
    raise
  rescue Errno::EIO
  end
end