Class: ExperellaProxy::Proxy

Inherits:
Object
  • Object
show all
Extended by:
Globals
Defined in:
lib/experella-proxy/proxy.rb

Overview

The proxy

Controls the EventMachine, initializes backends from config and starts proxy servers

Class Method Summary collapse

Methods included from Globals

config, connection_manager, event, logger

Class Method Details

.start(options, &blk) ⇒ Object

Starts the Eventmachine, initializes backends in ConnectionManager and starts the servers defined in config the proxy should listen on

Parameters:

  • options (Hash)

    option Hash passed to the Connection

  • blk (Block)

    Block evaluated in each new Connection



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
# File 'lib/experella-proxy/proxy.rb', line 13

def self.start(options, &blk)
  # initalize backend servers from config
  config.backends.each do |backend|
    connection_manager.add_backend(BackendServer.new(backend[:host], backend[:port], backend))
    logger.info "Initializing backend #{backend[:name]} at #{backend[:host]}:#{backend[:port]} with concurrency\
             #{backend[:concurrency]}"
    logger.info "Backend accepts: #{backend[:accepts].inspect}"
    logger.info "Backend mangles: #{backend[:mangle].inspect}"
  end

  # start eventmachine
  EM.epoll
  EM.run do
    trap("TERM"){ stop }
    trap("INT"){ stop }

    if config.proxy.empty?
      logger.fatal "No proxy host:port address configured. Stopping experella-proxy."
      return stop
    else
      config.proxy.each do |proxy|
        opts = options
        # pass proxy specific options
        unless proxy[:options].nil?
          opts = options.merge(proxy[:options])
        end
        logger.info "Launching experella-proxy (#{ExperellaProxy::VERSION}) at #{proxy[:host]}:#{proxy[:port]} with #{config.timeout}s timeout..."
        logger.info "with options: #{opts.inspect}"
        EventMachine.start_server(proxy[:host], proxy[:port],
                                  Connection, opts) do |conn|
          conn.instance_eval(&blk)
        end
      end
    end
  end
end

.stopObject

Stops the Eventmachine and terminates all connections



53
54
55
56
57
# File 'lib/experella-proxy/proxy.rb', line 53

def self.stop
  if EM.reactor_running?
    EventMachine.stop_event_loop
  end
end