2020-04-28 11:02:35 +02:00
|
|
|
// Copyright (c) Mainflux
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package nats
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/gogo/protobuf/proto"
|
|
|
|
|
|
|
|
log "github.com/mainflux/mainflux/logger"
|
2020-06-03 15:16:19 +02:00
|
|
|
"github.com/mainflux/mainflux/pkg/messaging"
|
2020-04-28 11:02:35 +02:00
|
|
|
broker "github.com/nats-io/nats.go"
|
|
|
|
)
|
|
|
|
|
|
|
|
const chansPrefix = "channels"
|
|
|
|
|
|
|
|
// SubjectAllChannels represents subject to subscribe for all the channels.
|
|
|
|
const SubjectAllChannels = "channels.>"
|
|
|
|
|
|
|
|
var (
|
|
|
|
errAlreadySubscribed = errors.New("already subscribed to topic")
|
|
|
|
errNotSubscribed = errors.New("not subscribed")
|
|
|
|
errEmptyTopic = errors.New("empty topic")
|
2022-05-03 19:02:19 +03:00
|
|
|
errEmptyID = errors.New("empty ID")
|
2020-04-28 11:02:35 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ messaging.PubSub = (*pubsub)(nil)
|
|
|
|
|
|
|
|
// PubSub wraps messaging Publisher exposing
|
|
|
|
// Close() method for NATS connection.
|
|
|
|
type PubSub interface {
|
|
|
|
messaging.PubSub
|
|
|
|
Close()
|
|
|
|
}
|
|
|
|
|
2022-05-03 19:02:19 +03:00
|
|
|
type subscription struct {
|
|
|
|
*broker.Subscription
|
|
|
|
cancel func() error
|
|
|
|
}
|
|
|
|
|
2020-04-28 11:02:35 +02:00
|
|
|
type pubsub struct {
|
|
|
|
conn *broker.Conn
|
|
|
|
logger log.Logger
|
|
|
|
mu sync.Mutex
|
|
|
|
queue string
|
2022-05-03 19:02:19 +03:00
|
|
|
subscriptions map[string]map[string]subscription
|
2020-04-28 11:02:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewPubSub returns NATS message publisher/subscriber.
|
|
|
|
// Parameter queue specifies the queue for the Subscribe method.
|
|
|
|
// If queue is specified (is not an empty string), Subscribe method
|
|
|
|
// will execute NATS QueueSubscribe which is conceptually different
|
|
|
|
// from ordinary subscribe. For more information, please take a look
|
|
|
|
// here: https://docs.nats.io/developing-with-nats/receiving/queues.
|
|
|
|
// If the queue is empty, Subscribe will be used.
|
|
|
|
func NewPubSub(url, queue string, logger log.Logger) (PubSub, error) {
|
|
|
|
conn, err := broker.Connect(url)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ret := &pubsub{
|
|
|
|
conn: conn,
|
|
|
|
queue: queue,
|
|
|
|
logger: logger,
|
2022-05-03 19:02:19 +03:00
|
|
|
subscriptions: make(map[string]map[string]subscription),
|
2020-04-28 11:02:35 +02:00
|
|
|
}
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ps *pubsub) Publish(topic string, msg messaging.Message) error {
|
|
|
|
data, err := proto.Marshal(&msg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
subject := fmt.Sprintf("%s.%s", chansPrefix, topic)
|
|
|
|
if msg.Subtopic != "" {
|
|
|
|
subject = fmt.Sprintf("%s.%s", subject, msg.Subtopic)
|
|
|
|
}
|
|
|
|
if err := ps.conn.Publish(subject, data); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-03 19:02:19 +03:00
|
|
|
func (ps *pubsub) Subscribe(id, topic string, handler messaging.MessageHandler) error {
|
|
|
|
if id == "" {
|
|
|
|
return errEmptyID
|
|
|
|
}
|
2020-04-28 11:02:35 +02:00
|
|
|
if topic == "" {
|
|
|
|
return errEmptyTopic
|
|
|
|
}
|
|
|
|
ps.mu.Lock()
|
|
|
|
defer ps.mu.Unlock()
|
2022-05-03 19:02:19 +03:00
|
|
|
// Check topic
|
|
|
|
s, ok := ps.subscriptions[topic]
|
|
|
|
switch ok {
|
|
|
|
case true:
|
|
|
|
// Check topic ID
|
|
|
|
if _, ok := s[id]; ok {
|
|
|
|
return errAlreadySubscribed
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
s = make(map[string]subscription)
|
|
|
|
ps.subscriptions[topic] = s
|
2020-04-28 11:02:35 +02:00
|
|
|
}
|
|
|
|
nh := ps.natsHandler(handler)
|
2020-05-04 13:14:06 +02:00
|
|
|
|
2020-04-28 11:02:35 +02:00
|
|
|
if ps.queue != "" {
|
|
|
|
sub, err := ps.conn.QueueSubscribe(topic, ps.queue, nh)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-05-03 19:02:19 +03:00
|
|
|
s[id] = subscription{
|
|
|
|
Subscription: sub,
|
|
|
|
cancel: handler.Cancel,
|
|
|
|
}
|
2020-04-28 11:02:35 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
sub, err := ps.conn.Subscribe(topic, nh)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-05-03 19:02:19 +03:00
|
|
|
s[id] = subscription{
|
|
|
|
Subscription: sub,
|
|
|
|
cancel: handler.Cancel,
|
|
|
|
}
|
2020-04-28 11:02:35 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-03 19:02:19 +03:00
|
|
|
func (ps *pubsub) Unsubscribe(id, topic string) error {
|
|
|
|
if id == "" {
|
|
|
|
return errEmptyID
|
|
|
|
}
|
2020-04-28 11:02:35 +02:00
|
|
|
if topic == "" {
|
|
|
|
return errEmptyTopic
|
|
|
|
}
|
|
|
|
ps.mu.Lock()
|
|
|
|
defer ps.mu.Unlock()
|
2022-05-03 19:02:19 +03:00
|
|
|
// Check topic
|
|
|
|
s, ok := ps.subscriptions[topic]
|
2020-04-28 11:02:35 +02:00
|
|
|
if !ok {
|
|
|
|
return errNotSubscribed
|
|
|
|
}
|
2022-05-03 19:02:19 +03:00
|
|
|
// Check topic ID
|
|
|
|
current, ok := s[id]
|
|
|
|
if !ok {
|
|
|
|
return errNotSubscribed
|
|
|
|
}
|
|
|
|
if current.cancel != nil {
|
|
|
|
if err := current.cancel(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := current.Unsubscribe(); err != nil {
|
2020-04-28 11:02:35 +02:00
|
|
|
return err
|
|
|
|
}
|
2022-05-03 19:02:19 +03:00
|
|
|
delete(s, id)
|
|
|
|
if len(s) == 0 {
|
|
|
|
delete(ps.subscriptions, topic)
|
|
|
|
}
|
2020-04-28 11:02:35 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ps *pubsub) Close() {
|
|
|
|
ps.conn.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ps *pubsub) natsHandler(h messaging.MessageHandler) broker.MsgHandler {
|
|
|
|
return func(m *broker.Msg) {
|
|
|
|
var msg messaging.Message
|
|
|
|
if err := proto.Unmarshal(m.Data, &msg); err != nil {
|
|
|
|
ps.logger.Warn(fmt.Sprintf("Failed to unmarshal received message: %s", err))
|
|
|
|
return
|
|
|
|
}
|
2022-05-03 19:02:19 +03:00
|
|
|
if err := h.Handle(msg); err != nil {
|
2020-04-28 11:02:35 +02:00
|
|
|
ps.logger.Warn(fmt.Sprintf("Failed to handle Mainflux message: %s", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|