Skip to content

Commit

Permalink
Added the "roundrobin" strategy for the BalancingProxy
Browse files Browse the repository at this point in the history
  • Loading branch information
karmi committed Nov 11, 2010
1 parent ae070ef commit f3befbc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
3 changes: 3 additions & 0 deletions examples/balancing.rb
Expand Up @@ -34,6 +34,9 @@ def self.select(strategy = :balanced)
case @strategy
when :balanced
backend = new list.sort { |a,b| a.values <=> b.values }.first.keys.first
when :roundrobin
@pool = list.clone if @pool.nil? || @pool.empty?
backend = new @pool.shift.keys.first
when :random
backend = new list[ rand(list.size-1) ].keys.first
else
Expand Down
21 changes: 20 additions & 1 deletion spec/proxy_spec.rb
Expand Up @@ -173,7 +173,7 @@ def failed

before(:each) do
class BalancingProxy::Backend
@list = nil
@list = nil; @pool = nil
end
end

Expand Down Expand Up @@ -206,6 +206,25 @@ def self.list

end

context "when using the 'roundrobin' strategy" do
it "should select backends in rotating order" do
class BalancingProxy::Backend
def self.list
@list ||= [
{"http://127.0.0.1:3000" => 0},
{"http://127.0.0.2:3000" => 0},
{"http://127.0.0.3:3000" => 0}
]
end
end

BalancingProxy::Backend.select(:roundrobin).host.should == '127.0.0.1'
BalancingProxy::Backend.select(:roundrobin).host.should == '127.0.0.2'
BalancingProxy::Backend.select(:roundrobin).host.should == '127.0.0.3'
BalancingProxy::Backend.select(:roundrobin).host.should == '127.0.0.1'
end
end

context "when using the 'balanced' strategy" do

it "should select the first backend when all backends have the same load" do
Expand Down

0 comments on commit f3befbc

Please sign in to comment.