-
Notifications
You must be signed in to change notification settings - Fork 2
/
pulsar.go
177 lines (150 loc) · 3.96 KB
/
pulsar.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
177
package pulsar
import (
"bytes"
"context"
"encoding/json"
"sync"
"text/template"
"time"
"github.com/apache/pulsar-client-go/pulsar"
"github.com/liangyuanpeng/chirpstack-event-forward/internal/config"
"github.com/liangyuanpeng/chirpstack-event-forward/internal/integration"
"github.com/liangyuanpeng/chirpstack-event-forward/pkg/chirpstack/client"
log "github.com/sirupsen/logrus"
)
type Integration struct {
topicTemplate *template.Template
client pulsar.Client
producers sync.Map
producerNameTemplate *template.Template
chirpstackClient *client.ChirpstackClient
ch chan integration.HandleError
consumer pulsar.Consumer
}
type ProducerGroup struct {
producer pulsar.Producer
sync.Mutex
}
func (i *Integration) consumeDownlinkMessage() {
for {
msg, err := i.consumer.Receive(context.TODO())
if err != nil {
i.consumer.Nack(msg)
continue
}
dqi := &client.DeviceQueueItem{}
err = json.Unmarshal(msg.Payload(), dqi)
if err != nil {
i.ch <- integration.HandleError{
Err: err,
Name: "pulsar",
}
} else {
err = i.chirpstackClient.DownLink(context.TODO(), dqi)
if err != nil {
i.ch <- integration.HandleError{
Err: err,
Name: "pulsar",
}
continue
}
i.consumer.Ack(msg)
}
}
}
func New(config config.PulsarConfig, opt *config.IntegrationOption) (*Integration, error) {
chirpstackClient := opt.ChirpstackClient
t := template.New("pulsar topic template")
tem, err := t.Parse(config.TopicTemplate)
if err != nil {
return nil, err
}
t2 := template.New("pulsar producer name template")
tem2, err2 := t2.Parse(config.ProducerNameTemplate)
if err2 != nil {
return nil, err2
}
client, err := pulsar.NewClient(pulsar.ClientOptions{
URL: config.Url,
OperationTimeout: 30 * time.Second,
ConnectionTimeout: 30 * time.Second,
})
if err != nil {
return nil, err
}
i := &Integration{
client: client,
topicTemplate: tem,
producers: sync.Map{},
producerNameTemplate: tem2,
chirpstackClient: chirpstackClient,
ch: opt.Ch,
}
if chirpstackClient != nil {
if config.TopicsPattern == "" || config.ConsumerName == "" || config.SubscriptionName == "" {
log.Errorf("topicPattern/consumerName/subscriptionName is empty,please config it.")
} else {
consumer, err := client.Subscribe(pulsar.ConsumerOptions{
TopicsPattern: config.TopicsPattern,
SubscriptionName: config.SubscriptionName,
Name: config.ConsumerName,
Type: pulsar.KeyShared,
})
if err != nil {
return nil, err
}
i.consumer = consumer
go i.consumeDownlinkMessage()
}
}
return i, nil
}
func (i *Integration) HandleEvent(ctx context.Context, ch chan integration.HandleError, vars map[string]string, data []byte) (string, error) {
buf := new(bytes.Buffer)
i.topicTemplate.Execute(buf, vars)
topic := buf.String()
buf2 := new(bytes.Buffer)
i.producerNameTemplate.Execute(buf2, vars)
produerName := buf.String()
log.Infof("integration/pulsar: topic: %s", topic)
key := produerName
pgt, _ := i.producers.LoadOrStore(key, &ProducerGroup{})
pg := pgt.(*ProducerGroup)
if pg.producer == nil {
pg.Lock()
if pg.producer == nil {
tmp, err := i.client.CreateProducer(pulsar.ProducerOptions{
Topic: topic,
Name: produerName,
})
if err != nil {
pg.Unlock()
return "pulsar", err
}
pg.producer = tmp
}
pg.Unlock()
}
pg.producer.SendAsync(context.TODO(), &pulsar.ProducerMessage{
Payload: data,
Properties: vars,
}, func(mi pulsar.MessageID, pm *pulsar.ProducerMessage, err error) {
if err != nil {
ch <- integration.HandleError{
Err: err,
Name: "pulsar",
}
}
})
// _, err := pg.producer.Send(context.TODO(), &pulsar.ProducerMessage{
// Payload: data,
// })
return "pulsar", nil
}
func (i *Integration) Close() error {
if i.consumer != nil {
i.consumer.Close()
}
i.client.Close()
return nil
}