mirror of
https://github.com/mainflux/mainflux.git
synced 2025-05-01 13:48:56 +08:00

* Make NATS reconnect never give up Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Extract maxReconnects to a constant Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
64 lines
1.4 KiB
Go
64 lines
1.4 KiB
Go
// Copyright (c) Mainflux
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package nats
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/gogo/protobuf/proto"
|
|
"github.com/mainflux/mainflux/pkg/messaging"
|
|
broker "github.com/nats-io/nats.go"
|
|
)
|
|
|
|
// A maximum number of reconnect attempts before NATS connection closes permanently.
|
|
// Value -1 represents an unlimited number of reconnect retries, i.e. the client
|
|
// will never give up on retrying to re-establish connection to NATS server.
|
|
const maxReconnects = -1
|
|
|
|
var _ messaging.Publisher = (*publisher)(nil)
|
|
|
|
type publisher struct {
|
|
conn *broker.Conn
|
|
}
|
|
|
|
// Publisher wraps messaging Publisher exposing
|
|
// Close() method for NATS connection.
|
|
|
|
// NewPublisher returns NATS message Publisher.
|
|
func NewPublisher(url string) (messaging.Publisher, error) {
|
|
conn, err := broker.Connect(url, broker.MaxReconnects(maxReconnects))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
ret := &publisher{
|
|
conn: conn,
|
|
}
|
|
return ret, nil
|
|
}
|
|
|
|
func (pub *publisher) Publish(topic string, msg messaging.Message) error {
|
|
if topic == "" {
|
|
return ErrEmptyTopic
|
|
}
|
|
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 := pub.conn.Publish(subject, data); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (pub *publisher) Close() error {
|
|
pub.conn.Close()
|
|
return nil
|
|
}
|