Last active
August 24, 2018 23:50
-
-
Save derekcollison/6706b7be385234388c6ae39635f760e6 to your computer and use it in GitHub Desktop.
NATS Drain Mode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"log" | |
"os" | |
"os/signal" | |
"runtime" | |
"syscall" | |
"github.com/nats-io/go-nats" | |
) | |
func main() { | |
// When we get called back that we are closed, go ahead and exit. | |
closed := func(nc *nats.Conn) { | |
log.Printf("Connection closed, exiting\n") | |
os.Exit(0) | |
} | |
// Connect to NATS | |
nc, err := nats.Connect("tls://demo.nats.io:4443", nats.ClosedHandler(closed)) | |
if err != nil { | |
log.Fatalf("Can't connect: %v\n", err) | |
} | |
// Queue Worker | |
nc.QueueSubscribe("foo", "worker_group", func(m *nats.Msg) { | |
// Do some work, then send reply. | |
nc.Publish(m.Reply, []byte("Your Answer!")) | |
}) | |
// Now handle signal to terminate. | |
c := make(chan os.Signal, 1) | |
signal.Notify(c, syscall.SIGINT) | |
go func() { | |
// Wait for signal | |
<-c | |
log.Printf("Received signal, entering drain mode\n") | |
nc.Drain() | |
}() | |
runtime.Goexit() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment