2019-05-29 16:46:57 +01:00
|
|
|
// Package nats provides a NATS broker
|
|
|
|
package nats
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
2020-01-30 14:39:00 +03:00
|
|
|
"github.com/micro/go-micro/v2/broker"
|
|
|
|
"github.com/micro/go-micro/v2/codec/json"
|
2020-03-11 20:55:39 +03:00
|
|
|
"github.com/micro/go-micro/v2/logger"
|
2020-01-30 14:39:00 +03:00
|
|
|
"github.com/micro/go-micro/v2/registry"
|
2019-05-29 16:46:57 +01:00
|
|
|
nats "github.com/nats-io/nats.go"
|
|
|
|
)
|
|
|
|
|
|
|
|
type natsBroker struct {
|
2019-08-07 17:58:45 +08:00
|
|
|
sync.Once
|
2019-05-29 16:46:57 +01:00
|
|
|
sync.RWMutex
|
2020-01-14 13:23:16 +00:00
|
|
|
|
|
|
|
// indicate if we're connected
|
|
|
|
connected bool
|
|
|
|
|
|
|
|
addrs []string
|
|
|
|
conn *nats.Conn
|
|
|
|
opts broker.Options
|
|
|
|
nopts nats.Options
|
|
|
|
|
|
|
|
// should we drain the connection
|
2019-08-07 17:58:45 +08:00
|
|
|
drain bool
|
|
|
|
closeCh chan (error)
|
2019-05-29 16:46:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type subscriber struct {
|
2019-08-07 17:58:45 +08:00
|
|
|
s *nats.Subscription
|
|
|
|
opts broker.SubscribeOptions
|
2019-05-29 16:46:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type publication struct {
|
2020-03-07 00:25:16 +03:00
|
|
|
t string
|
|
|
|
err error
|
|
|
|
m *broker.Message
|
2019-05-29 16:46:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *publication) Topic() string {
|
|
|
|
return p.t
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *publication) Message() *broker.Message {
|
|
|
|
return p.m
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *publication) Ack() error {
|
|
|
|
// nats does not support acking
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-07 00:25:16 +03:00
|
|
|
func (p *publication) Error() error {
|
|
|
|
return p.err
|
|
|
|
}
|
|
|
|
|
2019-05-29 16:46:57 +01:00
|
|
|
func (s *subscriber) Options() broker.SubscribeOptions {
|
|
|
|
return s.opts
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *subscriber) Topic() string {
|
|
|
|
return s.s.Subject
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *subscriber) Unsubscribe() error {
|
|
|
|
return s.s.Unsubscribe()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *natsBroker) Address() string {
|
|
|
|
if n.conn != nil && n.conn.IsConnected() {
|
|
|
|
return n.conn.ConnectedUrl()
|
|
|
|
}
|
2020-01-14 13:23:16 +00:00
|
|
|
|
2019-05-29 16:46:57 +01:00
|
|
|
if len(n.addrs) > 0 {
|
|
|
|
return n.addrs[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2020-01-14 13:23:16 +00:00
|
|
|
func (n *natsBroker) setAddrs(addrs []string) []string {
|
2019-12-03 22:59:44 +03:00
|
|
|
//nolint:prealloc
|
2019-05-29 16:46:57 +01:00
|
|
|
var cAddrs []string
|
|
|
|
for _, addr := range addrs {
|
|
|
|
if len(addr) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !strings.HasPrefix(addr, "nats://") {
|
|
|
|
addr = "nats://" + addr
|
|
|
|
}
|
|
|
|
cAddrs = append(cAddrs, addr)
|
|
|
|
}
|
2020-04-11 22:37:29 +03:00
|
|
|
if len(cAddrs) == 0 {
|
2019-05-29 16:46:57 +01:00
|
|
|
cAddrs = []string{nats.DefaultURL}
|
|
|
|
}
|
|
|
|
return cAddrs
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *natsBroker) Connect() error {
|
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
|
2020-04-11 22:37:29 +03:00
|
|
|
if n.connected {
|
|
|
|
return nil
|
2020-01-14 13:23:16 +00:00
|
|
|
}
|
|
|
|
|
2019-05-29 16:46:57 +01:00
|
|
|
status := nats.CLOSED
|
|
|
|
if n.conn != nil {
|
|
|
|
status = n.conn.Status()
|
|
|
|
}
|
|
|
|
|
|
|
|
switch status {
|
|
|
|
case nats.CONNECTED, nats.RECONNECTING, nats.CONNECTING:
|
2020-04-11 22:37:29 +03:00
|
|
|
n.connected = true
|
2019-05-29 16:46:57 +01:00
|
|
|
return nil
|
|
|
|
default: // DISCONNECTED or CLOSED or DRAINING
|
|
|
|
opts := n.nopts
|
|
|
|
opts.Servers = n.addrs
|
|
|
|
opts.Secure = n.opts.Secure
|
|
|
|
opts.TLSConfig = n.opts.TLSConfig
|
|
|
|
|
|
|
|
// secure might not be set
|
|
|
|
if n.opts.TLSConfig != nil {
|
|
|
|
opts.Secure = true
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := opts.Connect()
|
|
|
|
if err != nil {
|
2020-07-08 16:22:48 +01:00
|
|
|
if logger.V(logger.WarnLevel, logger.DefaultLogger) {
|
|
|
|
logger.Warnf("Error connecting to broker: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-05-29 16:46:57 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
n.conn = c
|
2020-04-11 22:37:29 +03:00
|
|
|
n.connected = true
|
2019-05-29 16:46:57 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *natsBroker) Disconnect() error {
|
2020-04-11 22:37:29 +03:00
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
2020-01-14 13:23:16 +00:00
|
|
|
|
|
|
|
// drain the connection if specified
|
2019-05-29 16:46:57 +01:00
|
|
|
if n.drain {
|
|
|
|
n.conn.Drain()
|
2020-02-11 18:46:50 +03:00
|
|
|
n.closeCh <- nil
|
2019-05-29 16:46:57 +01:00
|
|
|
}
|
2020-01-14 13:23:16 +00:00
|
|
|
|
|
|
|
// close the client connection
|
2019-08-07 17:58:45 +08:00
|
|
|
n.conn.Close()
|
2020-01-14 13:23:16 +00:00
|
|
|
|
|
|
|
// set not connected
|
|
|
|
n.connected = false
|
|
|
|
|
2019-05-29 16:46:57 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *natsBroker) Init(opts ...broker.Option) error {
|
2019-08-07 17:58:45 +08:00
|
|
|
n.setOption(opts...)
|
2019-05-29 16:46:57 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *natsBroker) Options() broker.Options {
|
|
|
|
return n.opts
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *natsBroker) Publish(topic string, msg *broker.Message, opts ...broker.PublishOption) error {
|
2020-04-11 22:37:29 +03:00
|
|
|
n.RLock()
|
|
|
|
defer n.RUnlock()
|
|
|
|
|
|
|
|
if n.conn == nil {
|
|
|
|
return errors.New("not connected")
|
|
|
|
}
|
|
|
|
|
2019-05-29 16:46:57 +01:00
|
|
|
b, err := n.opts.Codec.Marshal(msg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return n.conn.Publish(topic, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *natsBroker) Subscribe(topic string, handler broker.Handler, opts ...broker.SubscribeOption) (broker.Subscriber, error) {
|
2020-04-11 22:37:29 +03:00
|
|
|
n.RLock()
|
2019-05-29 16:46:57 +01:00
|
|
|
if n.conn == nil {
|
2020-04-11 22:37:29 +03:00
|
|
|
n.RUnlock()
|
2019-05-29 16:46:57 +01:00
|
|
|
return nil, errors.New("not connected")
|
|
|
|
}
|
2020-04-11 22:37:29 +03:00
|
|
|
n.RUnlock()
|
2019-05-29 16:46:57 +01:00
|
|
|
|
|
|
|
opt := broker.SubscribeOptions{
|
|
|
|
AutoAck: true,
|
|
|
|
Context: context.Background(),
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&opt)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn := func(msg *nats.Msg) {
|
|
|
|
var m broker.Message
|
2020-03-07 00:25:16 +03:00
|
|
|
pub := &publication{t: msg.Subject}
|
|
|
|
eh := n.opts.ErrorHandler
|
|
|
|
err := n.opts.Codec.Unmarshal(msg.Data, &m)
|
|
|
|
pub.err = err
|
|
|
|
pub.m = &m
|
|
|
|
if err != nil {
|
|
|
|
m.Body = msg.Data
|
2020-03-11 20:55:39 +03:00
|
|
|
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
|
|
|
|
logger.Error(err)
|
|
|
|
}
|
2020-03-07 00:25:16 +03:00
|
|
|
if eh != nil {
|
|
|
|
eh(pub)
|
|
|
|
}
|
2019-05-29 16:46:57 +01:00
|
|
|
return
|
|
|
|
}
|
2020-03-07 00:25:16 +03:00
|
|
|
if err := handler(pub); err != nil {
|
|
|
|
pub.err = err
|
2020-03-11 20:55:39 +03:00
|
|
|
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
|
|
|
|
logger.Error(err)
|
|
|
|
}
|
2020-03-07 00:25:16 +03:00
|
|
|
if eh != nil {
|
|
|
|
eh(pub)
|
|
|
|
}
|
|
|
|
}
|
2019-05-29 16:46:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var sub *nats.Subscription
|
|
|
|
var err error
|
|
|
|
|
|
|
|
n.RLock()
|
|
|
|
if len(opt.Queue) > 0 {
|
|
|
|
sub, err = n.conn.QueueSubscribe(topic, opt.Queue, fn)
|
|
|
|
} else {
|
|
|
|
sub, err = n.conn.Subscribe(topic, fn)
|
|
|
|
}
|
|
|
|
n.RUnlock()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-08-07 17:58:45 +08:00
|
|
|
return &subscriber{s: sub, opts: opt}, nil
|
2019-05-29 16:46:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *natsBroker) String() string {
|
|
|
|
return "nats"
|
|
|
|
}
|
|
|
|
|
2019-08-07 17:58:45 +08:00
|
|
|
func (n *natsBroker) setOption(opts ...broker.Option) {
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&n.opts)
|
2019-05-29 16:46:57 +01:00
|
|
|
}
|
|
|
|
|
2019-08-07 17:58:45 +08:00
|
|
|
n.Once.Do(func() {
|
|
|
|
n.nopts = nats.GetDefaultOptions()
|
|
|
|
})
|
|
|
|
|
|
|
|
if nopts, ok := n.opts.Context.Value(optionsKey{}).(nats.Options); ok {
|
|
|
|
n.nopts = nopts
|
2019-05-29 16:46:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// broker.Options have higher priority than nats.Options
|
|
|
|
// only if Addrs, Secure or TLSConfig were not set through a broker.Option
|
|
|
|
// we read them from nats.Option
|
2020-04-11 22:37:29 +03:00
|
|
|
if len(n.opts.Addrs) == 0 {
|
2019-08-07 17:58:45 +08:00
|
|
|
n.opts.Addrs = n.nopts.Servers
|
2019-05-29 16:46:57 +01:00
|
|
|
}
|
|
|
|
|
2019-08-07 17:58:45 +08:00
|
|
|
if !n.opts.Secure {
|
|
|
|
n.opts.Secure = n.nopts.Secure
|
2019-05-29 16:46:57 +01:00
|
|
|
}
|
|
|
|
|
2019-08-07 17:58:45 +08:00
|
|
|
if n.opts.TLSConfig == nil {
|
|
|
|
n.opts.TLSConfig = n.nopts.TLSConfig
|
2019-05-29 16:46:57 +01:00
|
|
|
}
|
2020-01-14 13:23:16 +00:00
|
|
|
n.addrs = n.setAddrs(n.opts.Addrs)
|
2019-08-07 17:58:45 +08:00
|
|
|
|
|
|
|
if n.opts.Context.Value(drainConnectionKey{}) != nil {
|
|
|
|
n.drain = true
|
|
|
|
n.closeCh = make(chan error)
|
|
|
|
n.nopts.ClosedCB = n.onClose
|
|
|
|
n.nopts.AsyncErrorCB = n.onAsyncError
|
2020-02-11 18:46:50 +03:00
|
|
|
n.nopts.DisconnectedErrCB = n.onDisconnectedError
|
2019-08-07 17:58:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *natsBroker) onClose(conn *nats.Conn) {
|
|
|
|
n.closeCh <- nil
|
|
|
|
}
|
2019-05-29 16:46:57 +01:00
|
|
|
|
2019-08-07 17:58:45 +08:00
|
|
|
func (n *natsBroker) onAsyncError(conn *nats.Conn, sub *nats.Subscription, err error) {
|
|
|
|
// There are kinds of different async error nats might callback, but we are interested
|
|
|
|
// in ErrDrainTimeout only here.
|
|
|
|
if err == nats.ErrDrainTimeout {
|
|
|
|
n.closeCh <- err
|
2019-05-29 16:46:57 +01:00
|
|
|
}
|
|
|
|
}
|
2020-01-14 13:23:16 +00:00
|
|
|
|
2020-02-11 18:46:50 +03:00
|
|
|
func (n *natsBroker) onDisconnectedError(conn *nats.Conn, err error) {
|
2020-02-11 21:41:23 +03:00
|
|
|
n.closeCh <- err
|
2020-02-11 18:46:50 +03:00
|
|
|
}
|
|
|
|
|
2020-01-14 13:23:16 +00:00
|
|
|
func NewBroker(opts ...broker.Option) broker.Broker {
|
|
|
|
options := broker.Options{
|
|
|
|
// Default codec
|
|
|
|
Codec: json.Marshaler{},
|
|
|
|
Context: context.Background(),
|
|
|
|
Registry: registry.DefaultRegistry,
|
|
|
|
}
|
|
|
|
|
|
|
|
n := &natsBroker{
|
|
|
|
opts: options,
|
|
|
|
}
|
|
|
|
n.setOption(opts...)
|
|
|
|
|
|
|
|
return n
|
|
|
|
}
|