-
Notifications
You must be signed in to change notification settings - Fork 11
/
apcera_client.go
150 lines (123 loc) · 3.21 KB
/
apcera_client.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
package yagnats
import (
"sync"
"time"
nats "github.com/nats-io/nats.go"
)
type NATSConn interface {
Close()
Publish(subject string, data []byte) error
PublishRequest(subj, reply string, data []byte) error
Subscribe(subject string, handler nats.MsgHandler) (*nats.Subscription, error)
QueueSubscribe(subject, queue string, handler nats.MsgHandler) (*nats.Subscription, error)
Unsubscribe(sub *nats.Subscription) error
Ping() bool
AddReconnectedCB(func(*nats.Conn))
AddClosedCB(func(*nats.Conn))
AddDisconnectedCB(func(*nats.Conn))
Options() nats.Options
}
type apceraNATSWrapper struct {
*nats.Conn
reconnectCbs *[]func(*nats.Conn)
closedCbs *[]func(*nats.Conn)
disconnectedCbs *[]func(*nats.Conn)
*sync.Mutex
}
func newApceraClient() *apceraNATSWrapper {
reconnectCallbacks := make([]func(*nats.Conn), 0)
closedCallbacks := make([]func(*nats.Conn), 0)
disconnectedCallbacks := make([]func(*nats.Conn), 0)
return &apceraNATSWrapper{
nil,
&reconnectCallbacks,
&closedCallbacks,
&disconnectedCallbacks,
&sync.Mutex{},
}
}
func Connect(urls []string) (NATSConn, error) {
options := DefaultOptions()
options.Servers = urls
s := newApceraClient()
options.ReconnectedCB = s.apceraReconnectCB
options.ClosedCB = s.apceraClosedCB
options.DisconnectedCB = s.apceraDisconnectedCB
conn, err := options.Connect()
if err != nil {
return nil, err
}
s.Conn = conn
return s, nil
}
func ConnectWithOptions(urls []string, opts nats.Options) (NATSConn, error) {
opts.Servers = urls
s := newApceraClient()
opts.ReconnectedCB = s.apceraReconnectCB
opts.ClosedCB = s.apceraClosedCB
opts.DisconnectedCB = s.apceraDisconnectedCB
conn, err := opts.Connect()
if err != nil {
return nil, err
}
s.Conn = conn
return s, nil
}
func DefaultOptions() nats.Options {
options := nats.DefaultOptions
options.ReconnectWait = 500 * time.Millisecond
options.MaxReconnect = -1
return options
}
func (c *apceraNATSWrapper) AddReconnectedCB(handler func(*nats.Conn)) {
c.Lock()
defer c.Unlock()
callbacks := *c.reconnectCbs
callbacks = append(callbacks, handler)
c.reconnectCbs = &callbacks
}
func (c *apceraNATSWrapper) Options() nats.Options {
return c.Conn.Opts
}
func (c *apceraNATSWrapper) AddClosedCB(handler func(*nats.Conn)) {
c.Lock()
defer c.Unlock()
callbacks := *c.closedCbs
callbacks = append(callbacks, handler)
c.closedCbs = &callbacks
}
func (c *apceraNATSWrapper) AddDisconnectedCB(handler func(*nats.Conn)) {
c.Lock()
defer c.Unlock()
callbacks := *c.disconnectedCbs
callbacks = append(callbacks, handler)
c.disconnectedCbs = &callbacks
}
func (c *apceraNATSWrapper) Unsubscribe(sub *nats.Subscription) error {
return sub.Unsubscribe()
}
func (c *apceraNATSWrapper) Ping() bool {
err := c.FlushTimeout(500 * time.Millisecond)
return err == nil
}
func (c *apceraNATSWrapper) apceraReconnectCB(conn *nats.Conn) {
c.Lock()
defer c.Unlock()
for _, cb := range *c.reconnectCbs {
cb(conn)
}
}
func (c *apceraNATSWrapper) apceraClosedCB(conn *nats.Conn) {
c.Lock()
defer c.Unlock()
for _, cb := range *c.closedCbs {
cb(conn)
}
}
func (c *apceraNATSWrapper) apceraDisconnectedCB(conn *nats.Conn) {
c.Lock()
defer c.Unlock()
for _, cb := range *c.disconnectedCbs {
cb(conn)
}
}