From f3befbcd3ecef70c82ada32f164f50dd00d084b8 Mon Sep 17 00:00:00 2001 From: Karel Minarik Date: Thu, 11 Nov 2010 23:14:29 +0100 Subject: [PATCH] Added the "roundrobin" strategy for the BalancingProxy --- examples/balancing.rb | 3 +++ spec/proxy_spec.rb | 21 ++++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/examples/balancing.rb b/examples/balancing.rb index 92f8cb6..320c215 100644 --- a/examples/balancing.rb +++ b/examples/balancing.rb @@ -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 diff --git a/spec/proxy_spec.rb b/spec/proxy_spec.rb index a1c8b95..05b9920 100644 --- a/spec/proxy_spec.rb +++ b/spec/proxy_spec.rb @@ -173,7 +173,7 @@ def failed before(:each) do class BalancingProxy::Backend - @list = nil + @list = nil; @pool = nil end end @@ -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