generated from ConduitIO/conduit-connector-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
source.go
183 lines (151 loc) · 4.63 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright © 2024 Meroxa, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package rabbitmq
import (
"context"
"errors"
"fmt"
"github.com/conduitio/conduit-commons/config"
"github.com/conduitio/conduit-commons/opencdc"
sdk "github.com/conduitio/conduit-connector-sdk"
"github.com/rabbitmq/amqp091-go"
)
type Source struct {
sdk.UnimplementedSource
conn *amqp091.Connection
ch *amqp091.Channel
queue amqp091.Queue
msgs <-chan amqp091.Delivery
config SourceConfig
}
func NewSource() sdk.Source {
return sdk.SourceWithMiddleware(&Source{}, sdk.DefaultSourceMiddleware()...)
}
func (s *Source) Parameters() config.Parameters {
return s.config.Parameters()
}
func (s *Source) Configure(ctx context.Context, cfg config.Config) error {
err := sdk.Util.ParseConfig(ctx, cfg, &s.config, s.config.Parameters())
if err != nil {
return fmt.Errorf("invalid config: %w", err)
}
sdk.Logger(ctx).Debug().Msg("source configured")
return nil
}
func (s *Source) Open(ctx context.Context, sdkPos opencdc.Position) (err error) {
s.conn, err = ampqDial(ctx, s.config.Config)
if err != nil {
return fmt.Errorf("failed to dial: %w", err)
}
sdk.Logger(ctx).Debug().Msg("connected to RabbitMQ")
s.ch, err = s.conn.Channel()
if err != nil {
return fmt.Errorf("failed to open channel: %w", err)
}
sdk.Logger(ctx).Debug().Msg("opened channel")
if sdkPos != nil {
pos, err := parsePosition(sdkPos)
if err != nil {
return fmt.Errorf("failed to parse position: %w", err)
}
if s.config.Queue.Name != "" && s.config.Queue.Name != pos.QueueName {
return fmt.Errorf(
"the old position contains a different queue name than the connector configuration (%q vs %q), please check if the configured queue name changed since the last run",
pos.QueueName, s.config.Queue.Name,
)
}
sdk.Logger(ctx).Debug().Msg("got queue name from given position")
s.config.Queue.Name = pos.QueueName
}
s.queue, err = s.ch.QueueDeclare(
s.config.Queue.Name,
s.config.Queue.Durable,
s.config.Queue.AutoDelete,
s.config.Queue.Exclusive,
s.config.Queue.NoWait,
nil)
if err != nil {
return fmt.Errorf("failed to declare queue: %w", err)
}
sdk.Logger(ctx).Debug().Str("queueName", s.queue.Name).Msg("declared queue")
s.msgs, err = s.ch.Consume(
s.queue.Name,
s.config.Consumer.Name,
s.config.Consumer.AutoAck,
s.config.Consumer.Exclusive,
s.config.Consumer.NoLocal,
s.config.Consumer.NoWait,
nil)
if err != nil {
return fmt.Errorf("failed to consume: %w", err)
}
sdk.Logger(ctx).Debug().Str("queueName", s.queue.Name).Msg("subscribed to queue")
return nil
}
func (s *Source) Read(ctx context.Context) (opencdc.Record, error) {
var rec opencdc.Record
select {
case <-ctx.Done():
err := ctx.Err()
if err != nil {
return rec, fmt.Errorf("context error: %w", err)
}
return rec, nil
case msg, ok := <-s.msgs:
if !ok {
return rec, errors.New("source message channel closed")
}
var (
pos = Position{
DeliveryTag: msg.DeliveryTag,
QueueName: s.queue.Name,
}
sdkPos = pos.ToSdkPosition()
metadata = metadataFromMessage(msg)
key = opencdc.RawData(msg.MessageId)
payload = opencdc.RawData(msg.Body)
)
rec = sdk.Util.Source.NewRecordCreate(sdkPos, metadata, key, payload)
sdk.Logger(ctx).Trace().Msgf("read message %s from %s", msg.MessageId, s.queue.Name)
return rec, nil
}
}
func (s *Source) Ack(_ context.Context, position opencdc.Position) error {
pos, err := parsePosition(position)
if err != nil {
return fmt.Errorf("failed to parse position: %w", err)
}
if err := s.ch.Ack(pos.DeliveryTag, false); err != nil {
return fmt.Errorf("failed to ack message: %w", err)
}
return nil
}
func (s *Source) Teardown(ctx context.Context) error {
errs := make([]error, 0, 2)
if s.ch != nil {
if err := s.ch.Close(); err != nil {
errs = append(errs, fmt.Errorf("failed to close channel: %w", err))
}
}
if s.conn != nil {
if err := s.conn.Close(); err != nil {
errs = append(errs, fmt.Errorf("failed to close connection: %w", err))
}
}
if err := errors.Join(errs...); err != nil {
return err
}
sdk.Logger(ctx).Debug().Msg("source teardown complete")
return nil
}