diff --git a/src/amqproxy.cr b/src/amqproxy.cr index 691cbc7..902150a 100644 --- a/src/amqproxy.cr +++ b/src/amqproxy.cr @@ -10,9 +10,10 @@ class AMQProxy::CLI @listen_port = ENV["LISTEN_PORT"]? || 5673 @log_level : Log::Severity = Log::Severity::Info @idle_connection_timeout : Int32 = ENV.fetch("IDLE_CONNECTION_TIMEOUT", "5").to_i + @term_timeout = -1 @upstream = ENV["AMQP_URL"]? - def parse_config(path) + def parse_config(path) # ameba:disable Metrics/CyclomaticComplexity INI.parse(File.read(path)).each do |name, section| case name when "main", "" @@ -21,6 +22,7 @@ class AMQProxy::CLI when "upstream" then @upstream = value when "log_level" then @log_level = Log::Severity.parse(value) when "idle_connection_timeout" then @idle_connection_timeout = value.to_i + when "term_timeout" then @term_timeout = value.to_i else raise "Unsupported config #{name}/#{key}" end end @@ -50,6 +52,9 @@ class AMQProxy::CLI parser.on("-t IDLE_CONNECTION_TIMEOUT", "--idle-connection-timeout=SECONDS", "Maxiumum time in seconds an unused pooled connection stays open (default 5s)") do |v| @idle_connection_timeout = v.to_i end + parser.on("--term-timeout=SECONDS", "At TERM the server will wait this many seconds for clients to gracefully close their sockets (default: infinite)") do |v| + @term_timeout = v.to_i + end parser.on("-d", "--debug", "Verbose logging") { @log_level = Log::Severity::Debug } parser.on("-c FILE", "--config=FILE", "Load config file") { |v| parse_config(v) } parser.on("-h", "--help", "Show this help") { puts parser.to_s; exit 0 } @@ -86,8 +91,14 @@ class AMQProxy::CLI first_shutdown = false server.stop_accepting_clients server.disconnect_clients + if @term_timeout >= 0 + spawn do + sleep @term_timeout + abort "Exiting with #{server.client_connections} client connections still open" + end + end else - server.close_sockets + abort "Exiting with #{server.client_connections} client connections still open" end end Signal::INT.trap &shutdown