Skip to content

Commit

Permalink
Merge pull request #148 from wallyqs/cluster-listen-arg
Browse files Browse the repository at this point in the history
Add --cluster_listen flag to set cluster addr and port
  • Loading branch information
derekcollison committed Nov 30, 2015
2 parents e16fc7f + 4958f0f commit 6dd81b0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
40 changes: 40 additions & 0 deletions gnatsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ package main

import (
"flag"
"fmt"
"net"
"net/url"
"os"
"strings"

Expand Down Expand Up @@ -53,6 +56,7 @@ func main() {
flag.BoolVar(&showVersion, "v", false, "Print version information.")
flag.IntVar(&opts.ProfPort, "profile", 0, "Profiling HTTP port")
flag.StringVar(&opts.RoutesStr, "routes", "", "Routes to actively solicit a connection.")
flag.StringVar(&opts.ClusterListenStr, "cluster_listen", "", "Cluster url from which members can solicit routes.")
flag.BoolVar(&showTlsHelp, "help_tls", false, "TLS help.")

flag.BoolVar(&opts.TLS, "tls", false, "Enable TLS.")
Expand Down Expand Up @@ -113,6 +117,12 @@ func main() {
// Configure TLS based on any present flags
configureTLS(&opts)

// Configure cluster opts if explicitly set via flags.
err = configureClusterOpts(&opts)
if err != nil {
server.PrintAndDie(err.Error())
}

// Create the server with appropriate options.
s := server.New(&opts)

Expand Down Expand Up @@ -189,3 +199,33 @@ func configureTLS(opts *server.Options) {
server.PrintAndDie(err.Error())
}
}

func configureClusterOpts(opts *server.Options) error {
if opts.ClusterListenStr == "" {
return nil
}

clusterUrl, err := url.Parse(opts.ClusterListenStr)
h, p, err := net.SplitHostPort(clusterUrl.Host)
if err != nil {
return err
}
opts.ClusterHost = h
_, err = fmt.Sscan(p, &opts.ClusterPort)
if err != nil {
return err
}

if clusterUrl.User != nil {
pass, hasPassword := clusterUrl.User.Password()
if !hasPassword {
return fmt.Errorf("Expected cluster password to be set.")
}
opts.ClusterPassword = pass

user := clusterUrl.User.Username()
opts.ClusterUsername = user
}

return nil
}
3 changes: 2 additions & 1 deletion server/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import (
"io/ioutil"
"net"
"net/url"
"os"
"strconv"
"strings"
"time"

"github.com/nats-io/gnatsd/conf"
"os"
)

// Options block for gnatsd server.
Expand Down Expand Up @@ -44,6 +44,7 @@ type Options struct {
ClusterAuthTimeout float64 `json:"auth_timeout"`
ClusterTLSTimeout float64 `json:"-"`
ClusterTLSConfig *tls.Config `json:"-"`
ClusterListenStr string `json:"-"`
ProfPort int `json:"-"`
PidFile string `json:"-"`
LogFile string `json:"-"`
Expand Down

0 comments on commit 6dd81b0

Please sign in to comment.