mirror of
https://github.com/mainflux/mainflux.git
synced 2025-05-09 19:29:29 +08:00

* NOISSUE - Add mProxy support (#1017) * Add mproxy Signed-off-by: Drasko DRASKOVIC <drasko.draskovic@gmail.com> * Fix docker and add EMQ compose Signed-off-by: Drasko DRASKOVIC <drasko.draskovic@gmail.com> * Fix EMQX name Signed-off-by: Drasko DRASKOVIC <drasko.draskovic@gmail.com> * Add nats, auth and es Signed-off-by: Drasko DRASKOVIC <drasko.draskovic@gmail.com> * Removed unucessary vendoring Signed-off-by: Drasko Draskovic <drasko.draskovic@gmail.com> * Update vendoring Signed-off-by: Drasko Draskovic <drasko.draskovic@gmail.com> * Fix mproxy interface implementation Signed-off-by: Drasko Draskovic <drasko.draskovic@gmail.com> NOISSUE - Aligned Event interface method signatures with new spec (#1025) * Aligned Event interface method signatures with new spec Signed-off-by: Nikola Marcetic <n.marcetic86@gmail.com> * Updated deps Signed-off-by: Nikola Marcetic <n.marcetic86@gmail.com> NOISSUE - Update mproxy dependency (#1038) Signed-off-by: Nikola Marcetic <n.marcetic86@gmail.com> Update Vendor with new mProxy (#1043) Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> Twins merge conflict reverted Signed-off-by: Nikola Marcetic <n.marcetic86@gmail.com> Twins merge conflict reverted Signed-off-by: Nikola Marcetic <n.marcetic86@gmail.com> Twins fixed nats import Signed-off-by: Nikola Marcetic <n.marcetic86@gmail.com> Update deps Signed-off-by: Nikola Marcetic <n.marcetic86@gmail.com> * Resolved GolangCI remarks Signed-off-by: Nikola Marcetic <n.marcetic86@gmail.com> Resolved GolangCI remarks Signed-off-by: Nikola Marcetic <n.marcetic86@gmail.com> Resolved GolangCI remarks Signed-off-by: Nikola Marcetic <n.marcetic86@gmail.com> * Fixed Event interface Unsubscribe() typo Signed-off-by: Nikola Marcetic <n.marcetic86@gmail.com> * Update vendors Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Upgrade CI script Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> Co-authored-by: Drasko DRASKOVIC <drasko.draskovic@gmail.com> Co-authored-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com>
98 lines
2.3 KiB
Go
98 lines
2.3 KiB
Go
// Copyright (c) Mainflux
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
// Package nats contains NATS message publisher implementation.
|
|
package nats
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/sony/gobreaker"
|
|
|
|
"github.com/gogo/protobuf/proto"
|
|
"github.com/mainflux/mainflux"
|
|
log "github.com/mainflux/mainflux/logger"
|
|
"github.com/mainflux/mainflux/ws"
|
|
broker "github.com/nats-io/nats.go"
|
|
)
|
|
|
|
const (
|
|
prefix = "channel"
|
|
maxFailedReqs = 3
|
|
maxFailureRatio = 0.6
|
|
)
|
|
|
|
var _ ws.Service = (*natsPubSub)(nil)
|
|
|
|
type natsPubSub struct {
|
|
nc *broker.Conn
|
|
cb *gobreaker.CircuitBreaker
|
|
logger log.Logger
|
|
}
|
|
|
|
// New instantiates NATS message publisher.
|
|
func New(nc *broker.Conn, logger log.Logger) ws.Service {
|
|
st := gobreaker.Settings{
|
|
Name: "NATS",
|
|
ReadyToTrip: func(counts gobreaker.Counts) bool {
|
|
fr := float64(counts.TotalFailures) / float64(counts.Requests)
|
|
return counts.Requests >= maxFailedReqs && fr >= maxFailureRatio
|
|
},
|
|
}
|
|
cb := gobreaker.NewCircuitBreaker(st)
|
|
return &natsPubSub{
|
|
nc: nc,
|
|
cb: cb,
|
|
logger: logger,
|
|
}
|
|
}
|
|
|
|
func (pubsub *natsPubSub) fmtSubject(chanID, subtopic string) string {
|
|
subject := fmt.Sprintf("%s.%s", prefix, chanID)
|
|
if subtopic != "" {
|
|
subject = fmt.Sprintf("%s.%s", subject, subtopic)
|
|
}
|
|
return subject
|
|
}
|
|
|
|
func (pubsub *natsPubSub) Publish(_ context.Context, _ string, msg mainflux.Message) error {
|
|
data, err := proto.Marshal(&msg)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
subject := pubsub.fmtSubject(msg.Channel, msg.Subtopic)
|
|
return pubsub.nc.Publish(subject, data)
|
|
}
|
|
|
|
func (pubsub *natsPubSub) Subscribe(chanID, subtopic string, channel *ws.Channel) error {
|
|
var sub *broker.Subscription
|
|
|
|
sub, err := pubsub.nc.Subscribe(pubsub.fmtSubject(chanID, subtopic), func(msg *broker.Msg) {
|
|
if msg == nil {
|
|
pubsub.logger.Warn("Received nil message")
|
|
return
|
|
}
|
|
|
|
var m mainflux.Message
|
|
if err := proto.Unmarshal(msg.Data, &m); err != nil {
|
|
pubsub.logger.Warn(fmt.Sprintf("Failed to deserialize received message: %s", err.Error()))
|
|
return
|
|
}
|
|
|
|
pubsub.logger.Debug(fmt.Sprintf("Successfully received message from NATS from channel %s", m.GetChannel()))
|
|
|
|
// Sends message to messages channel
|
|
channel.Send(m)
|
|
})
|
|
|
|
// Check if subscription should be closed
|
|
go func() {
|
|
<-channel.Closed
|
|
sub.Unsubscribe()
|
|
}()
|
|
|
|
return err
|
|
}
|