Add reload support for ping interval, max pings, and write deadline

This commit is contained in:
Tyler Treat
2017-06-22 17:01:56 -05:00
parent 5501e288a8
commit 88c864b2af
4 changed files with 60 additions and 3 deletions

View File

@@ -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 {

View File

@@ -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

View File

@@ -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.

View File

@@ -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