mirror of
https://github.com/mainflux/mainflux.git
synced 2025-04-28 13:48:49 +08:00

* things, twins, and logger lint fixed Signed-off-by: aryan <aryangodara03@gmail.com> * all services updated, auth jwt not working, ineffectual assignment issue Signed-off-by: aryan <aryangodara03@gmail.com> * handle error from grpc server in endpointtest Signed-off-by: aryan <aryangodara03@gmail.com> * temp commit, auth/jwt needs to be resolved Signed-off-by: aryan <aryangodara03@gmail.com> * revert back to jwt v4 temporarily Signed-off-by: aryan <aryangodara03@gmail.com> * updated jwt tokenizer Signed-off-by: aryan <aryangodara03@gmail.com> * resolve EOF error for httptest requests Signed-off-by: aryan <aryangodara03@gmail.com> * fix auth jwt, update to registeredclaims Signed-off-by: aryan <aryangodara03@gmail.com> * fix ineffective assignment, auth/api/grpc endpoint failing Signed-off-by: aryan <aryangodara03@gmail.com> * temp commit, remove later Signed-off-by: aryan <aryangodara03@gmail.com> * fix grpc server setup Signed-off-by: aryan <aryangodara03@gmail.com> * resolve golangci tests, remove debug statements Signed-off-by: aryan <aryangodara03@gmail.com> * update golangci version and modify linters used Signed-off-by: aryan <aryangodara03@gmail.com> * fix failing tests Signed-off-by: aryan <aryangodara03@gmail.com> * fix grpc server for setup tests Signed-off-by: aryan <aryangodara03@gmail.com> * fix logging and errors inlined Signed-off-by: aryan <aryangodara03@gmail.com> * fix remarks, update grpc setup_test Signed-off-by: aryan <aryangodara03@gmail.com> * fix setup_test Signed-off-by: aryan <aryangodara03@gmail.com> * update setup_test grpc Signed-off-by: aryan <aryangodara03@gmail.com> * fix data race Signed-off-by: aryan <aryangodara03@gmail.com> * update setup_test grpc Signed-off-by: aryan <aryangodara03@gmail.com> * fix grpc setup down to single simple function Signed-off-by: aryan <aryangodara03@gmail.com> * fix linting issues Signed-off-by: aryan <aryangodara03@gmail.com> * resolve pr comments Signed-off-by: aryan <aryangodara03@gmail.com> * fix tests, handle returned errors, go mod tidy vendor Signed-off-by: aryan <aryangodara03@gmail.com> * fix errors from new linters Signed-off-by: aryan <aryangodara03@gmail.com> --------- Signed-off-by: aryan <aryangodara03@gmail.com>
63 lines
1.4 KiB
Go
63 lines
1.4 KiB
Go
// Copyright (c) Mainflux
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package nats
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/mainflux/mainflux/pkg/messaging"
|
|
broker "github.com/nats-io/nats.go"
|
|
"google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
// 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(ctx context.Context, 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)
|
|
}
|
|
|
|
return pub.conn.Publish(subject, data)
|
|
}
|
|
|
|
func (pub *publisher) Close() error {
|
|
pub.conn.Close()
|
|
return nil
|
|
}
|