1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-05-02 22:17:10 +08:00
Mainflux.mainflux/ws/nats/publisher.go
Aleksandar Novaković 1d75268ffa NOISSUE - Fix channel closing bug in WebSocket adapter (#309)
* Remove unnecessary error checks in ws adapter

Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>

* Fix WebSocket adapter channel closing bug

Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
2018-05-28 12:47:20 +02:00

74 lines
1.6 KiB
Go

// Package nats contains NATS message publisher implementation.
package nats
import (
"fmt"
"github.com/sony/gobreaker"
"github.com/gogo/protobuf/proto"
"github.com/mainflux/mainflux"
"github.com/mainflux/mainflux/ws"
broker "github.com/nats-io/go-nats"
)
const (
prefix = "channel"
maxFailedReqs = 3
maxFailureRatio = 0.6
)
var _ ws.Service = (*natsPubSub)(nil)
type natsPubSub struct {
nc *broker.Conn
cb *gobreaker.CircuitBreaker
}
// New instantiates NATS message publisher.
func New(nc *broker.Conn) 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, cb}
}
func (pubsub *natsPubSub) Publish(msg mainflux.RawMessage) error {
data, err := proto.Marshal(&msg)
if err != nil {
return err
}
return pubsub.nc.Publish(fmt.Sprintf("%s.%d", prefix, msg.Channel), data)
}
func (pubsub *natsPubSub) Subscribe(chanID uint64, channel *ws.Channel) error {
var sub *broker.Subscription
sub, err := pubsub.nc.Subscribe(fmt.Sprintf("%s.%d", prefix, chanID), func(msg *broker.Msg) {
if msg == nil {
return
}
var rawMsg mainflux.RawMessage
if err := proto.Unmarshal(msg.Data, &rawMsg); err != nil {
return
}
// Sends message to messages channel
channel.Send(rawMsg)
})
// Check if subscription should be closed
go func() {
<-channel.Closed
sub.Unsubscribe()
}()
return err
}