Skip to content
This repository has been archived by the owner on Jan 4, 2021. It is now read-only.

Commit

Permalink
(#523) support max_reconnect_attempts for the NATS connection
Browse files Browse the repository at this point in the history
On clients the behaviour of never ending reconnects is a problem when
running some client in an automation since you might end up getting
stuck forever in a loop should the configuration be incorrect

This allows setting plugin.nats.max_reconnect_attempts to a integer
setting, it defaults to -1 as today
  • Loading branch information
ripienaar committed Jul 26, 2018
1 parent ac63233 commit c2f9651
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ task :update_ddl do
require "mcollective"

Dir.glob("lib/mcollective/agent/*.ddl") do |ddlfile|
next if ddlfile =~ /^choria_uril/
next if ddlfile =~ /^choria_util/

agent_dir = File.dirname(ddlfile)
agent_name = File.basename(ddlfile, ".ddl")
Expand Down
2 changes: 1 addition & 1 deletion lib/mcollective/connector/nats.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def connect
end

parameters = {
:max_reconnect_attempts => -1,
:max_reconnect_attempts => Integer(get_option("nats.max_reconnect_attempts", "-1")),
:reconnect_time_wait => 1,
:dont_randomize_servers => !choria.randomize_middleware_servers?,
:name => @config.identity,
Expand Down
22 changes: 22 additions & 0 deletions spec/unit/mcollective/connector/nats_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,28 @@ module MCollective
connector.connect
end

it "should support setting maximum reconnects" do
Config.instance.stubs(:pluginconf).returns("nats.max_reconnect_attempts" => "10")
mock_context = OpenSSL::SSL::SSLContext.new
choria.stubs(:ssl_context).returns(mock_context)
choria.expects(:randomize_middleware_servers?).returns(true)

connector.connection.expects(:start).with(
:max_reconnect_attempts => 10,
:reconnect_time_wait => 1,
:dont_randomize_servers => false,
:name => "rspec_identity",
:tls => {
:context => mock_context
},
:servers => ["nats://puppet:4222"]
)

choria.expects(:check_ssl_setup)

connector.connect
end

it "should connect" do
mock_context = OpenSSL::SSL::SSLContext.new
choria.stubs(:ssl_context).returns(mock_context)
Expand Down

0 comments on commit c2f9651

Please sign in to comment.