natsio/jetstream_consumer.go

39 lines
878 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 ...
func (js JetStream) AddConsumer(stream, subject, name string) error {
2022-03-25 17:00:58 +00:00
// Get consumer first, return if existed
consumer, err := js.GetConsumerInfo(stream, name)
if consumer != nil {
return nil
}
// Generate channel name
channel := combineStreamAndSubjectName(stream, subject)
// Add
2022-03-25 17:00:58 +00:00
_, err = js.instance.AddConsumer(stream, &nats.ConsumerConfig{
Durable: name,
AckPolicy: nats.AckExplicitPolicy,
FilterSubject: channel,
})
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
}