natsio/jetstream_consumer.go

36 lines
802 B
Go
Raw Normal View History

package natsio
import (
"errors"
"fmt"
"github.com/nats-io/nats.go"
)
// GetConsumerInfo ...
2022-12-04 15:10:05 +00:00
func (js JetStream) GetConsumerInfo(name string) (*nats.ConsumerInfo, error) {
return js.instance.ConsumerInfo(js.streamName, name)
}
// AddConsumer ...
2022-12-04 15:10:05 +00:00
func (js JetStream) AddConsumer(name, filterSubject string) error {
2022-03-25 17:00:58 +00:00
// Get consumer first, return if existed
2022-12-04 15:10:05 +00:00
consumer, _ := js.GetConsumerInfo(name)
2022-03-25 17:00:58 +00:00
if consumer != nil {
return nil
}
// Add
2022-12-04 15:10:05 +00:00
_, err := js.instance.AddConsumer(js.streamName, &nats.ConsumerConfig{
Durable: name,
AckPolicy: nats.AckExplicitPolicy,
2022-12-04 10:18:49 +00:00
FilterSubject: filterSubject,
})
if err != nil {
2022-12-04 15:10:05 +00:00
msg := fmt.Sprintf("[NATS JETSTREAM] - add consumer %s for stream #%s error: %s", name, js.streamName, err.Error())
return errors.New(msg)
}
return nil
}