2022-02-04 10:05:36 +00:00
|
|
|
package notifications
|
|
|
|
|
|
|
|
import (
|
2022-03-05 06:58:54 +00:00
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
2022-02-04 10:05:36 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/nats-io/nats.go"
|
2022-03-05 06:58:54 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api/layer"
|
2022-03-23 07:36:25 +00:00
|
|
|
"go.uber.org/zap"
|
2022-02-04 10:05:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
DefaultTimeout = 30 * time.Second
|
|
|
|
)
|
|
|
|
|
|
|
|
type Options struct {
|
|
|
|
URL string
|
|
|
|
TLSCertFilepath string
|
|
|
|
TLSAuthPrivateKeyFilePath string
|
|
|
|
Timeout time.Duration
|
|
|
|
RootCAFiles []string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Controller struct {
|
2022-03-23 07:36:25 +00:00
|
|
|
logger *zap.Logger
|
2022-02-04 10:05:36 +00:00
|
|
|
taskQueueConnection *nats.Conn
|
2022-03-05 06:58:54 +00:00
|
|
|
jsClient nats.JetStreamContext
|
|
|
|
handlers map[string]Stream
|
|
|
|
mu sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
type Stream struct {
|
|
|
|
h layer.MsgHandler
|
|
|
|
ch chan *nats.Msg
|
2022-02-04 10:05:36 +00:00
|
|
|
}
|
|
|
|
|
2022-03-23 07:36:25 +00:00
|
|
|
func NewController(p *Options, l *zap.Logger) (*Controller, error) {
|
2022-02-04 10:05:36 +00:00
|
|
|
ncopts := []nats.Option{
|
|
|
|
nats.Timeout(p.Timeout),
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:36:35 +00:00
|
|
|
if len(p.TLSCertFilepath) != 0 && len(p.TLSAuthPrivateKeyFilePath) != 0 {
|
|
|
|
ncopts = append(ncopts, nats.ClientCert(p.TLSCertFilepath, p.TLSAuthPrivateKeyFilePath))
|
|
|
|
}
|
|
|
|
if len(p.RootCAFiles) != 0 {
|
|
|
|
ncopts = append(ncopts, nats.RootCAs(p.RootCAFiles...))
|
|
|
|
}
|
|
|
|
|
2022-02-04 10:05:36 +00:00
|
|
|
nc, err := nats.Connect(p.URL, ncopts...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
js, err := nc.JetStream()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Controller{
|
2022-03-23 07:36:25 +00:00
|
|
|
logger: l,
|
2022-02-04 10:05:36 +00:00
|
|
|
taskQueueConnection: nc,
|
|
|
|
jsClient: js,
|
2022-03-05 06:58:54 +00:00
|
|
|
handlers: make(map[string]Stream),
|
2022-02-04 10:05:36 +00:00
|
|
|
}, nil
|
|
|
|
}
|
2022-03-05 06:58:54 +00:00
|
|
|
|
|
|
|
func (c *Controller) Subscribe(ctx context.Context, topic string, handler layer.MsgHandler) error {
|
|
|
|
ch := make(chan *nats.Msg, 1)
|
|
|
|
|
|
|
|
c.mu.RLock()
|
|
|
|
if _, ok := c.handlers[topic]; ok {
|
|
|
|
return fmt.Errorf("already subscribed to topic '%s'", topic)
|
|
|
|
}
|
|
|
|
c.mu.RUnlock()
|
|
|
|
|
|
|
|
if _, err := c.jsClient.AddStream(&nats.StreamConfig{Name: topic}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := c.jsClient.ChanSubscribe(topic, ch); err != nil {
|
|
|
|
return fmt.Errorf("could not subscribe: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.mu.Lock()
|
|
|
|
c.handlers[topic] = Stream{
|
|
|
|
h: handler,
|
|
|
|
ch: ch,
|
|
|
|
}
|
|
|
|
c.mu.Unlock()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Controller) Listen(ctx context.Context) {
|
|
|
|
c.mu.RLock()
|
|
|
|
defer c.mu.RUnlock()
|
|
|
|
|
|
|
|
for _, stream := range c.handlers {
|
|
|
|
go func(stream Stream) {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case msg := <-stream.ch:
|
|
|
|
if err := stream.h.HandleMessage(ctx, msg); err != nil {
|
2022-03-23 07:36:25 +00:00
|
|
|
c.logger.Error("could not handle message", zap.Error(err))
|
2022-03-05 06:58:54 +00:00
|
|
|
} else if err = msg.Ack(); err != nil {
|
2022-03-23 07:36:25 +00:00
|
|
|
c.logger.Error("could not ACK message", zap.Error(err))
|
2022-03-05 06:58:54 +00:00
|
|
|
}
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}(stream)
|
|
|
|
}
|
|
|
|
}
|