-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection_manager.go
176 lines (138 loc) · 3.93 KB
/
connection_manager.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package nats
import (
"context"
"time"
"github.com/nats-io/nats.go"
"github.com/nats-io/nats.go/jetstream"
"gofr.dev/pkg/gofr/datasource"
"gofr.dev/pkg/gofr/datasource/pubsub"
)
//go:generate mockgen -destination=mock_jetstream.go -package=nats github.com/nats-io/nats.go/jetstream JetStream,Stream,Consumer,Msg,MessageBatch
const (
ctxCloseTimeout = 5 * time.Second
)
type ConnectionManager struct {
conn ConnInterface
jetStream jetstream.JetStream
config *Config
logger pubsub.Logger
natsConnector NATSConnector
jetStreamCreator JetStreamCreator
}
func (cm *ConnectionManager) JetStream() (jetstream.JetStream, error) {
if cm.jetStream == nil {
return nil, errJetStreamNotConfigured
}
return cm.jetStream, nil
}
// natsConnWrapper wraps a nats.Conn to implement the ConnInterface.
type natsConnWrapper struct {
conn *nats.Conn
}
func (w *natsConnWrapper) Status() nats.Status {
return w.conn.Status()
}
func (w *natsConnWrapper) Close() {
w.conn.Close()
}
func (w *natsConnWrapper) NATSConn() *nats.Conn {
return w.conn
}
func (w *natsConnWrapper) JetStream() (jetstream.JetStream, error) {
return jetstream.New(w.conn)
}
// NewConnectionManager creates a new ConnectionManager.
func NewConnectionManager(
cfg *Config,
logger pubsub.Logger,
natsConnector NATSConnector,
jetStreamCreator JetStreamCreator) *ConnectionManager {
// if logger is nil panic
if logger == nil {
panic("logger is required")
}
if natsConnector == nil {
natsConnector = &DefaultNATSConnector{}
}
if jetStreamCreator == nil {
jetStreamCreator = &DefaultJetStreamCreator{}
}
return &ConnectionManager{
config: cfg,
logger: logger,
natsConnector: natsConnector,
jetStreamCreator: jetStreamCreator,
}
}
// Connect establishes a connection to NATS and sets up JetStream.
func (cm *ConnectionManager) Connect(_ context.Context) error {
cm.logger.Logf("Connecting to NATS server at %v", cm.config.Server)
opts := []nats.Option{nats.Name("GoFr NATS JetStreamClient")}
if cm.config.CredsFile != "" {
opts = append(opts, nats.UserCredentials(cm.config.CredsFile))
}
connInterface, err := cm.natsConnector.Connect(cm.config.Server, opts...)
if err != nil {
cm.logger.Errorf("failed to connect to NATS server at %v: %v", cm.config.Server, err)
return err
}
js, err := cm.jetStreamCreator.New(connInterface)
if err != nil {
connInterface.Close()
cm.logger.Errorf("failed to create JetStream context: %v", err)
return err
}
cm.conn = connInterface
cm.jetStream = js
return nil
}
func (cm *ConnectionManager) Close(ctx context.Context) {
_, cancel := context.WithTimeout(ctx, ctxCloseTimeout)
defer cancel()
if cm.conn != nil {
cm.conn.Close()
}
}
func (cm *ConnectionManager) Publish(ctx context.Context, subject string, message []byte, metrics Metrics) error {
metrics.IncrementCounter(ctx, "app_pubsub_publish_total_count", "subject", subject)
if err := cm.validateJetStream(subject); err != nil {
return err
}
_, err := cm.jetStream.Publish(ctx, subject, message)
if err != nil {
cm.logger.Errorf("failed to publish message to NATS JetStream: %v", err)
return err
}
metrics.IncrementCounter(ctx, "app_pubsub_publish_success_count", "subject", subject)
return nil
}
func (cm *ConnectionManager) validateJetStream(subject string) error {
if cm.jetStream == nil || subject == "" {
err := errJetStreamNotConfigured
cm.logger.Error(err.Error())
return err
}
return nil
}
func (cm *ConnectionManager) Health() datasource.Health {
if cm.conn == nil {
return datasource.Health{
Status: datasource.StatusDown,
}
}
status := cm.conn.Status()
if status == nats.CONNECTED {
return datasource.Health{
Status: datasource.StatusUp,
Details: map[string]interface{}{
"server": cm.config.Server,
},
}
}
return datasource.Health{
Status: datasource.StatusDown,
Details: map[string]interface{}{
"server": cm.config.Server,
},
}
}