Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions example/natsq/publisher/publisher.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"log"
"math/rand"
"time"
Expand Down Expand Up @@ -29,7 +30,7 @@ func main() {
p.Close()

// JetMode
j, _ := natsq.NewJetProducer(&c)
j, _ := natsq.NewJetProducer(&c, context.Background())
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: Error handling is ignored. Could lead to nil pointer dereference if producer creation fails.

Suggested change
j, _ := natsq.NewJetProducer(&c, context.Background())
j, err := natsq.NewJetProducer(&c, context.Background())
if err != nil {
log.Fatalf("Error creating jet producer: %v", err)
}

j.CreateOrUpdateStream(jetstream.StreamConfig{
Name: "ccc",
Subjects: []string{"ccc", "ddd", "eee"},
Expand All @@ -50,8 +51,8 @@ func main() {

func randSub() string {
source := rand.NewSource(time.Now().UnixNano())
// 创建一个新的随机数生成器
rng := rand.New(source)
// 创建一个新的随机数生成器
rng := rand.New(source)
strings := []string{"ccc", "ddd", "eee"}
randomIndex := rng.Intn(len(strings))
return strings[randomIndex]
Expand Down
3 changes: 2 additions & 1 deletion natsq/producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type JetProducer struct {
// NewJetProducer creates a new JetStream producer.
// It takes a NatsConfig as input and returns a pointer to a JetProducer and an error.
// It connects to the NATS server using the provided configuration and creates a new JetStream context.
func NewJetProducer(c *NatsConfig) (*JetProducer, error) {
func NewJetProducer(c *NatsConfig, ctx context.Context) (*JetProducer, error) {
sc, err := nats.Connect(c.ServerUri, c.Options...)
if err != nil {
return nil, err
Expand All @@ -59,6 +59,7 @@ func NewJetProducer(c *NatsConfig) (*JetProducer, error) {
return &JetProducer{
conn: sc,
js: js,
ctx: ctx,
}, nil
}

Expand Down