Class: ExperellaProxy::BackendServer
- Inherits:
-
Object
- Object
- ExperellaProxy::BackendServer
- 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
-
#concurrency ⇒ Object
Returns the value of attribute concurrency.
-
#host ⇒ Object
Returns the value of attribute host.
-
#mangle ⇒ Object
readonly
Returns the value of attribute mangle.
-
#message_matcher ⇒ Object
readonly
Returns the value of attribute message_matcher.
-
#name ⇒ Object
Returns the value of attribute name.
-
#port ⇒ Object
Returns the value of attribute port.
-
#workload ⇒ Object
Returns the value of attribute workload.
Instance Method Summary collapse
-
#accept?(request) ⇒ Boolean
compares Backend servers message_matcher to request object.
-
#initialize(host, port, options = {}) ⇒ BackendServer
constructor
Constructor of the BackendServer.
-
#make_message_matcher(obj) ⇒ Object
Makes a message matching block from the message_pattern.
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
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, ={}) @host = host # host URL as string @port = port # port as string @name = [:name] || "#{host}:#{port}" if [:concurrency].nil? @concurrency = 1 else @concurrency = [:concurrency].to_i end @workload = 0 ([:accepts]) # mangle can be nil @mangle = [: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
#concurrency ⇒ Object
Returns the value of attribute concurrency.
8 9 10 |
# File 'lib/experella-proxy/backend_server.rb', line 8 def concurrency @concurrency end |
#host ⇒ Object
Returns the value of attribute host.
8 9 10 |
# File 'lib/experella-proxy/backend_server.rb', line 8 def host @host end |
#mangle ⇒ Object (readonly)
Returns the value of attribute mangle.
9 10 11 |
# File 'lib/experella-proxy/backend_server.rb', line 9 def mangle @mangle end |
#message_matcher ⇒ Object (readonly)
Returns the value of attribute message_matcher.
9 10 11 |
# File 'lib/experella-proxy/backend_server.rb', line 9 def end |
#name ⇒ Object
Returns the value of attribute name.
8 9 10 |
# File 'lib/experella-proxy/backend_server.rb', line 8 def name @name end |
#port ⇒ Object
Returns the value of attribute port.
8 9 10 |
# File 'lib/experella-proxy/backend_server.rb', line 8 def port @port end |
#workload ⇒ Object
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
66 67 68 69 70 |
# File 'lib/experella-proxy/backend_server.rb', line 66 def accept?(request) res = .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.
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 (obj) = 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 |