-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
71 lines (63 loc) · 2.21 KB
/
main.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
package main
import (
"math/rand"
"time"
"github.com/Shopify/sarama"
prototube "github.com/fx19880617/prototube-go"
"github.com/google/uuid"
log "github.com/sirupsen/logrus"
)
var (
demoKafkaTopic = "prototube_demo"
demoKafkaBootstrapBrokerList = []string{"localhost:9092"}
)
func getDefaultKafkaProducerConfig() *sarama.Config {
config := sarama.NewConfig()
config.Producer.RequiredAcks = sarama.WaitForAll // Wait for all in-sync replicas to commit to ack
config.Producer.Compression = sarama.CompressionSnappy // Compress messages
config.Producer.Flush.Frequency = 1 * time.Second // Flush batches every 1 second
config.Producer.Flush.Bytes = 1 * 1024 * 1024 // The best-effort number of bytes needed to trigger a flush.
config.Producer.Flush.Messages = 1000 // The best-effort number of messages needed to trigger a flush.
// Retry in 5 min
config.Producer.Retry.Max = 30 // Retry 30 times for producer requests
config.Producer.Retry.Backoff = 10 * time.Second // Retry interval 10 secs for producer requests
return config
}
func generateRandomEvent() (*ExamplePrototubeMessage, error) {
uuid, err := uuid.NewUUID()
if err != nil {
return nil, err
}
uuidBytes, err := uuid.MarshalBinary()
if err != nil {
return nil, err
}
return &ExamplePrototubeMessage{
Int32Field: int32(rand.Intn(10000)),
Int64Field: rand.Int63n(int64(10000) + 10000),
DoubleField: rand.Float64(),
StringField: uuid.String(),
BytesField: uuidBytes,
}, nil
}
func main() {
log.Infoln("Trying to start example producer!")
producerConfig := &prototube.ProducerConfig{
KafkaBootstrapBrokerList: demoKafkaBootstrapBrokerList,
KafkaProducerConfig: getDefaultKafkaProducerConfig(),
}
producer, err := prototube.NewWithConfig(demoKafkaTopic, producerConfig)
if err != nil {
log.Fatalf("Failed to initialize stream producer with topic [ %v ] and producer config [ %v ]: %v", demoKafkaTopic, producerConfig, err)
}
defer producer.Stop()
for i := 0; i < 100; i++ {
msg, err := generateRandomEvent()
if err != nil {
log.Fatalf("Failed to generate random event: %v", err)
} else {
producer.Emit(msg)
}
}
log.Infof("Emit all messages to Kafka")
}