natsio/jetstream_consumer.go

36 lines
805 B
Go
Raw Normal View History

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