generated from layer5io/layer5-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 95
/
nats.go
160 lines (140 loc) · 3.74 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package nats
import (
"log"
"strings"
"sync"
"time"
"github.com/layer5io/meshkit/broker"
nats "github.com/nats-io/nats.go"
)
var (
NewEmptyConnection = &Nats{}
)
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) ConnectedEndpoints() (endpoints []string) {
for _, server := range n.ec.Conn.Servers() {
endpoints = append(endpoints, strings.TrimPrefix(server, "nats://"))
}
return
}
func (n *Nats) Info() string {
if n.ec == nil || n.ec.Conn == nil {
return broker.NotConnected
}
return n.ec.Conn.Opts.Name
}
func (n *Nats) CloseConnection() {
n.ec.Close()
}
// 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
}
// DeepCopyInto is a deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *Nats) DeepCopyInto(out broker.Handler) {
*out.(*Nats) = *in
}
// DeepCopy is a deepcopy function, copying the receiver, creating a new Nats.
func (in *Nats) DeepCopy() *Nats {
if in == nil {
return nil
}
out := new(Nats)
in.DeepCopyInto(out)
return out
}
// DeepCopyObject is a deepcopy function, copying the receiver, creating a new broker.Handler.
func (in *Nats) DeepCopyObject() broker.Handler {
if c := in.DeepCopy(); c != nil {
return c
}
return nil
}
// Check if the connection object is empty
func (in *Nats) IsEmpty() bool {
empty := &Nats{}
if in == nil || *in == *empty {
return true
}
return false
}