2015-05-23 20:04:16 +01:00
|
|
|
package nats
|
2015-04-26 19:33:35 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2015-05-16 00:34:02 +01:00
|
|
|
"strings"
|
2015-04-26 19:33:35 +01:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"code.google.com/p/go-uuid/uuid"
|
|
|
|
"github.com/apcera/nats"
|
2015-05-23 20:04:16 +01:00
|
|
|
"github.com/myodc/go-micro/broker"
|
2015-05-23 23:16:26 +01:00
|
|
|
c "github.com/myodc/go-micro/context"
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
2015-04-26 19:33:35 +01:00
|
|
|
)
|
|
|
|
|
2015-05-23 20:04:16 +01:00
|
|
|
type nbroker struct {
|
2015-05-16 00:34:02 +01:00
|
|
|
addrs []string
|
|
|
|
conn *nats.Conn
|
2015-04-26 19:33:35 +01:00
|
|
|
}
|
|
|
|
|
2015-05-23 20:04:16 +01:00
|
|
|
type subscriber struct {
|
2015-04-26 19:33:35 +01:00
|
|
|
s *nats.Subscription
|
|
|
|
}
|
|
|
|
|
2015-05-23 23:16:26 +01:00
|
|
|
// used in brokers where there is no support for headers
|
|
|
|
type envelope struct {
|
|
|
|
Header map[string]string
|
|
|
|
Message *broker.Message
|
|
|
|
}
|
|
|
|
|
2015-05-23 20:04:16 +01:00
|
|
|
func (n *subscriber) Topic() string {
|
2015-04-26 19:33:35 +01:00
|
|
|
return n.s.Subject
|
|
|
|
}
|
|
|
|
|
2015-05-23 20:04:16 +01:00
|
|
|
func (n *subscriber) Unsubscribe() error {
|
2015-04-26 19:33:35 +01:00
|
|
|
return n.s.Unsubscribe()
|
|
|
|
}
|
|
|
|
|
2015-05-23 20:04:16 +01:00
|
|
|
func (n *nbroker) Address() string {
|
2015-05-16 00:34:02 +01:00
|
|
|
if len(n.addrs) > 0 {
|
|
|
|
return n.addrs[0]
|
|
|
|
}
|
|
|
|
return ""
|
2015-04-26 19:33:35 +01:00
|
|
|
}
|
|
|
|
|
2015-05-23 20:04:16 +01:00
|
|
|
func (n *nbroker) Connect() error {
|
2015-04-26 19:33:35 +01:00
|
|
|
if n.conn != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-16 00:34:02 +01:00
|
|
|
opts := nats.DefaultOptions
|
|
|
|
opts.Servers = n.addrs
|
|
|
|
c, err := opts.Connect()
|
2015-04-26 19:33:35 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
n.conn = c
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-23 20:04:16 +01:00
|
|
|
func (n *nbroker) Disconnect() error {
|
2015-04-26 19:33:35 +01:00
|
|
|
n.conn.Close()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-23 20:04:16 +01:00
|
|
|
func (n *nbroker) Init() error {
|
2015-04-26 19:33:35 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-23 23:16:26 +01:00
|
|
|
func (n *nbroker) Publish(ctx context.Context, topic string, body []byte) error {
|
2015-05-26 22:39:48 +01:00
|
|
|
header, _ := c.GetMetadata(ctx)
|
2015-05-23 23:16:26 +01:00
|
|
|
|
|
|
|
message := &broker.Message{
|
2015-04-26 19:33:35 +01:00
|
|
|
Id: uuid.NewUUID().String(),
|
|
|
|
Timestamp: time.Now().Unix(),
|
|
|
|
Topic: topic,
|
2015-05-23 23:16:26 +01:00
|
|
|
Body: body,
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := json.Marshal(&envelope{
|
|
|
|
header,
|
|
|
|
message,
|
2015-04-26 19:33:35 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return n.conn.Publish(topic, b)
|
|
|
|
}
|
|
|
|
|
2015-05-23 23:16:26 +01:00
|
|
|
func (n *nbroker) Subscribe(topic string, function func(context.Context, *broker.Message)) (broker.Subscriber, error) {
|
2015-05-23 20:04:16 +01:00
|
|
|
sub, err := n.conn.Subscribe(topic, func(msg *nats.Msg) {
|
2015-05-23 23:16:26 +01:00
|
|
|
var e *envelope
|
|
|
|
if err := json.Unmarshal(msg.Data, &e); err != nil {
|
2015-04-26 19:33:35 +01:00
|
|
|
return
|
|
|
|
}
|
2015-05-26 22:39:48 +01:00
|
|
|
ctx := c.WithMetadata(context.Background(), e.Header)
|
2015-05-23 23:16:26 +01:00
|
|
|
function(ctx, e.Message)
|
2015-04-26 19:33:35 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-05-23 20:04:16 +01:00
|
|
|
return &subscriber{s: sub}, nil
|
2015-04-26 19:33:35 +01:00
|
|
|
}
|
|
|
|
|
2015-05-23 20:04:16 +01:00
|
|
|
func NewBroker(addrs []string, opt ...broker.Option) broker.Broker {
|
2015-05-23 13:13:55 +01:00
|
|
|
var cAddrs []string
|
|
|
|
for _, addr := range addrs {
|
|
|
|
if len(addr) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2015-05-16 00:34:02 +01:00
|
|
|
if !strings.HasPrefix(addr, "nats://") {
|
2015-05-23 13:13:55 +01:00
|
|
|
addr = "nats://" + addr
|
2015-05-16 00:34:02 +01:00
|
|
|
}
|
2015-05-23 13:13:55 +01:00
|
|
|
cAddrs = append(cAddrs, addr)
|
|
|
|
}
|
|
|
|
if len(cAddrs) == 0 {
|
|
|
|
cAddrs = []string{nats.DefaultURL}
|
2015-05-16 00:34:02 +01:00
|
|
|
}
|
2015-05-23 20:04:16 +01:00
|
|
|
return &nbroker{
|
2015-05-23 13:13:55 +01:00
|
|
|
addrs: cAddrs,
|
2015-04-26 19:33:35 +01:00
|
|
|
}
|
|
|
|
}
|