-
Notifications
You must be signed in to change notification settings - Fork 71
/
read.go
137 lines (113 loc) · 3.34 KB
/
read.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
package pulsar
import (
"context"
"encoding/json"
"fmt"
"time"
"github.com/apache/pulsar-client-go/pulsar"
"github.com/pkg/errors"
uuid "github.com/satori/go.uuid"
"github.com/batchcorp/plumber-schemas/build/go/protos/opts"
"github.com/batchcorp/plumber-schemas/build/go/protos/records"
"github.com/batchcorp/plumber/util"
"github.com/batchcorp/plumber/validate"
)
func (p *Pulsar) Read(ctx context.Context, readOpts *opts.ReadOptions, resultsChan chan *records.ReadRecord, errorChan chan *records.ErrorRecord) error {
if err := validateReadOptions(readOpts); err != nil {
return errors.Wrap(err, "invalid read options")
}
consumer, err := p.client.Subscribe(pulsar.ConsumerOptions{
Topic: readOpts.Pulsar.Args.Topic,
SubscriptionName: readOpts.Pulsar.Args.SubscriptionName,
Type: getSubscriptionType(readOpts),
})
if err != nil {
return errors.Wrap(err, "unable to create pulsar subscription")
}
defer consumer.Close()
defer consumer.Unsubscribe()
p.log.Info("Listening for message(s) ...")
var count int64
for {
msg, err := consumer.Receive(ctx)
if err != nil {
if err == context.Canceled {
p.log.Debug("context cancelled")
return nil
}
util.WriteError(nil, errorChan, errors.Wrap(err, "unable to read pulsar message"))
if !readOpts.Continuous {
return nil
}
continue
}
consumer.Ack(msg)
count++
serializedMsg, err := json.Marshal(msg)
if err != nil {
errorChan <- &records.ErrorRecord{
OccurredAtUnixTsUtc: time.Now().UTC().Unix(),
Error: errors.Wrap(err, "unable to serialize message into JSON").Error(),
}
}
resultsChan <- &records.ReadRecord{
MessageId: uuid.NewV4().String(),
Num: count,
ReceivedAtUnixTsUtc: time.Now().UTC().Unix(),
Payload: msg.Payload(),
XRaw: serializedMsg,
Record: &records.ReadRecord_Pulsar{
Pulsar: &records.Pulsar{
Id: fmt.Sprintf("%s", msg.ID()),
Key: msg.Key(),
Topic: msg.Topic(),
Properties: msg.Properties(),
RedeliveryCount: msg.RedeliveryCount(),
EventTime: msg.EventTime().Format(time.RFC3339),
IsReplicated: msg.IsReplicated(),
OrderingKey: msg.OrderingKey(),
ProducerName: msg.ProducerName(),
PublishTime: msg.PublishTime().Format(time.RFC3339),
Timestamp: time.Now().UTC().Unix(),
Value: msg.Payload(),
},
},
}
if !readOpts.Continuous {
return nil
}
}
return nil
}
// getSubscriptionType converts string input of the subscription type to pulsar library's equivalent
func getSubscriptionType(readOpts *opts.ReadOptions) pulsar.SubscriptionType {
switch readOpts.Pulsar.Args.SubscriptionType.String() {
case "EXCLUSIVE":
return pulsar.Exclusive
case "FAILOVER":
return pulsar.Failover
case "KEYSHARED":
return pulsar.KeyShared
default:
return pulsar.Shared
}
}
func validateReadOptions(readOpts *opts.ReadOptions) error {
if readOpts == nil {
return validate.ErrMissingReadOptions
}
if readOpts.Pulsar == nil {
return validate.ErrEmptyBackendGroup
}
args := readOpts.Pulsar.Args
if args == nil {
return validate.ErrEmptyBackendArgs
}
if args.Topic == "" {
return ErrEmptyTopic
}
if args.SubscriptionName == "" {
return ErrEmptySubscriptionName
}
return nil
}