Skip to content

Commit 2186827

Browse files
authored
Merge pull request #874 from nats-io/fix_logtime_reload
[FIXED] Logtime reset to true on config reload
2 parents c310489 + d8817a3 commit 2186827

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

server/opts.go

+18
Original file line numberDiff line numberDiff line change
@@ -2249,6 +2249,9 @@ func ConfigureOptions(fs *flag.FlagSet, args []string, printVersion, printHelp,
22492249
err error
22502250
)
22512251

2252+
// IMPORTANT: If you add boolean flags and use a 'true' as the default,
2253+
// check what needs to be done for FlagSnapshot down below.
2254+
// See logtimeIsExplicitlySet for reference.
22522255
fs.BoolVar(&showHelp, "h", false, "Show this message.")
22532256
fs.BoolVar(&showHelp, "help", false, "Show this message.")
22542257
fs.IntVar(&opts.Port, "port", 0, "Port to listen on.")
@@ -2300,6 +2303,7 @@ func ConfigureOptions(fs *flag.FlagSet, args []string, printVersion, printHelp,
23002303
fs.StringVar(&opts.TLSCert, "tlscert", "", "Server certificate file.")
23012304
fs.StringVar(&opts.TLSKey, "tlskey", "", "Private key for server certificate.")
23022305
fs.StringVar(&opts.TLSCaCert, "tlscacert", "", "Client certificate CA for verification.")
2306+
// --- See comment on top of flags definition before adding a flag ---
23032307

23042308
// The flags definition above set "default" values to some of the options.
23052309
// Calling Parse() here will override the default options with any value
@@ -2344,6 +2348,20 @@ func ConfigureOptions(fs *flag.FlagSet, args []string, printVersion, printHelp,
23442348
// Snapshot flag options.
23452349
FlagSnapshot = opts.Clone()
23462350

2351+
// Before parsing the config file, we need to know if boolean
2352+
// flags are explicitly set or not, and for those which default
2353+
// to `true`, we need to reset them otherwise config reload would
2354+
// incorrectly override what is in the config file.
2355+
logtimeIsExplicitlySet := false
2356+
fs.Visit(func(f *flag.Flag) {
2357+
if f.Name == "logtime" {
2358+
logtimeIsExplicitlySet = true
2359+
}
2360+
})
2361+
if !logtimeIsExplicitlySet {
2362+
FlagSnapshot.Logtime = false
2363+
}
2364+
23472365
// Process signal control.
23482366
if signal != "" {
23492367
if err := processSignal(signal); err != nil {

server/reload_test.go

+35
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"crypto/tls"
1818
"encoding/base64"
1919
"encoding/json"
20+
"flag"
2021
"fmt"
2122
"io/ioutil"
2223
"net"
@@ -3215,3 +3216,37 @@ func TestConfigReloadNotPreventedByGateways(t *testing.T) {
32153216
t.Fatalf("Expected Reload to return a not supported error, got %v", err)
32163217
}
32173218
}
3219+
3220+
func TestConfigReloadLogtime(t *testing.T) {
3221+
logfile := "logtime.log"
3222+
defer os.Remove(logfile)
3223+
content := `
3224+
listen: "127.0.0.1:-1"
3225+
logfile: "%s"
3226+
logtime: false
3227+
`
3228+
conf := createConfFile(t, []byte(fmt.Sprintf(content, logfile)))
3229+
defer os.Remove(conf)
3230+
3231+
// For this test, we need to invoke ConfigureOptions which is what main.go does.
3232+
fs := flag.NewFlagSet("test", flag.ContinueOnError)
3233+
opts, err := ConfigureOptions(fs, []string{"-c", conf}, nil, nil, nil)
3234+
if err != nil {
3235+
t.Fatalf("Error processing config: %v", err)
3236+
}
3237+
opts.NoSigs = true
3238+
s := RunServer(opts)
3239+
defer s.Shutdown()
3240+
3241+
if s.getOpts().Logtime {
3242+
t.Fatal("Logtime should be set to false")
3243+
}
3244+
3245+
if err := s.Reload(); err != nil {
3246+
t.Fatalf("Error on reload: %v", err)
3247+
}
3248+
3249+
if s.getOpts().Logtime {
3250+
t.Fatal("Logtime should be set to false")
3251+
}
3252+
}

0 commit comments

Comments
 (0)