forked from cloudfoundry/yagnats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapcera_client.go
117 lines (99 loc) · 2.6 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
package yagnats
import (
"sync"
"time"
"github.com/apcera/nats"
)
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))
}
type apceraNATSWrapper struct {
*nats.Conn
reconnectCbs *[]func(*nats.Conn)
closedCbs *[]func(*nats.Conn)
disconnectedCbs *[]func(*nats.Conn)
*sync.Mutex
}
func Connect(urls []string) (NATSConn, error) {
options := nats.DefaultOptions
options.Servers = urls
options.ReconnectWait = 500 * time.Millisecond
options.MaxReconnect = -1
reconnectCallbacks := make([]func(*nats.Conn), 0)
closedCallbacks := make([]func(*nats.Conn), 0)
disconnectedCallbacks := make([]func(*nats.Conn), 0)
s := &apceraNATSWrapper{
nil,
&reconnectCallbacks,
&closedCallbacks,
&disconnectedCallbacks,
&sync.Mutex{},
}
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 (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) 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)
}
}