Class: ExperellaProxy::BackendServer

Inherits:
Object
  • Object
show all
Defined in:
lib/experella-proxy/backend_server.rb

Overview

BackendServer objects contain information on available BackendServers

Accepts Requests based on Request header information and it’s message_matcher

See #initialize

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port, options = {}) ⇒ BackendServer

Constructor of the BackendServer

required keys

:host         Host address of the Server, can be IP or domain
:port         Port of the Server, Integervalue as String

optional keys

:name         Name of the Server used in the Logger and Cashing, String
              Will default to #{host}:#{port} but needs to be unique, though theoretical there can be
              multiple servers with the same host:port value pair!

:concurrency  max Number of concurrent connections, Integervalue as String. Default is 1

:accepts      Hash containing keys matching HTTP headers or URI :path, :port, :query
              Values are Regexp as Strings or as Regex, use ^((?!pattern).)*$ to negate matching
              Care: Will match any Header/value pairs not defined in the accepts Hash

:mangle       Hash containing keys matching HTTP headers. Values can be callable block or Strings
              Mangle modifies the header value based on the given block or replaces the header value with the String

mangle is applied in Connection#connect_backendserver

Parameters:

  • host (String)

    host domain-URL oder IP

  • port (String)

    port as string

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :name (String)

    name used in logs and for storage. will use Host:Port if no name is specified

  • :concurrency (String)

    concurrency. will use 1 as default

  • :accepts (Hash|Proc)

    message_pattern that will be converted to a message_matcher or an arbitrary message_matcher as proc Empty Hash is default

  • :mangle (Hash)

    Hash which can modify request headers. Keys get symbolized. nil is default



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/experella-proxy/backend_server.rb', line 43

def initialize(host, port, options={})
  @host = host # host URL as string
  @port = port # port as string
  @name = options[:name] || "#{host}:#{port}"
  if options[:concurrency].nil?
    @concurrency = 1
  else
    @concurrency = options[:concurrency].to_i
  end
  @workload = 0

  make_message_matcher(options[:accepts])

  # mangle can be nil
  @mangle = options[:mangle]
               # convert keys to symbols to match request header keys
  @mangle = @mangle.reduce({}){ |memo, (k, v)| memo[k.to_sym] = v; memo } unless @mangle.nil?
end

Instance Attribute Details

#concurrencyObject

Returns the value of attribute concurrency.



8
9
10
# File 'lib/experella-proxy/backend_server.rb', line 8

def concurrency
  @concurrency
end

#hostObject

Returns the value of attribute host.



8
9
10
# File 'lib/experella-proxy/backend_server.rb', line 8

def host
  @host
end

#mangleObject (readonly)

Returns the value of attribute mangle.



9
10
11
# File 'lib/experella-proxy/backend_server.rb', line 9

def mangle
  @mangle
end

#message_matcherObject (readonly)

Returns the value of attribute message_matcher.



9
10
11
# File 'lib/experella-proxy/backend_server.rb', line 9

def message_matcher
  @message_matcher
end

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/experella-proxy/backend_server.rb', line 8

def name
  @name
end

#portObject

Returns the value of attribute port.



8
9
10
# File 'lib/experella-proxy/backend_server.rb', line 8

def port
  @port
end

#workloadObject

Returns the value of attribute workload.



8
9
10
# File 'lib/experella-proxy/backend_server.rb', line 8

def workload
  @workload
end

Instance Method Details

#accept?(request) ⇒ Boolean

compares Backend servers message_matcher to request object

Parameters:

  • request (Request)

    a request object

Returns:

  • (Boolean)

    true if BackendServer accepts the Request, false otherwise



66
67
68
69
70
# File 'lib/experella-proxy/backend_server.rb', line 66

def accept?(request)
  res = @message_matcher.call(request)
  # puts "#{name} #{request.header['request_url']} #{res}"
  res
end

#make_message_matcher(obj) ⇒ Object

Makes a message matching block from the message_pattern.

Parameters:

  • obj (Hash|Proc)

    hash containing a message_pattern that will be converted to a message_matcher proc or an arbitrary own message_matcher



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/experella-proxy/backend_server.rb', line 75

def make_message_matcher(obj)
  @message_matcher = if obj.respond_to?(:call)
                       obj
  else
    # precompile message pattern keys to symbols and values to regexp objects
    keys = (obj || {}).reduce({}){ |memo, (k, v)| memo[k.to_sym] = Regexp.new(v); memo }
    lambda do |request| # TODO: ugly!
      ret = true
      keys.each do |key, pattern|
        # use uri for :port :path and :query keys
        if key == :port || key == :path || key == :query
          ret = false unless request.uri[key] && request.uri[key].match(pattern)
        else # use headers
          ret = false unless request.header[key] && request.header[key].match(pattern)
        end
        break unless ret # stop as soon as possible
      end
      ret
    end # lambda
  end
end