From 88c864b2af767b9ae2de399a8e9514c24b65e1f1 Mon Sep 17 00:00:00 2001 From: Tyler Treat Date: Thu, 22 Jun 2017 17:01:56 -0500 Subject: [PATCH] Add reload support for ping interval, max pings, and write deadline --- server/configs/reload/reload.conf | 3 ++ server/opts.go | 5 ++-- server/reload.go | 46 +++++++++++++++++++++++++++++++ server/reload_test.go | 9 ++++++ 4 files changed, 60 insertions(+), 3 deletions(-) diff --git a/server/configs/reload/reload.conf b/server/configs/reload/reload.conf index 17b179de..eba5533d 100644 --- a/server/configs/reload/reload.conf +++ b/server/configs/reload/reload.conf @@ -10,6 +10,9 @@ log_file: "/tmp/gnatsd-2.log" # change on reload pid_file: "/tmp/gnatsd.pid" # change on reload max_control_line: 512 # change on reload +ping_interval: 5 # change on reload +ping_max: 1 # change on reload +write_deadline: "2s" # change on reload # Enable TLS on reload tls { diff --git a/server/opts.go b/server/opts.go index 41f3ecdc..5323df2e 100644 --- a/server/opts.go +++ b/server/opts.go @@ -712,9 +712,8 @@ func MergeOptions(fileOpts, flagOpts *Options) *Options { if flagOpts.Trace { opts.Trace = true } - // Logtime flag defaults to true, so only take precedence if it was set to false. - if !flagOpts.Logtime { - opts.Logtime = false + if flagOpts.Logtime { + opts.Logtime = true } if flagOpts.LogFile != "" { opts.LogFile = flagOpts.LogFile diff --git a/server/reload.go b/server/reload.go index 312048cd..903d5be4 100644 --- a/server/reload.go +++ b/server/reload.go @@ -10,6 +10,7 @@ import ( "reflect" "strings" "sync/atomic" + "time" ) // FlagSnapshot captures the server options as specified by CLI flags at @@ -368,6 +369,45 @@ func (m *maxPayloadOption) Apply(server *Server) { server.Noticef("Reloaded: max_payload = %d", m.newValue) } +// pingIntervalOption implements the option interface for the `ping_interval` +// setting. +type pingIntervalOption struct { + noopOption + newValue time.Duration +} + +// Apply is a no-op because the ping interval will be reloaded after options +// are applied. +func (p *pingIntervalOption) Apply(server *Server) { + server.Noticef("Reloaded: ping_interval = %s", p.newValue) +} + +// maxPingsOutOption implements the option interface for the `ping_max` +// setting. +type maxPingsOutOption struct { + noopOption + newValue int +} + +// Apply is a no-op because the ping interval will be reloaded after options +// are applied. +func (m *maxPingsOutOption) Apply(server *Server) { + server.Noticef("Reloaded: ping_max = %d", m.newValue) +} + +// writeDeadlineOption implements the option interface for the `write_deadline` +// setting. +type writeDeadlineOption struct { + noopOption + newValue time.Duration +} + +// Apply is a no-op because the write deadline will be reloaded after options +// are applied. +func (w *writeDeadlineOption) Apply(server *Server) { + server.Noticef("Reloaded: write_deadline = %s", w.newValue) +} + // Reload reads the current configuration file and applies any supported // changes. This returns an error if the server was not started with a config // file or an option which doesn't support hot-swapping was changed. @@ -467,6 +507,12 @@ func (s *Server) diffOptions(newOpts *Options) ([]option, error) { diffOpts = append(diffOpts, &maxControlLineOption{newValue: newValue.(int)}) case "maxpayload": diffOpts = append(diffOpts, &maxPayloadOption{newValue: newValue.(int64)}) + case "pinginterval": + diffOpts = append(diffOpts, &pingIntervalOption{newValue: newValue.(time.Duration)}) + case "maxpingsout": + diffOpts = append(diffOpts, &maxPingsOutOption{newValue: newValue.(int)}) + case "writedeadline": + diffOpts = append(diffOpts, &writeDeadlineOption{newValue: newValue.(time.Duration)}) case "nolog": // Ignore NoLog option since it's not parsed and only used in // testing. diff --git a/server/reload_test.go b/server/reload_test.go index d26d9afc..37c86f7d 100644 --- a/server/reload_test.go +++ b/server/reload_test.go @@ -223,6 +223,15 @@ func TestConfigReload(t *testing.T) { if updated.MaxControlLine != 512 { t.Fatalf("MaxControlLine is incorrect.\nexpected: 512\ngot: %d", updated.MaxControlLine) } + if updated.PingInterval != 5*time.Second { + t.Fatalf("PingInterval is incorrect.\nexpected 5s\ngot: %s", updated.PingInterval) + } + if updated.MaxPingsOut != 1 { + t.Fatalf("MaxPingsOut is incorrect.\nexpected 1\ngot: %d", updated.MaxPingsOut) + } + if updated.WriteDeadline != 2*time.Second { + t.Fatalf("WriteDeadline is incorrect.\nexpected 2s\ngot: %s", updated.WriteDeadline) + } } // Ensure Reload supports TLS config changes. Test this by starting a server