natsio/jetstream_consumer.go

36 lines
849 B
Go
Raw Permalink Normal View History

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