Skip to content

Commit

Permalink
chore: Update NATS connection handling and error logging
Browse files Browse the repository at this point in the history
  • Loading branch information
qjoly committed Sep 14, 2024
1 parent d0c6fee commit dc2454f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
9 changes: 6 additions & 3 deletions coffee-maker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,16 @@ func main() {
os.Exit(1)
}

nc, _ := nats.Connect(os.Getenv("NATS_URL"))
nc, err := nats.Connect(os.Getenv("NATS_URL"), nats.Options{}
if err != nil {
log.Fatal("connect to nats: ", err)
}

defer nc.Close()

js, err := jetstream.New(nc)
if err != nil {
log.Fatal(err)
log.Fatal("create js client: ", err)
}

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
Expand All @@ -60,7 +63,7 @@ func main() {

_, err = js.CreateOrUpdateStream(ctx, cfgStream)
if err != nil {
log.Fatal(err)
log.Fatal("create stream : ", err)
}

cfgConsu := jetstream.ConsumerConfig{
Expand Down
6 changes: 5 additions & 1 deletion routes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,11 @@ func handleHome(w http.ResponseWriter, numberOfPendingOrders int) {
func main() {

url := os.Getenv("NATS_URL")
nc, _ = nats.Connect(url)
nc, err := nats.Connect(url)
if err != nil {
log.Fatal(err)
}

defer nc.Drain()

js, err := jetstream.New(nc)
Expand Down

0 comments on commit dc2454f

Please sign in to comment.