Skip to content

Commit

Permalink
Updated Examples
Browse files Browse the repository at this point in the history
Log the error code on closed for sub, qsub, and rply examples.
Use Drain() for qsub and rply examples.

Signed-off-by: Derek Collison <derek@nats.io>
  • Loading branch information
derekcollison committed Jun 5, 2019
1 parent e2837a2 commit 7b479cd
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
14 changes: 11 additions & 3 deletions examples/nats-qsub/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"flag"
"log"
"os"
"runtime"
"os/signal"
"time"

"github.com/nats-io/nats.go"
Expand Down Expand Up @@ -92,7 +92,15 @@ func main() {
log.SetFlags(log.LstdFlags)
}

runtime.Goexit()
// Setup the interrupt handler to drain so we don't miss
// requests when scaling down.
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
<-c
log.Println()
log.Printf("Draining...")
nc.Drain()
log.Fatalf("Exiting")
}

func setupConnOptions(opts []nats.Option) []nats.Option {
Expand All @@ -108,7 +116,7 @@ func setupConnOptions(opts []nats.Option) []nats.Option {
log.Printf("Reconnected [%s]", nc.ConnectedUrl())
}))
opts = append(opts, nats.ClosedHandler(func(nc *nats.Conn) {
log.Fatal("Exiting, no servers available")
log.Fatalf("Exiting: %v", nc.LastError())
}))
return opts
}
21 changes: 15 additions & 6 deletions examples/nats-rply/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"flag"
"log"
"os"
"runtime"
"os/signal"
"time"

"github.com/nats-io/nats.go"
Expand All @@ -28,7 +28,7 @@ import (
// nats-rply -s demo.nats.io:4443 <subject> <response> (TLS version)

func usage() {
log.Printf("Usage: nats-rply [-s server] [-creds file] [-t] <subject> <response>\n")
log.Printf("Usage: nats-rply [-s server] [-creds file] [-t] [-q queue] <subject> <response>\n")
flag.PrintDefaults()
}

Expand All @@ -45,6 +45,7 @@ func main() {
var urls = flag.String("s", nats.DefaultURL, "The nats server URLs (separated by comma)")
var userCreds = flag.String("creds", "", "User Credentials File")
var showTime = flag.Bool("t", false, "Display timestamps")
var queueName = flag.String("q", "NATS-RPLY-22", "Queue Group Name")
var showHelp = flag.Bool("h", false, "Show help message")

log.SetFlags(0)
Expand Down Expand Up @@ -77,10 +78,10 @@ func main() {

subj, reply, i := args[0], args[1], 0

nc.Subscribe(subj, func(msg *nats.Msg) {
nc.QueueSubscribe(subj, *queueName, func(msg *nats.Msg) {
i++
printMsg(msg, i)
nc.Publish(msg.Reply, []byte(reply))
msg.Respond([]byte(reply))
})
nc.Flush()

Expand All @@ -93,7 +94,15 @@ func main() {
log.SetFlags(log.LstdFlags)
}

runtime.Goexit()
// Setup the interrupt handler to drain so we don't miss
// requests when scaling down.
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
<-c
log.Println()
log.Printf("Draining...")
nc.Drain()
log.Fatalf("Exiting")
}

func setupConnOptions(opts []nats.Option) []nats.Option {
Expand All @@ -109,7 +118,7 @@ func setupConnOptions(opts []nats.Option) []nats.Option {
log.Printf("Reconnected [%s]", nc.ConnectedUrl())
}))
opts = append(opts, nats.ClosedHandler(func(nc *nats.Conn) {
log.Fatal("Exiting, no servers available")
log.Fatalf("Exiting: %v", nc.LastError())
}))
return opts
}
2 changes: 1 addition & 1 deletion examples/nats-sub/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func setupConnOptions(opts []nats.Option) []nats.Option {
log.Printf("Reconnected [%s]", nc.ConnectedUrl())
}))
opts = append(opts, nats.ClosedHandler(func(nc *nats.Conn) {
log.Fatal("Exiting, no servers available")
log.Fatalf("Exiting: %v", nc.LastError())
}))
return opts
}

0 comments on commit 7b479cd

Please sign in to comment.