-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
kinesis.go
84 lines (66 loc) · 2.23 KB
/
kinesis.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
// Package kinesis is the AWS Kinesis implementation of an event stream.
package kinesis
import (
"context"
"fmt"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/kinesis"
"github.com/aws/aws-sdk-go/service/kinesis/kinesisiface"
"github.com/italolelis/outboxer"
)
const (
// StreamNameOption is the stream name option.
StreamNameOption = "stream_name"
// ExplicitHashKeyOption is the explicit hash key option.
ExplicitHashKeyOption = "explicit_hash_key"
// PartitionKeyOption is the partition key option.
PartitionKeyOption = "partition_key"
// SequenceNumberForOrderingOption is the sequence number for ordering option.
SequenceNumberForOrderingOption = "partition_key"
)
// Kinesis is the wrapper for the Kinesis library.
type Kinesis struct {
conn kinesisiface.KinesisAPI
}
type options struct {
streamName *string
explicitHashKey *string
partitionKey *string
sequenceNumberForOrdering *string
}
// New creates a new instance of Kinesis.
func New(conn kinesisiface.KinesisAPI) *Kinesis {
return &Kinesis{conn: conn}
}
// Send sends the message to the event stream.
func (r *Kinesis) Send(ctx context.Context, evt *outboxer.OutboxMessage) error {
opts := r.parseOptions(evt.Options)
_, err := r.conn.PutRecordWithContext(ctx, &kinesis.PutRecordInput{
Data: evt.Payload,
StreamName: opts.streamName,
ExplicitHashKey: opts.explicitHashKey,
SequenceNumberForOrdering: opts.sequenceNumberForOrdering,
PartitionKey: opts.partitionKey,
})
if err != nil {
return fmt.Errorf("failed to publish message: %w", err)
}
return nil
}
func (r *Kinesis) parseOptions(opts outboxer.DynamicValues) *options {
opt := options{partitionKey: aws.String(time.Now().Format(time.RFC3339Nano))}
if data, ok := opts[StreamNameOption]; ok {
opt.streamName = aws.String(data.(string))
}
if data, ok := opts[ExplicitHashKeyOption]; ok {
opt.explicitHashKey = aws.String(data.(string))
}
if data, ok := opts[PartitionKeyOption]; ok {
opt.partitionKey = aws.String(data.(string))
}
if data, ok := opts[SequenceNumberForOrderingOption]; ok {
opt.sequenceNumberForOrdering = aws.String(data.(string))
}
return &opt
}