2021-03-01 11:51:38 +00:00
|
|
|
package nats
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
|
2023-04-12 14:35:10 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/logger"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
2021-03-01 11:51:38 +00:00
|
|
|
"github.com/nats-io/nats.go"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Writer is a NATS object notification writer.
|
|
|
|
// It handles NATS JetStream connections and allows
|
|
|
|
// sending string representation of the address to
|
|
|
|
// the NATS server.
|
|
|
|
//
|
|
|
|
// For correct operation must be created via New function.
|
|
|
|
// new(Writer) or Writer{} construction leads to undefined
|
|
|
|
// behaviour and is not safe.
|
|
|
|
type Writer struct {
|
2022-03-18 15:13:12 +00:00
|
|
|
js nats.JetStreamContext
|
|
|
|
nc *nats.Conn
|
2021-03-01 11:51:38 +00:00
|
|
|
|
2023-05-30 07:14:37 +00:00
|
|
|
m sync.RWMutex
|
2021-03-01 11:51:38 +00:00
|
|
|
createdStreams map[string]struct{}
|
2022-03-18 15:13:12 +00:00
|
|
|
opts
|
2021-03-01 11:51:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type opts struct {
|
2022-09-28 07:41:01 +00:00
|
|
|
log *logger.Logger
|
2022-03-18 15:13:12 +00:00
|
|
|
nOpts []nats.Option
|
2021-03-01 11:51:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Option func(*opts)
|
|
|
|
|
|
|
|
var errConnIsClosed = errors.New("connection to the server is closed")
|
|
|
|
|
|
|
|
// Notify sends object address's string representation to the provided topic.
|
2022-03-15 17:49:42 +00:00
|
|
|
// Uses first 4 bytes of object ID as a message ID to support 'exactly once'
|
|
|
|
// message delivery.
|
2021-03-01 11:51:38 +00:00
|
|
|
//
|
|
|
|
// Returns error only if:
|
2022-03-15 17:49:42 +00:00
|
|
|
// 1. underlying connection was closed and has not been established again;
|
|
|
|
// 2. NATS server could not respond that it has saved the message.
|
2022-05-31 17:00:41 +00:00
|
|
|
func (n *Writer) Notify(topic string, address oid.Address) error {
|
2021-03-01 11:51:38 +00:00
|
|
|
if !n.nc.IsConnected() {
|
|
|
|
return errConnIsClosed
|
|
|
|
}
|
|
|
|
|
|
|
|
// use first 4 byte of the encoded string as
|
2022-03-15 17:49:42 +00:00
|
|
|
// message ID for the 'exactly once' delivery
|
2022-05-31 17:00:41 +00:00
|
|
|
messageID := address.Object().EncodeToString()[:4]
|
2021-03-01 11:51:38 +00:00
|
|
|
|
|
|
|
// check if the stream was previously created
|
|
|
|
n.m.RLock()
|
|
|
|
_, created := n.createdStreams[topic]
|
|
|
|
n.m.RUnlock()
|
|
|
|
|
|
|
|
if !created {
|
|
|
|
_, err := n.js.AddStream(&nats.StreamConfig{
|
|
|
|
Name: topic,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not add stream: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
n.m.Lock()
|
|
|
|
n.createdStreams[topic] = struct{}{}
|
|
|
|
n.m.Unlock()
|
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
_, err := n.js.Publish(topic, []byte(address.EncodeToString()), nats.MsgId(messageID))
|
2023-08-14 11:06:17 +00:00
|
|
|
return err
|
2021-03-01 11:51:38 +00:00
|
|
|
}
|
|
|
|
|
2022-03-18 15:13:12 +00:00
|
|
|
// New creates new Writer.
|
|
|
|
func New(oo ...Option) *Writer {
|
|
|
|
w := &Writer{
|
|
|
|
createdStreams: make(map[string]struct{}),
|
|
|
|
opts: opts{
|
2022-09-28 07:41:01 +00:00
|
|
|
log: &logger.Logger{Logger: zap.L()},
|
2022-03-18 15:13:12 +00:00
|
|
|
nOpts: make([]nats.Option, 0, len(oo)+3),
|
|
|
|
},
|
2021-03-01 11:51:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range oo {
|
2022-03-18 15:13:12 +00:00
|
|
|
o(&w.opts)
|
2021-03-01 11:51:38 +00:00
|
|
|
}
|
|
|
|
|
2022-03-18 15:13:12 +00:00
|
|
|
w.opts.nOpts = append(w.opts.nOpts,
|
2021-03-01 11:51:38 +00:00
|
|
|
nats.NoCallbacksAfterClientClose(), // do not call callbacks when it was planned writer stop
|
2024-03-11 14:11:49 +00:00
|
|
|
nats.DisconnectErrHandler(func(_ *nats.Conn, err error) {
|
2023-04-12 14:35:10 +00:00
|
|
|
w.log.Error(logs.NatsNatsConnectionWasLost, zap.Error(err))
|
2021-03-01 11:51:38 +00:00
|
|
|
}),
|
2024-03-11 14:11:49 +00:00
|
|
|
nats.ReconnectHandler(func(_ *nats.Conn) {
|
2023-04-12 14:35:10 +00:00
|
|
|
w.log.Warn(logs.NatsNatsReconnectedToTheServer)
|
2021-03-01 11:51:38 +00:00
|
|
|
}),
|
|
|
|
)
|
|
|
|
|
2022-03-18 15:13:12 +00:00
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
|
|
|
// Connect tries to connect to a specified NATS endpoint.
|
|
|
|
//
|
|
|
|
// Connection is closed when passed context is done.
|
|
|
|
func (n *Writer) Connect(ctx context.Context, endpoint string) error {
|
|
|
|
nc, err := nats.Connect(endpoint, n.opts.nOpts...)
|
2021-03-01 11:51:38 +00:00
|
|
|
if err != nil {
|
2022-03-18 15:13:12 +00:00
|
|
|
return fmt.Errorf("could not connect to server: %w", err)
|
2021-03-01 11:51:38 +00:00
|
|
|
}
|
|
|
|
|
2022-03-18 15:13:12 +00:00
|
|
|
n.nc = nc
|
|
|
|
|
2021-03-01 11:51:38 +00:00
|
|
|
// usage w/o options is error-free
|
2022-03-18 15:13:12 +00:00
|
|
|
n.js, _ = nc.JetStream()
|
2021-03-01 11:51:38 +00:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
<-ctx.Done()
|
2023-04-12 14:35:10 +00:00
|
|
|
n.opts.log.Info(logs.NatsNatsClosingConnectionAsTheContextIsDone)
|
2021-03-01 11:51:38 +00:00
|
|
|
|
|
|
|
nc.Close()
|
|
|
|
}()
|
|
|
|
|
2022-03-18 15:13:12 +00:00
|
|
|
return nil
|
2021-03-01 11:51:38 +00:00
|
|
|
}
|