Class: Scales::Server::Proxy::Backend
- Inherits:
-
Object
- Object
- Scales::Server::Proxy::Backend
- Defined in:
- lib/scales-server/proxy/backend.rb
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#load ⇒ Object
Returns the value of attribute load.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#url ⇒ Object
(also: #to_s)
readonly
Returns the value of attribute url.
Class Method Summary collapse
- .add(ports) ⇒ Object
- .list ⇒ Object
- .ports ⇒ Object
- .select(strategy = :balanced) {|backend| ... } ⇒ Object
- .strategy ⇒ Object
Instance Method Summary collapse
- #decrement_counter ⇒ Object
- #increment_counter ⇒ Object
-
#initialize(options = {}) ⇒ Backend
constructor
A new instance of Backend.
Constructor Details
#initialize(options = {}) ⇒ Backend
Returns a new instance of Backend.
11 12 13 14 15 16 17 |
# File 'lib/scales-server/proxy/backend.rb', line 11 def initialize(={}) raise ArgumentError, "Please provide a :url and :load" unless [:url] @url = [:url] @load = [:load] || 0 parsed = URI.parse(@url) @host, @port = parsed.host, parsed.port end |
Instance Attribute Details
#host ⇒ Object (readonly)
Returns the value of attribute host.
7 8 9 |
# File 'lib/scales-server/proxy/backend.rb', line 7 def host @host end |
#load ⇒ Object
Returns the value of attribute load.
8 9 10 |
# File 'lib/scales-server/proxy/backend.rb', line 8 def load @load end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
7 8 9 |
# File 'lib/scales-server/proxy/backend.rb', line 7 def port @port end |
#url ⇒ Object (readonly) Also known as: to_s
Returns the value of attribute url.
7 8 9 |
# File 'lib/scales-server/proxy/backend.rb', line 7 def url @url end |
Class Method Details
.add(ports) ⇒ Object
19 20 21 |
# File 'lib/scales-server/proxy/backend.rb', line 19 def self.add(ports) ports.each{ |port| @ports << { :url => "http://0.0.0.0:#{port}" } } end |
.list ⇒ Object
46 47 48 |
# File 'lib/scales-server/proxy/backend.rb', line 46 def self.list @list ||= @ports.map { |backend| new backend } end |
.ports ⇒ Object
23 24 25 |
# File 'lib/scales-server/proxy/backend.rb', line 23 def self.ports @ports.map{ |port| port[:url].split(":").last } end |
.select(strategy = :balanced) {|backend| ... } ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/scales-server/proxy/backend.rb', line 27 def self.select(strategy = :balanced) @strategy = strategy.to_sym case @strategy when :balanced backend = list.sort_by { |b| b.load }.first when :roundrobin @pool = list.clone if @pool.nil? || @pool.empty? backend = @pool.shift when :random backend = list[ rand(list.size-1) ] else raise ArgumentError, "Unknown strategy: #{@strategy}" end Callbacks.on_select.call(backend) yield backend if block_given? backend end |
.strategy ⇒ Object
50 51 52 |
# File 'lib/scales-server/proxy/backend.rb', line 50 def self.strategy @strategy end |
Instance Method Details
#decrement_counter ⇒ Object
58 59 60 |
# File 'lib/scales-server/proxy/backend.rb', line 58 def decrement_counter self.load -= 1 end |
#increment_counter ⇒ Object
54 55 56 |
# File 'lib/scales-server/proxy/backend.rb', line 54 def increment_counter self.load += 1 end |