diff --git a/Gemfile.lock b/Gemfile.lock index 07b0aba3..ce332ef4 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -24,6 +24,7 @@ GEM excon (0.45.4) ffi (1.9.3-java) json (1.8.3) + json (1.8.3-java) little-plugger (1.1.3) logging (1.8.2) little-plugger (>= 1.1.3) @@ -33,6 +34,7 @@ GEM multi_json (1.11.2) nokogiri (1.6.6.2) mini_portile (~> 0.6.0) + nokogiri (1.6.6.2-java) pry (0.9.12.2) coderay (~> 1.0.5) method_source (~> 0.8) @@ -58,12 +60,17 @@ GEM rspec-support (~> 3.1.0) rspec-support (3.1.2) slop (3.4.6) + slyphon-log4j (1.2.15) + slyphon-zookeeper_jar (3.3.5-java) spoon (0.0.4) ffi zk (1.9.5) logging (~> 1.8.2) zookeeper (~> 1.4.0) zookeeper (1.4.10) + zookeeper (1.4.10-java) + slyphon-log4j (= 1.2.15) + slyphon-zookeeper_jar (= 3.3.5) PLATFORMS java @@ -75,6 +82,3 @@ DEPENDENCIES rake rspec (~> 3.1.0) synapse! - -BUNDLED WITH - 1.10.5 diff --git a/README.md b/README.md index 32b503f0..8826890b 100644 --- a/README.md +++ b/README.md @@ -222,6 +222,7 @@ This section is its own hash, which should contain the following keys: * `backend`: additional lines passed to the HAProxy config in the `backend` stanza of this service * `listen`: these lines will be parsed and placed in the correct `frontend`/`backend` section as applicable; you can put lines which are the same for the frontend and backend here. * `shared_frontend`: optional: haproxy configuration directives for a shared http frontend (see below) +* `ignore_weights`: optional: stops haproxy backend 'weight' options being generated, even if the Nerve registrations contain this information. This will cause all backend servers to be treated equally by haproxy. This defaults to off (and so weights *will* be used by default). ### Configuring HAProxy ### diff --git a/lib/synapse/haproxy.rb b/lib/synapse/haproxy.rb index 89c50077..0fcf0602 100644 --- a/lib/synapse/haproxy.rb +++ b/lib/synapse/haproxy.rb @@ -533,6 +533,7 @@ def initialize(opts) @opts['do_writes'] = true unless @opts.key?('do_writes') @opts['do_socket'] = true unless @opts.key?('do_socket') @opts['do_reloads'] = true unless @opts.key?('do_reloads') + @opts['ignore_weights'] = true if @opts.key?('ignore_weights') and @opts['ignore_weights'] # how to restart haproxy @restart_interval = @opts.fetch('restart_interval', 2).to_i @@ -719,6 +720,10 @@ def generate_backend_stanza(watcher, config) backend = backends[backend_name] b = "\tserver #{backend_name} #{backend['host']}:#{backend['port']}" b = "#{b} cookie #{backend_name}" unless config.include?('mode tcp') + if !@opts['ignore_weights'] && backend.has_key?('weight') + weight = backend['weight'].to_i + b = "#{b} weight #{weight}" + end b = "#{b} #{watcher.haproxy['server_options']}" b = "#{b} disabled" unless backend['enabled'] b } diff --git a/spec/lib/synapse/haproxy_spec.rb b/spec/lib/synapse/haproxy_spec.rb index a90d2ec7..b686253a 100644 --- a/spec/lib/synapse/haproxy_spec.rb +++ b/spec/lib/synapse/haproxy_spec.rb @@ -5,15 +5,18 @@ class MockWatcher; end; describe Synapse::Haproxy do subject { Synapse::Haproxy.new(config['haproxy']) } - let(:mockwatcher) do + def createmockwatcher(backends) mockWatcher = double(Synapse::ServiceWatcher) allow(mockWatcher).to receive(:name).and_return('example_service') - backends = [{ 'host' => 'somehost', 'port' => '5555'}] allow(mockWatcher).to receive(:backends).and_return(backends) allow(mockWatcher).to receive(:haproxy).and_return({'server_options' => "check inter 2000 rise 3 fall 2"}) mockWatcher end + let(:mockwatcher) do + createmockwatcher [{ 'host' => 'somehost', 'port' => '5555'}] + end + it 'updating the config' do expect(subject).to receive(:generate_config) subject.update_config([mockwatcher]) @@ -29,4 +32,26 @@ class MockWatcher; end; expect(subject.generate_backend_stanza(mockwatcher, mockConfig)).to eql(["\nbackend example_service", ["\tmode tcp"], ["\tserver somehost:5555 somehost:5555 check inter 2000 rise 3 fall 2"]]) end + it 'generates backend stanza with weight' do + mockConfig = [] + expect(subject.generate_backend_stanza(createmockwatcher([{ 'weight' => 1, 'host' => 'somehost', 'port' => '5555'}]), mockConfig)).to eql(["\nbackend example_service", [], ["\tserver somehost:5555 somehost:5555 cookie somehost:5555 weight 1 check inter 2000 rise 3 fall 2"]]) + end + + it 'generates backend stanza with bad weight = 0' do + mockConfig = [] + expect(subject.generate_backend_stanza(createmockwatcher([{ 'weight' => 'hi', 'host' => 'somehost', 'port' => '5555'}]), mockConfig)).to eql(["\nbackend example_service", [], ["\tserver somehost:5555 somehost:5555 cookie somehost:5555 weight 0 check inter 2000 rise 3 fall 2"]]) + end + + it 'generates backend stanza with nil weight = 0' do + mockConfig = [] + expect(subject.generate_backend_stanza(createmockwatcher([{ 'weight' => nil, 'host' => 'somehost', 'port' => '5555'}]), mockConfig)).to eql(["\nbackend example_service", [], ["\tserver somehost:5555 somehost:5555 cookie somehost:5555 weight 0 check inter 2000 rise 3 fall 2"]]) + end + + it 'generates backend stanza without weight' do + mockConfig = [] + expect(subject.generate_backend_stanza(createmockwatcher([{ 'host' => 'somehost', 'port' => '5555'}]), mockConfig)).to eql(["\nbackend example_service", [], ["\tserver somehost:5555 somehost:5555 cookie somehost:5555 check inter 2000 rise 3 fall 2"]]) + end + + + end