assign stream name to config
This commit is contained in:
parent
a94138e574
commit
b3519790e2
|
@ -18,6 +18,9 @@ type Config struct {
|
||||||
|
|
||||||
// RequestTimeout
|
// RequestTimeout
|
||||||
RequestTimeout time.Duration
|
RequestTimeout time.Duration
|
||||||
|
|
||||||
|
// Stream name
|
||||||
|
StreamName string
|
||||||
}
|
}
|
||||||
|
|
||||||
// TLSConfig ...
|
// TLSConfig ...
|
||||||
|
|
|
@ -8,27 +8,27 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetConsumerInfo ...
|
// GetConsumerInfo ...
|
||||||
func (js JetStream) GetConsumerInfo(stream, name string) (*nats.ConsumerInfo, error) {
|
func (js JetStream) GetConsumerInfo(name string) (*nats.ConsumerInfo, error) {
|
||||||
return js.instance.ConsumerInfo(stream, name)
|
return js.instance.ConsumerInfo(js.streamName, name)
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddConsumer ...
|
// AddConsumer ...
|
||||||
func (js JetStream) AddConsumer(stream, name, filterSubject string) error {
|
func (js JetStream) AddConsumer(name, filterSubject string) error {
|
||||||
// Get consumer first, return if existed
|
// Get consumer first, return if existed
|
||||||
consumer, _ := js.GetConsumerInfo(stream, name)
|
consumer, _ := js.GetConsumerInfo(name)
|
||||||
if consumer != nil {
|
if consumer != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add
|
// Add
|
||||||
_, err := js.instance.AddConsumer(stream, &nats.ConsumerConfig{
|
_, err := js.instance.AddConsumer(js.streamName, &nats.ConsumerConfig{
|
||||||
Durable: name,
|
Durable: name,
|
||||||
AckPolicy: nats.AckExplicitPolicy,
|
AckPolicy: nats.AckExplicitPolicy,
|
||||||
FilterSubject: filterSubject,
|
FilterSubject: filterSubject,
|
||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
msg := fmt.Sprintf("[NATS JETSTREAM] - add consumer %s for stream #%s error: %s", name, stream, err.Error())
|
msg := fmt.Sprintf("[NATS JETSTREAM] - add consumer %s for stream #%s error: %s", name, js.streamName, err.Error())
|
||||||
return errors.New(msg)
|
return errors.New(msg)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -8,8 +8,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// Publish ...
|
// Publish ...
|
||||||
func (js JetStream) Publish(stream, subject string, payload []byte) error {
|
func (js JetStream) Publish(subject string, payload []byte) error {
|
||||||
channel := combineStreamAndSubjectName(stream, subject)
|
channel := combineStreamAndSubjectName(js.streamName, subject)
|
||||||
|
|
||||||
_, err := js.instance.PublishAsync(channel, payload)
|
_, err := js.instance.PublishAsync(channel, payload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -20,8 +20,8 @@ func (js JetStream) Publish(stream, subject string, payload []byte) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Subscribe ...
|
// Subscribe ...
|
||||||
func (js JetStream) Subscribe(stream, subject string, cb nats.MsgHandler) (*nats.Subscription, error) {
|
func (js JetStream) Subscribe(subject string, cb nats.MsgHandler) (*nats.Subscription, error) {
|
||||||
channel := combineStreamAndSubjectName(stream, subject)
|
channel := combineStreamAndSubjectName(js.streamName, subject)
|
||||||
|
|
||||||
sub, err := js.instance.Subscribe(channel, cb)
|
sub, err := js.instance.Subscribe(channel, cb)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -43,14 +43,13 @@ func (js JetStream) Subscribe(stream, subject string, cb nats.MsgHandler) (*nats
|
||||||
// messages, err := sub.Fetch(10)
|
// messages, err := sub.Fetch(10)
|
||||||
// // process each messages
|
// // process each messages
|
||||||
// }
|
// }
|
||||||
//
|
func (js JetStream) PullSubscribe(subject, consumer string) (*nats.Subscription, error) {
|
||||||
func (js JetStream) PullSubscribe(stream, subject, consumer string) (*nats.Subscription, error) {
|
channel := combineStreamAndSubjectName(js.streamName, subject)
|
||||||
channel := combineStreamAndSubjectName(stream, subject)
|
|
||||||
|
|
||||||
// Check if consumer existed
|
// Check if consumer existed
|
||||||
con, err := js.GetConsumerInfo(stream, consumer)
|
con, err := js.GetConsumerInfo(consumer)
|
||||||
if con == nil || err != nil {
|
if con == nil || err != nil {
|
||||||
msg := fmt.Sprintf("[NATS JETSTREAM] - pull subscribe consumer %s not existed in stream %s", consumer, stream)
|
msg := fmt.Sprintf("[NATS JETSTREAM] - pull subscribe consumer %s not existed in stream %s", consumer, js.streamName)
|
||||||
return nil, errors.New(msg)
|
return nil, errors.New(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,8 +8,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// QueueSubscribe ...
|
// QueueSubscribe ...
|
||||||
func (js JetStream) QueueSubscribe(stream, subject, queueName string, cb nats.MsgHandler) error {
|
func (js JetStream) QueueSubscribe(subject, queueName string, cb nats.MsgHandler) error {
|
||||||
channel := combineStreamAndSubjectName(stream, subject)
|
channel := combineStreamAndSubjectName(js.streamName, subject)
|
||||||
|
|
||||||
_, err := js.instance.QueueSubscribe(channel, queueName, cb)
|
_, err := js.instance.QueueSubscribe(channel, queueName, cb)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -21,21 +21,21 @@ func generateStreamConfig(stream string, subjects []string) *nats.StreamConfig {
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetStreamInfo ...
|
// GetStreamInfo ...
|
||||||
func (js JetStream) GetStreamInfo(name string) (*nats.StreamInfo, error) {
|
func (js JetStream) GetStreamInfo() (*nats.StreamInfo, error) {
|
||||||
return js.instance.StreamInfo(name)
|
return js.instance.StreamInfo(js.streamName)
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddStream add new stream, with default config
|
// AddStream add new stream, with default config
|
||||||
// Due to subject must have a unique name, subject name will be combined with stream name
|
// Due to subject must have a unique name, subject name will be combined with stream name
|
||||||
// E.g: stream name is "DEMO", subject name is "Subject-1", so final name in NATS will be: DEMO.Subject-1
|
// E.g: stream name is "DEMO", subject name is "Subject-1", so final name in NATS will be: DEMO.Subject-1
|
||||||
func (js JetStream) AddStream(name string, subjects []string) error {
|
func (js JetStream) AddStream(subjects []string) error {
|
||||||
// Get info about the stream
|
// Get info about the stream
|
||||||
stream, _ := js.GetStreamInfo(name)
|
stream, _ := js.GetStreamInfo()
|
||||||
|
|
||||||
// If stream not found, create new
|
// If stream not found, create new
|
||||||
if stream == nil {
|
if stream == nil {
|
||||||
subjectNames := generateSubjectNames(name, subjects)
|
subjectNames := generateSubjectNames(js.streamName, subjects)
|
||||||
_, err := js.instance.AddStream(generateStreamConfig(name, subjectNames))
|
_, err := js.instance.AddStream(generateStreamConfig(js.streamName, subjectNames))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
msg := fmt.Sprintf("[NATS JETSTREAM] - add stream error: %s", err.Error())
|
msg := fmt.Sprintf("[NATS JETSTREAM] - add stream error: %s", err.Error())
|
||||||
return errors.New(msg)
|
return errors.New(msg)
|
||||||
|
@ -46,8 +46,8 @@ func (js JetStream) AddStream(name string, subjects []string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteStream ...
|
// DeleteStream ...
|
||||||
func (js JetStream) DeleteStream(name string) error {
|
func (js JetStream) DeleteStream() error {
|
||||||
if err := js.instance.DeleteStream(name); err != nil {
|
if err := js.instance.DeleteStream(js.streamName); err != nil {
|
||||||
msg := fmt.Sprintf("[NATS JETSTREAM] - delete stream error: %s", err.Error())
|
msg := fmt.Sprintf("[NATS JETSTREAM] - delete stream error: %s", err.Error())
|
||||||
return errors.New(msg)
|
return errors.New(msg)
|
||||||
}
|
}
|
||||||
|
@ -55,19 +55,19 @@ func (js JetStream) DeleteStream(name string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddStreamSubjects ...
|
// AddStreamSubjects ...
|
||||||
func (js JetStream) AddStreamSubjects(name string, subjects []string) error {
|
func (js JetStream) AddStreamSubjects(subjects []string) error {
|
||||||
// Get info about the stream
|
// Get info about the stream
|
||||||
stream, _ := js.GetStreamInfo(name)
|
stream, _ := js.GetStreamInfo()
|
||||||
if stream == nil {
|
if stream == nil {
|
||||||
msg := fmt.Sprintf("[NATS JETSTREAM] - error when adding stream %s subjects: stream not found", name)
|
msg := fmt.Sprintf("[NATS JETSTREAM] - error when adding stream %s subjects: stream not found", js.streamName)
|
||||||
return errors.New(msg)
|
return errors.New(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Merge current and new subjects
|
// Merge current and new subjects
|
||||||
subjectNames := generateSubjectNames(name, subjects)
|
subjectNames := generateSubjectNames(js.streamName, subjects)
|
||||||
newSubjects := mergeAndUniqueArrayStrings(subjectNames, stream.Config.Subjects)
|
newSubjects := mergeAndUniqueArrayStrings(subjectNames, stream.Config.Subjects)
|
||||||
|
|
||||||
_, err := js.instance.UpdateStream(generateStreamConfig(name, newSubjects))
|
_, err := js.instance.UpdateStream(generateStreamConfig(js.streamName, newSubjects))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
msg := fmt.Sprintf("[NATS JETSTREAM] - add stream error: %s", err.Error())
|
msg := fmt.Sprintf("[NATS JETSTREAM] - add stream error: %s", err.Error())
|
||||||
return errors.New(msg)
|
return errors.New(msg)
|
||||||
|
|
|
@ -3,6 +3,7 @@ package natsio
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/nats-io/nats.go"
|
"github.com/nats-io/nats.go"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -15,6 +16,7 @@ type Server struct {
|
||||||
// JetStream ...
|
// JetStream ...
|
||||||
type JetStream struct {
|
type JetStream struct {
|
||||||
instance nats.JetStreamContext
|
instance nats.JetStreamContext
|
||||||
|
streamName string
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -61,6 +63,7 @@ func Connect(cfg Config) error {
|
||||||
return errors.New(msg)
|
return errors.New(msg)
|
||||||
}
|
}
|
||||||
natsJetStream.instance = js
|
natsJetStream.instance = js
|
||||||
|
natsJetStream.streamName = cfg.StreamName
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue