-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
61 lines (54 loc) · 1.23 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
package main
import (
"fmt"
"log"
"os"
"os/signal"
"runtime/pprof"
"github.com/Shopify/sarama"
)
func main() {
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt)
f, err := os.Create("profile")
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
clientConfig := sarama.NewClientConfig()
client, err := sarama.NewClient("client_id", []string{"192.168.100.67:6667"}, clientConfig)
if err != nil {
panic(err)
} else {
fmt.Println("> connected")
}
defer client.Close()
sarama.Logger = log.New(os.Stdout, "[Sarama] ", log.LstdFlags)
producerConfig := sarama.NewProducerConfig()
producerConfig.Compression = sarama.CompressionSnappy
producer, err := sarama.NewProducer(client, producerConfig)
if err != nil {
panic(err)
}
defer func() {
fmt.Println(producer.Close())
}()
i := 0
for {
select {
// default:
case producer.Input() <- &sarama.MessageToSend{Topic: "my_topic", Key: nil, Value: sarama.ByteEncoder(make([]byte, 1024))}:
// producer.QueueMessage("my_topic", nil, sarama.ByteEncoder(make([]byte, 1024)))
i++
if i >= 10000 {
fmt.Println("batch")
i = 0
}
case err := <-producer.Errors():
fmt.Println(err)
case <-sig:
return
}
}
}