-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsumer.go
66 lines (55 loc) · 1.61 KB
/
consumer.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
package metamorph
import "github.com/Shopify/sarama"
import "log"
type consumer struct {
server *Server
consumer sarama.Consumer
partitionConsumers []sarama.PartitionConsumer
}
func newConsumer(server *Server) *consumer {
return &consumer{server, nil, make([]sarama.PartitionConsumer, 5)}
}
func (c *consumer) start(topics []string) error {
config := sarama.NewConfig()
consumer, err := sarama.NewConsumer([]string{"localhost:9092"}, config)
if err != nil {
return err
}
for i := range topics {
err = c.consumeTopic(consumer, topics[i])
if err != nil {
return err
}
}
return nil
}
func (c *consumer) consumeTopic(consumer sarama.Consumer, topic string) error {
log.Println("Creating consumer for topic", topic)
partitions, err := consumer.Partitions(topic)
if err != nil {
return err
}
for i := range partitions {
p := partitions[i]
log.Printf("Creating partition consumer for topic %s and partition %d", topic, p)
partitionConsumer, err := consumer.ConsumePartition(topic, p, sarama.OffsetOldest)
if err != nil {
log.Printf("ERROR: Couldn't create partition consumer %d", p)
return err
}
go c.forwardMessage(topic, partitionConsumer.Messages())
c.partitionConsumers = append(c.partitionConsumers, partitionConsumer)
}
return nil
}
type Message struct {
Type string `json:"type"`
Topic string `json:"topic"`
Value []byte `json:"message"`
}
func (c *consumer) forwardMessage(topic string, chMessages <-chan *sarama.ConsumerMessage) {
for m := range chMessages {
log.Println("Received message:", m)
c.server.SendEvent(Message{"message", topic, m.Value})
}
}