This repository has been archived by the owner on Apr 27, 2023. It is now read-only.
forked from a8m/kinesis-producer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
example_test.go
152 lines (129 loc) · 3.13 KB
/
example_test.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
package producer
import (
"encoding/json"
"log"
"math/big"
"os"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/kinesis"
"github.com/google/uuid"
)
func ExampleSimple() {
logger := &StdLogger{log.New(os.Stdout, "", log.LstdFlags)}
client := kinesis.NewFromConfig(*aws.NewConfig())
pr := New(&Config{
StreamName: "test",
BacklogCount: 2000,
Client: client,
Logger: logger,
})
pr.Start()
failures := pr.NotifyFailures()
// Handle failures
go func() {
for r := range failures {
logger.Error("detected put failure", r)
}
}()
go func() {
for i := 0; i < 5000; i++ {
err := pr.Put([]byte("foo"), "bar")
if err != nil {
logger.Error("error producing", err)
}
}
}()
time.Sleep(3 * time.Second)
pr.Stop()
}
func ExampleShardMap() {
logger := &StdLogger{log.New(os.Stdout, "", log.LstdFlags)}
client := kinesis.NewFromConfig(*aws.NewConfig())
pr := New(&Config{
StreamName: "test",
BacklogCount: 2000,
Client: client,
GetShards: GetKinesisShardsFunc(client, "test"),
ShardRefreshInterval: 5 * time.Second,
Logger: logger,
})
pr.Start()
failures := pr.NotifyFailures()
// Handle failures
go func() {
for r := range failures {
logger.Error("detected put failure", r)
}
}()
go func() {
for i := 0; i < 1000; i++ {
pk := uuid.New().String()
for j := 0; j < 5; j++ {
err := pr.Put([]byte("foo"), pk)
if err != nil {
logger.Error("error producing", err)
}
}
}
}()
time.Sleep(3 * time.Second)
pr.Stop()
}
type myExampleUserRecord struct {
Id string `json:"id"`
Key string `json:"key"`
Val string `json:"val"`
data []byte `json:"-"`
}
func (r *myExampleUserRecord) PartitionKey() string { return r.Id }
func (r *myExampleUserRecord) ExplicitHashKey() *big.Int { return nil }
func (r *myExampleUserRecord) Data() []byte { return r.data }
func (r *myExampleUserRecord) Size() int { return len(r.data) }
func newMyExampleUserRecord(key, val string) (*myExampleUserRecord, error) {
r := &myExampleUserRecord{
Id: uuid.New().String(),
Key: key,
Val: val,
}
data, err := json.Marshal(r)
if err != nil {
return nil, err
}
r.data = data
return r, nil
}
func ExampleUserRecord() {
logger := &StdLogger{log.New(os.Stdout, "", log.LstdFlags)}
client := kinesis.NewFromConfig(*aws.NewConfig())
pr := New(&Config{
StreamName: "test",
BacklogCount: 2000,
Client: client,
GetShards: GetKinesisShardsFunc(client, "test"),
ShardRefreshInterval: 5 * time.Second,
Logger: logger,
})
pr.Start()
failures := pr.NotifyFailures()
// Handle failures
go func() {
for r := range failures {
logger.Error("detected put failure", r)
}
}()
go func() {
for i := 0; i < 5000; i++ {
record, err := newMyExampleUserRecord("foo", "bar")
if err != nil {
logger.Error("error creating user record", err)
}
err = pr.PutUserRecord(record)
if err != nil {
logger.Error("error producing", err)
}
}
}()
time.Sleep(3 * time.Second)
pr.Stop()
}