Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Weights in generated config #131

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -75,6 +82,3 @@ DEPENDENCIES
rake
rspec (~> 3.1.0)
synapse!

BUNDLED WITH
1.10.5
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 ###

Expand Down
5 changes: 5 additions & 0 deletions lib/synapse/haproxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 }
Expand Down
29 changes: 27 additions & 2 deletions spec/lib/synapse/haproxy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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