Skip to content

Commit

Permalink
Merge pull request #95 from nats-io/add_nats_url_option_setter
Browse files Browse the repository at this point in the history
[ADDED] Option setter for the URL client connects to
  • Loading branch information
kozlovic committed Jul 19, 2016
2 parents f01862a + d6edc7b commit beba61d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 14 deletions.
11 changes: 5 additions & 6 deletions examples/stan-pub.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,13 @@ func usage() {
}

func main() {
opts := stan.DefaultOptions

var clusterID string
var clientID string
var async bool
var URL string

flag.StringVar(&opts.NatsURL, "s", stan.DefaultNatsURL, "The nats server URLs (separated by comma)")
flag.StringVar(&opts.NatsURL, "server", stan.DefaultNatsURL, "The nats server URLs (separated by comma)")
flag.StringVar(&URL, "s", stan.DefaultNatsURL, "The nats server URLs (separated by comma)")
flag.StringVar(&URL, "server", stan.DefaultNatsURL, "The nats server URLs (separated by comma)")
flag.StringVar(&clusterID, "c", "test-cluster", "The NATS Streaming cluster ID")
flag.StringVar(&clusterID, "cluster", "test-cluster", "The NATS Streaming cluster ID")
flag.StringVar(&clientID, "id", "stan-pub", "The NATS Streaming client ID to connect with")
Expand All @@ -56,9 +55,9 @@ func main() {
usage()
}

sc, err := stan.Connect(clusterID, clientID)
sc, err := stan.Connect(clusterID, clientID, stan.NatsURL(URL))
if err != nil {
log.Fatalf("Can't connect: %v.\nMake sure a NATS Streaming Server is running at: %s", err, opts.NatsURL)
log.Fatalf("Can't connect: %v.\nMake sure a NATS Streaming Server is running at: %s", err, URL)
}
defer sc.Close()

Expand Down
14 changes: 6 additions & 8 deletions examples/stan-sub.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ func printMsg(m *Msg, i int) {
}

func main() {
opts := DefaultOptions

var clusterID string
var clientID string
var showTime bool
Expand All @@ -56,11 +54,12 @@ func main() {
var durable string
var qgroup string
var unsubscribe bool
var URL string

// defaultID := fmt.Sprintf("client.%s", nuid.Next())

flag.StringVar(&opts.NatsURL, "s", DefaultNatsURL, "The nats server URLs (separated by comma)")
flag.StringVar(&opts.NatsURL, "server", DefaultNatsURL, "The nats server URLs (separated by comma)")
flag.StringVar(&URL, "s", DefaultNatsURL, "The nats server URLs (separated by comma)")
flag.StringVar(&URL, "server", DefaultNatsURL, "The nats server URLs (separated by comma)")
flag.StringVar(&clusterID, "c", "test-cluster", "The NATS Streaming cluster ID")
flag.StringVar(&clusterID, "cluster", "test-cluster", "The NATS Streaming cluster ID")
flag.StringVar(&clientID, "id", "", "The NATS Streaming client ID to connect with")
Expand Down Expand Up @@ -90,12 +89,11 @@ func main() {
usage()
}


sc, err := Connect(clusterID, clientID)
sc, err := Connect(clusterID, clientID, NatsURL(URL))
if err != nil {
log.Fatalf("Can't connect: %v.\nMake sure a NATS Streaming Server is running at: %s", err, opts.NatsURL)
log.Fatalf("Can't connect: %v.\nMake sure a NATS Streaming Server is running at: %s", err, URL)
}
log.Printf("Connected to %s clusterID: [%s] clientID: [%s]\n", opts.NatsURL, clusterID, clientID)
log.Printf("Connected to %s clusterID: [%s] clientID: [%s]\n", URL, clusterID, clientID)

subj, i := args[0], 0

Expand Down
8 changes: 8 additions & 0 deletions stan.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ var DefaultOptions = Options{
// Option is a function on the options for a connection.
type Option func(*Options) error

// NatsURL is an Option to set the URL the client should connect to.
func NatsURL(u string) Option {
return func(o *Options) error {
o.NatsURL = u
return nil
}
}

// ConnectWait is an Option to set the timeout for establishing a connection.
func ConnectWait(t time.Duration) Option {
return func(o *Options) error {
Expand Down
11 changes: 11 additions & 0 deletions stan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1928,3 +1928,14 @@ func TestMaxPubAcksInflight(t *testing.T) {
t.Fatal("Should have blocked after 1 message sent")
}
}

func TestNatsURLOption(t *testing.T) {
s := RunServer(clusterName)
defer s.Shutdown()

sc, err := Connect(clusterName, clientName, NatsURL("nats://localhost:5555"))
if err == nil {
sc.Close()
t.Fatal("Expected connect to fail")
}
}

0 comments on commit beba61d

Please sign in to comment.