-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsource.go
154 lines (139 loc) · 3.59 KB
/
source.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
package queue
import (
"context"
"fmt"
"time"
"github.com/kubemq-io/kubemq-bridges/middleware"
"github.com/kubemq-io/kubemq-bridges/pkg/roundrobin"
"github.com/kubemq-io/kubemq-bridges/config"
"github.com/kubemq-io/kubemq-bridges/pkg/logger"
"github.com/kubemq-io/kubemq-go/queues_stream"
)
type Source struct {
opts options
log *logger.Logger
targets []middleware.Middleware
isStopped bool
properties config.Metadata
roundRobin *roundrobin.RoundRobin
loadBalancingMode bool
bindingName string
}
func New() *Source {
return &Source{}
}
func (s *Source) getQueuesClient(ctx context.Context, id int) (*queues_stream.QueuesStreamClient, error) {
return queues_stream.NewQueuesStreamClient(ctx,
queues_stream.WithAddress(s.opts.host, s.opts.port),
queues_stream.WithClientId(fmt.Sprintf("kubemq-bridges_%s_%s", s.bindingName, s.opts.clientId)),
queues_stream.WithCheckConnection(true),
queues_stream.WithAutoReconnect(true),
queues_stream.WithAuthToken(s.opts.authToken),
queues_stream.WithConnectionNotificationFunc(
func(msg string) {
s.log.Infof(fmt.Sprintf("connection: %d, %s", id, msg))
}),
)
}
func (s *Source) onError(err error) {
s.log.Error(err.Error())
}
func (s *Source) Init(ctx context.Context, connection config.Metadata, properties config.Metadata, bindingName string, log *logger.Logger) error {
s.log = log
if s.log == nil {
s.log = logger.NewLogger("queue-stream")
}
var err error
s.opts, err = parseOptions(connection)
s.properties = properties
if err != nil {
return err
}
s.bindingName = bindingName
return nil
}
func (s *Source) Start(ctx context.Context, target []middleware.Middleware) error {
s.roundRobin = roundrobin.NewRoundRobin(len(target))
if s.properties != nil {
mode, ok := s.properties["load-balancing"]
if ok && mode == "true" {
s.loadBalancingMode = true
}
}
s.targets = target
for i := 0; i < s.opts.sources; i++ {
client, err := s.getQueuesClient(ctx, i+1)
if err != nil {
return err
}
go s.run(ctx, client)
}
return nil
}
func (s *Source) run(ctx context.Context, client *queues_stream.QueuesStreamClient) {
defer func() {
_ = client.Close()
}()
for {
if s.isStopped {
return
}
err := s.processQueueMessage(ctx, client)
if err != nil {
s.log.Error(err.Error())
time.Sleep(time.Second)
}
select {
case <-ctx.Done():
return
default:
}
}
}
func (s *Source) processQueueMessage(ctx context.Context, client *queues_stream.QueuesStreamClient) error {
pr := queues_stream.NewPollRequest().
SetChannel(s.opts.channel).
SetMaxItems(s.opts.batchSize).
SetWaitTimeout(s.opts.waitTimeout * 1000).
SetAutoAck(false).
SetOnErrorFunc(s.onError)
pollResp, err := client.Poll(ctx, pr)
if err != nil {
return err
}
if !pollResp.HasMessages() {
return nil
}
for _, message := range pollResp.Messages {
if s.loadBalancingMode {
_, err := s.targets[s.roundRobin.Next()].Do(ctx, message)
if err != nil {
if message.Policy.MaxReceiveCount < 1024 && message.Policy.MaxReceiveCount != message.Attributes.ReceiveCount {
return message.NAck()
}
}
} else {
wasExecuted := false
for _, target := range s.targets {
_, err := target.Do(ctx, message)
if err == nil {
wasExecuted = true
}
}
if !wasExecuted {
if message.Policy.MaxReceiveCount < 1024 && message.Policy.MaxReceiveCount != message.Attributes.ReceiveCount {
return message.NAck()
}
}
}
err = message.Ack()
if err != nil {
return err
}
}
return nil
}
func (s *Source) Stop() error {
s.isStopped = true
return nil
}