Skip to content

Commit

Permalink
Merge pull request #312 from nats-io/consolidated_config
Browse files Browse the repository at this point in the history
[ADDED] Ability to combine NATS and Streaming options in same config
  • Loading branch information
kozlovic committed May 16, 2017
2 parents 4f36aef + 8d6fae7 commit 8a62b7b
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 7 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,10 @@ You can use a configuration file to configure the options specific to the NATS S

Use the `-sc` or `-stan_config` command line parameter to specify the file to use.

For the embedded NATS Server, you can use another configuration file and pass it to the Streaming server using `-c` or `--config` command line parameters.

However, since options do not overlap, it is possible to combine all options into a single file and specify this file using either the `-sc` or `-c` command line parameter.

Note the order in which options are applied during the start of a NATS Streaming server:

1. Start with some reasonable default options.
Expand Down
25 changes: 18 additions & 7 deletions nats-streaming-server.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func parseFlags() (*stand.Options, *natsd.Options) {
var showVersion bool
var natsDebugAndTrace bool
var showTLSHelp bool
var configFile string
var gnatsdConfigFile string

natsOpts := natsd.Options{}

Expand Down Expand Up @@ -226,8 +226,8 @@ func parseFlags() (*stand.Options, *natsd.Options) {
flag.IntVar(&natsOpts.HTTPPort, "http_port", 0, "")
flag.IntVar(&natsOpts.HTTPSPort, "ms", 0, "")
flag.IntVar(&natsOpts.HTTPSPort, "https_port", 0, "")
flag.StringVar(&configFile, "c", "", "")
flag.StringVar(&configFile, "config", "", "")
flag.StringVar(&gnatsdConfigFile, "c", "", "")
flag.StringVar(&gnatsdConfigFile, "config", "", "")
flag.StringVar(&natsOpts.PidFile, "P", "", "")
flag.StringVar(&natsOpts.PidFile, "pid", "", "")
flag.StringVar(&natsOpts.LogFile, "l", "", "")
Expand Down Expand Up @@ -279,10 +279,17 @@ func parseFlags() (*stand.Options, *natsd.Options) {
usage()
}

// If user provides explicit config files for "-sc" and "-c" then use
// given config file only, otherwise, try to parse the same config file
// for each components.
cfgFile := stanConfigFile
if cfgFile == "" && gnatsdConfigFile != "" {
cfgFile = gnatsdConfigFile
}
// Parse NATS Streaming configuration file, updating stanOpts with
// what is found in the file, possibly overriding the defaults.
if stanConfigFile != "" {
if err := stand.ProcessConfigFile(stanConfigFile, stanOpts); err != nil {
if cfgFile != "" {
if err := stand.ProcessConfigFile(cfgFile, stanOpts); err != nil {
natsd.PrintAndDie(fmt.Sprintf("Configuration error: %v", err.Error()))
}
}
Expand All @@ -292,8 +299,12 @@ func parseFlags() (*stand.Options, *natsd.Options) {
}

// Parse NATS config if given
if configFile != "" {
fileOpts, err := natsd.ProcessConfigFile(configFile)
cfgFile = gnatsdConfigFile
if cfgFile == "" && stanConfigFile != "" {
cfgFile = stanConfigFile
}
if cfgFile != "" {
fileOpts, err := natsd.ProcessConfigFile(cfgFile)
if err != nil {
natsd.PrintAndDie(err.Error())
}
Expand Down
60 changes: 60 additions & 0 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"reflect"
"runtime"
"strconv"
Expand Down Expand Up @@ -6235,3 +6236,62 @@ func TestUnlimitedPerChannelLimits(t *testing.T) {
t.Fatalf("Expected 0 messages, store reports %v", n)
}
}

func TestSingleConfFileForBoth(t *testing.T) {
configFile := "config.cfg"
defer os.Remove(configFile)
content := []byte(`
port: 4223
store_limits: {
max_channels: 1
}`)
if err := ioutil.WriteFile(configFile, content, 0666); err != nil {
t.Fatalf("Error writing config file: %v", err)
}
var cmd *exec.Cmd
defer func() {
if cmd != nil {
cmd.Process.Kill()
cmd.Wait()
}
}()
param := []string{"-sc", "-c"}
for i := 0; i < 2; i++ {
cmd = exec.Command("nats-streaming-server", param[i], configFile)
if err := cmd.Start(); err != nil {
t.Fatalf("Error starting process: %v", err)
}
var (
sc stan.Conn
err error
failed int
)
for {
sc, err = stan.Connect(clusterName, clientName,
stan.NatsURL("nats://localhost:4223"),
stan.ConnectWait(250*time.Millisecond))
if err != nil {
failed++
if failed < 10 {
time.Sleep(100 * time.Millisecond)
continue
}
t.Fatalf("Unable to connect: %v", err)
}
break
}
defer sc.Close()
// Should be able to create 1 channel
if err := sc.Publish("foo", []byte("hello")); err != nil {
t.Fatalf("Unexpected error on publish: %v", err)
}
// But not 2.
if err := sc.Publish("bar", []byte("hello")); err == nil {
t.Fatalf("Param %v, should have failed to create a 2nd channel", param[i])
}
sc.Close()
cmd.Process.Kill()
cmd.Wait()
cmd = nil
}
}

0 comments on commit 8a62b7b

Please sign in to comment.