generated from layer5io/layer5-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 96
/
nats.go
111 lines (97 loc) · 2.67 KB
/
nats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package nats
import (
"log"
"strings"
"sync"
"time"
"github.com/layer5io/meshkit/broker"
nats "github.com/nats-io/nats.go"
)
type Options struct {
URLS []string
ConnectionName string
Username string
Password string
ReconnectWait time.Duration
MaxReconnect int
}
// Nats will implement Nats subscribe and publish functionality
type Nats struct {
ec *nats.EncodedConn
wg *sync.WaitGroup
}
// New - constructor
func New(opts Options) (broker.Handler, error) {
nc, err := nats.Connect(strings.Join(opts.URLS, ","),
nats.Name(opts.ConnectionName),
nats.ReconnectWait(opts.ReconnectWait),
nats.MaxReconnects(opts.MaxReconnect),
nats.UserInfo(opts.Username, opts.Password),
nats.DisconnectErrHandler(func(_ *nats.Conn, err error) {
log.Printf("client disconnected: %v", err)
}),
nats.ReconnectHandler(func(_ *nats.Conn) {
log.Printf("client reconnected")
}),
nats.ClosedHandler(func(_ *nats.Conn) {
log.Printf("client closed")
}),
nats.DiscoveredServersHandler(func(nc *nats.Conn) {
log.Printf("Known servers: %v\n", nc.Servers())
log.Printf("Discovered servers: %v\n", nc.DiscoveredServers())
}),
nats.ErrorHandler(func(_ *nats.Conn, _ *nats.Subscription, err error) {
log.Printf("Error: %v", err)
}),
)
if err != nil {
return nil, ErrConnect(err)
}
ec, err := nats.NewEncodedConn(nc, nats.JSON_ENCODER)
if err != nil {
return nil, ErrEncodedConn(err)
}
return &Nats{ec: ec}, nil
}
func (n *Nats) Info() string {
return n.ec.Conn.Opts.Name
}
// Publish - to publish messages
func (n *Nats) Publish(subject string, message *broker.Message) error {
err := n.ec.Publish(subject, message)
if err != nil {
return ErrPublish(err)
}
return nil
}
// PublishWithChannel - to publish messages with channel
func (n *Nats) PublishWithChannel(subject string, msgch chan *broker.Message) error {
err := n.ec.BindSendChan(subject, msgch)
if err != nil {
return ErrPublish(err)
}
return nil
}
// Subscribe - for subscribing messages
// TODO Ques: Do we want to unsubscribe
// TODO will the method-user just subsribe, how will it handle the received messages?
func (n *Nats) Subscribe(subject, queue string, message []byte) error {
n.wg.Add(1)
_, err := n.ec.QueueSubscribe(subject, queue, func(msg *nats.Msg) {
message = msg.Data
n.wg.Done()
})
if err != nil {
return ErrQueueSubscribe(err)
}
n.wg.Wait()
return nil
}
// SubscribeWithChannel will publish all the messages received to the given channel
func (n *Nats) SubscribeWithChannel(subject, queue string, msgch chan *broker.Message) error {
_, err := n.ec.BindRecvQueueChan(subject, queue, msgch)
if err != nil {
return ErrQueueSubscribe(err)
}
return nil
}