2022-02-04 10:05:36 +00:00
|
|
|
package notifications
|
|
|
|
|
|
|
|
import (
|
2022-03-05 06:58:54 +00:00
|
|
|
"context"
|
2022-03-31 06:10:27 +00:00
|
|
|
"encoding/json"
|
2022-03-05 06:58:54 +00:00
|
|
|
"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
|
|
|
|
)
|
|
|
|
|
2022-03-31 06:10:27 +00:00
|
|
|
type (
|
|
|
|
Options struct {
|
|
|
|
URL string
|
|
|
|
TLSCertFilepath string
|
|
|
|
TLSAuthPrivateKeyFilePath string
|
|
|
|
Timeout time.Duration
|
|
|
|
RootCAFiles []string
|
|
|
|
}
|
2022-02-04 10:05:36 +00:00
|
|
|
|
2022-03-31 06:10:27 +00:00
|
|
|
Controller struct {
|
|
|
|
logger *zap.Logger
|
|
|
|
taskQueueConnection *nats.Conn
|
|
|
|
jsClient nats.JetStreamContext
|
|
|
|
handlers map[string]Stream
|
|
|
|
mu sync.RWMutex
|
|
|
|
}
|
2022-03-05 06:58:54 +00:00
|
|
|
|
2022-03-31 06:10:27 +00:00
|
|
|
Stream struct {
|
|
|
|
h layer.MsgHandler
|
|
|
|
ch chan *nats.Msg
|
|
|
|
}
|
|
|
|
|
|
|
|
TestEvent struct {
|
|
|
|
Service string
|
|
|
|
Event string
|
|
|
|
Time time.Time
|
|
|
|
Bucket string
|
|
|
|
RequestID string
|
|
|
|
HostID string
|
|
|
|
}
|
|
|
|
)
|
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)
|
|
|
|
}
|
|
|
|
}
|
2022-03-31 06:10:27 +00:00
|
|
|
|
|
|
|
func (c *Controller) SendTestNotification(topic, bucketName, requestID, HostID string) error {
|
|
|
|
event := &TestEvent{
|
|
|
|
Service: "NeoFS S3",
|
|
|
|
Event: "s3:TestEvent",
|
|
|
|
Time: time.Now(),
|
|
|
|
Bucket: bucketName,
|
|
|
|
RequestID: requestID,
|
|
|
|
HostID: HostID,
|
|
|
|
}
|
|
|
|
|
|
|
|
msg, err := json.Marshal(event)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("couldn't marshal test event: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.publish(topic, msg)
|
|
|
|
}
|