-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconnection.go
140 lines (117 loc) · 3.1 KB
/
connection.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
package dgnats
import (
"errors"
dgctx "github.com/darwinOrg/go-common/context"
dglogger "github.com/darwinOrg/go-logger"
"github.com/nats-io/nats.go"
"github.com/nats-io/nuid"
"math/rand"
"time"
)
var (
natsConns []*nats.Conn
natsJsMap = map[*nats.Conn]nats.JetStreamContext{}
connectionFailedError = errors.New("connection failed")
noConnectionError = errors.New("no connection")
connectWaitDuration = time.Second * 3
)
type NatsConfig struct {
PoolSize int `json:"pool-size" mapstructure:"pool-size"`
Servers []string `json:"servers" mapstructure:"servers"`
ConnectionName string `json:"connection-name" mapstructure:"connection-name"`
Username string `json:"username" mapstructure:"username"`
Password string `json:"password" mapstructure:"password"`
}
func Connect(natsConf *NatsConfig) error {
for i := 0; i < natsConf.PoolSize; i++ {
nc, err := connect(natsConf)
if err != nil {
return err
}
if nc.Status() != nats.CONNECTED {
time.Sleep(time.Second * 3)
}
if nc.Status() != nats.CONNECTED {
return connectionFailedError
}
}
return nil
}
func connect(natsConf *NatsConfig) (*nats.Conn, error) {
opts := nats.GetDefaultOptions()
opts.Servers = natsConf.Servers
opts.Name = natsConf.ConnectionName
opts.User = natsConf.Username
opts.Password = natsConf.Password
ctx := &dgctx.DgContext{TraceId: nuid.Next()}
opts.ConnectedCB = func(conn *nats.Conn) {
dglogger.Info(ctx, "nats: connection opened")
}
opts.ClosedCB = func(conn *nats.Conn) {
dglogger.Info(ctx, "nats: connection closed")
}
opts.ReconnectedCB = func(conn *nats.Conn) {
dglogger.Info(ctx, "nats: connection reconnected")
}
opts.LameDuckModeHandler = func(conn *nats.Conn) {
dglogger.Warn(ctx, "nats: lame duck mode")
}
opts.DiscoveredServersCB = func(conn *nats.Conn) {
dglogger.Warn(ctx, "nats: discovered servers")
}
opts.DisconnectedErrCB = func(conn *nats.Conn, err error) {
if err != nil {
dglogger.Infof(ctx, "nats disconnected error: %v", err)
}
}
opts.AsyncErrorCB = func(conn *nats.Conn, subscription *nats.Subscription, err error) {
if err != nil {
dglogger.Infof(ctx, "nats async subscription[%s] error: %v", subscription.Subject, err)
}
}
nc, err := opts.Connect()
if err != nil {
return nil, err
}
js, err := nc.JetStream()
if err != nil {
return nil, err
}
natsConns = append(natsConns, nc)
natsJsMap[nc] = js
return nc, nil
}
func getConn() (*nats.Conn, error) {
if len(natsConns) == 0 {
return nil, noConnectionError
}
nc := natsConns[rand.Int()%len(natsConns)]
if nc.Status() != nats.CONNECTED {
time.Sleep(connectWaitDuration)
}
if nc.Status() != nats.CONNECTED {
return nil, connectionFailedError
}
return nc, nil
}
func getJs() (nats.JetStreamContext, error) {
nc, err := getConn()
if err != nil {
return nil, err
}
return natsJsMap[nc], nil
}
func Flush(timeout time.Duration) error {
nc, err := getConn()
if err != nil {
return err
}
return nc.FlushTimeout(timeout)
}
func Close() {
for _, nc := range natsConns {
if nc != nil && !nc.IsClosed() {
nc.Close()
}
}
}