2022-03-18 04:28:49 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-03-18 04:28:49 +00:00
|
|
|
// Add
|
2022-12-04 10:18:49 +00:00
|
|
|
_, err := js.instance.AddConsumer(stream, &nats.ConsumerConfig{
|
2022-03-18 04:28:49 +00:00
|
|
|
Durable: name,
|
|
|
|
AckPolicy: nats.AckExplicitPolicy,
|
2022-12-04 10:18:49 +00:00
|
|
|
FilterSubject: filterSubject,
|
2022-03-18 04:28:49 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|