-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnats_adapter.go
More file actions
166 lines (134 loc) · 4.55 KB
/
nats_adapter.go
File metadata and controls
166 lines (134 loc) · 4.55 KB
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
package dgmq
import (
"fmt"
"os"
"time"
dgctx "github.com/darwinOrg/go-common/context"
dgsys "github.com/darwinOrg/go-common/sys"
dglogger "github.com/darwinOrg/go-logger"
dgnats "github.com/darwinOrg/go-nats"
)
type natsAdapter struct {
group string
}
func NewNatsAdapter(config *MqAdapterConfig) MqAdapter {
ctx := dgctx.SimpleDgContext()
natsCfg := &dgnats.NatsConfig{
PoolSize: config.PoolSize,
Servers: []string{fmt.Sprintf("nats://%s:%d", config.Host, config.Port)},
ConnectionName: config.Username,
Username: config.Username,
Password: config.Password,
}
err := dgnats.Connect(natsCfg)
if err != nil {
dglogger.Errorf(ctx, "connect nats error: %v", err)
if dgsys.IsFormalProfile() {
os.Exit(1)
}
}
return &natsAdapter{
group: config.Group,
}
}
func (a *natsAdapter) CreateTopic(ctx *dgctx.DgContext, topic string) error {
subject := a.buildNatsSubject(topic)
return dgnats.InitStream(ctx, subject)
}
func (a *natsAdapter) Publish(ctx *dgctx.DgContext, topic string, message any) error {
return a.PublishWithTag(ctx, topic, "", message)
}
func (a *natsAdapter) PublishWithTag(ctx *dgctx.DgContext, topic, tag string, message any) error {
msgData, err := dgnats.ToBytes(ctx, message)
if err != nil {
return err
}
if tag != "" {
dglogger.Infof(ctx, "Publish | topic: %s | tag: %s | message: %s", topic, tag, string(msgData))
} else {
dglogger.Infof(ctx, "Publish | topic: %s | message: %s", topic, string(msgData))
}
subject := a.buildNatsSubject(topic)
err = dgnats.PublishRawWithTag(ctx, subject, tag, msgData)
if err != nil {
dglogger.Errorf(ctx, "dgnats.PublishRaw error | topic: %s | err: %v", topic, err)
}
return err
}
func (a *natsAdapter) PublishDelay(ctx *dgctx.DgContext, topic string, message any, delay time.Duration) error {
msgData, err := dgnats.ToBytes(ctx, message)
if err != nil {
return err
}
dglogger.Infof(ctx, "PublishDelay | topic: %s | message: %s | delay: %d", topic, string(msgData), delay)
subject := a.buildNatsSubject(topic)
err = dgnats.PublishDelay(ctx, subject, message, delay)
if err != nil {
dglogger.Errorf(ctx, "dgnats.PublishDelay error | topic: %s | err: %v", topic, err)
}
return err
}
func (a *natsAdapter) Destroy(ctx *dgctx.DgContext, topic string) error {
subject := a.buildNatsSubject(topic)
return dgnats.DeleteStream(ctx, subject)
}
func (a *natsAdapter) Subscribe(ctx *dgctx.DgContext, topic string, handler SubscribeHandler) (SubscribeEndCallback, error) {
return a.SubscribeWithTag(ctx, topic, "", handler)
}
func (a *natsAdapter) SubscribeWithTag(ctx *dgctx.DgContext, topic, tag string, handler SubscribeHandler) (SubscribeEndCallback, error) {
subject := a.buildNatsSubject(topic)
sub, err := dgnats.SubscribeWithTag(ctx, subject, tag, func(ctx *dgctx.DgContext, bytes []byte) error {
message := string(bytes)
if tag != "" {
dglogger.Infof(ctx, "Subscribe | topic: %s | tag: %s | message: %s", topic, tag, message)
} else {
dglogger.Infof(ctx, "Subscribe | topic: %s | message: %s", topic, message)
}
return handler(ctx, message)
})
if err != nil {
return nil, err
}
return func() { _ = sub.Unsubscribe() }, nil
}
func (a *natsAdapter) DynamicSubscribe(ctx *dgctx.DgContext, closeCh chan struct{}, topic string, handler SubscribeHandler) error {
if closeCh != nil {
go func() {
<-closeCh
dglogger.Infof(ctx, "closed topic: %s ", topic)
}()
}
_, err := a.Subscribe(ctx, topic, handler)
return err
}
func (a *natsAdapter) SubscribeDelay(ctx *dgctx.DgContext, topic string, sleepDuration time.Duration, handler SubscribeHandler) (SubscribeEndCallback, error) {
subject := a.buildNatsSubject(topic)
sub, err := dgnats.SubscribeDelay(ctx, subject, sleepDuration, func(ctx *dgctx.DgContext, bytes []byte) error {
message := string(bytes)
dglogger.Infof(ctx, "Subscribe | topic: %s | message: %s", topic, message)
return handler(ctx, message)
})
if err != nil {
return nil, err
}
return func() { _ = sub.Unsubscribe() }, nil
}
func (a *natsAdapter) Unsubscribe(ctx *dgctx.DgContext, topic string) error {
subject := a.buildNatsSubject(topic)
return dgnats.Unsubscribe(ctx, subject, "")
}
func (a *natsAdapter) UnsubscribeWithTag(ctx *dgctx.DgContext, topic, tag string) error {
subject := a.buildNatsSubject(topic)
return dgnats.Unsubscribe(ctx, subject, tag)
}
func (a *natsAdapter) Close() {
dgnats.Close()
}
func (a *natsAdapter) buildNatsSubject(topic string) *dgnats.NatsSubject {
name := dgnats.ReplaceIllegalCharacter(topic)
return &dgnats.NatsSubject{
Category: name,
Name: name,
Group: a.group,
}
}