2022-03-18 04:28:49 +00:00
|
|
|
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)
|
2022-03-18 04:28:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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,
|
2022-03-18 04:28:49 +00:00
|
|
|
AckPolicy: nats.AckExplicitPolicy,
|
2022-12-04 10:18:49 +00:00
|
|
|
FilterSubject: filterSubject,
|
2022-03-18 04:28:49 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
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())
|
2022-03-18 04:28:49 +00:00
|
|
|
return errors.New(msg)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|