-
Notifications
You must be signed in to change notification settings - Fork 0
/
kafka.go
160 lines (146 loc) · 4.53 KB
/
kafka.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
153
154
155
156
157
158
159
160
package kafkaservice
import (
"context"
"encoding/json"
"fmt"
"math/rand"
"strconv"
"os"
models "consumer/models"
"consumer/pkg/utl/convert"
"consumer/resolver"
kafka "github.com/segmentio/kafka-go"
)
type KAFKA_TOPIC string
const (
ISSUED_TOKEN = "issued-token"
NEW_USER_CREATED = "new-user-created"
SIDE_TOPIC_FOR_RETRY = "side-topic-for-retry"
DEAD_LETTER_QUEUE = "dead-letter-queue"
)
type RetryMessage struct {
Topic string
Message []byte
Key string
}
func getBrokers(count int) []string {
broker := os.Getenv(fmt.Sprintf("KAFKA_HOST_%d", count))
if broker == "" {
return []string{}
}
return append([]string{broker}, getBrokers(count+1)...)
}
func consumeIssuedToken(ctx context.Context, r *resolver.Resolver) {
// initialize a new reader with the brokers and topic
// the groupID identifies the consumer and prevents
// it from receiving duplicate messages
issuedToken := kafka.NewReader(kafka.ReaderConfig{
Brokers: getBrokers(1),
Topic: string(ISSUED_TOKEN),
GroupID: "my-group",
})
for {
// here we will manual commit messages using FetchMessage instead of read message
msg, err := issuedToken.FetchMessage(ctx)
if err != nil {
fmt.Print("could not read message " + err.Error())
}
// after receiving the message, log its value
var user models.User
e := json.Unmarshal(msg.Value, &user)
if e != nil {
fmt.Print("consumeIssuedToken::error while unmarshalling.\n", e)
} else {
fmt.Printf("\n\nNew token issued by user:%d, partition: %d, offset: %d", user.ID, msg.Partition, msg.Offset)
for _, o := range r.Observers {
o <- convert.UserToGraphQlUser(&user)
}
if err := issuedToken.CommitMessages(ctx, msg); err != nil {
fmt.Printf("failed to commit message. "+
"Partition: %d, offset: %d", msg.Partition, msg.Offset)
} else {
// this is just an example of how to push to the side topic for retry
byteUser, _ := json.Marshal(user)
retryMessage := RetryMessage{
Topic: ISSUED_TOKEN,
Message: byteUser,
Key: string(msg.Key),
}
byteRetryMessage, _ := json.Marshal(retryMessage)
go Produce(context.Background(), SIDE_TOPIC_FOR_RETRY, []byte(strconv.Itoa(rand.Int())), byteRetryMessage)
fmt.Print("\n\ncommited message")
}
}
}
}
func consumeNewUserCreated(ctx context.Context, r *resolver.Resolver) {
// initialize a new reader with the brokers and topic
// the groupID identifies the consumer and prevents
// it from receiving duplicate messages
newUserCreatedReader := kafka.NewReader(kafka.ReaderConfig{
Brokers: getBrokers(1),
Topic: string(NEW_USER_CREATED),
GroupID: "my-group",
})
for {
// the `ReadMessage` method blocks until we receive the next event
msg, err := newUserCreatedReader.ReadMessage(ctx)
if err != nil {
fmt.Print("\n\ncould not read message " + err.Error())
}
var user models.User
e := json.Unmarshal(msg.Value, &user)
if e != nil {
fmt.Print("consumeNewUserCreated::\n\nerror while unmarshalling.\n", e)
} else {
fmt.Printf("\n\nNew user created: %d, partition: %d, offset: %d", user.ID, msg.Partition, msg.Offset)
for _, o := range r.Observers {
o <- convert.UserToGraphQlUser(&user)
}
}
}
}
func consumeSideTopic(ctx context.Context, r *resolver.Resolver) {
sideTopic := kafka.NewReader(kafka.ReaderConfig{
Brokers: getBrokers(1),
Topic: string(SIDE_TOPIC_FOR_RETRY),
GroupID: "my-group",
})
for {
msg, err := sideTopic.ReadMessage(ctx)
if err != nil {
fmt.Print("could not read message " + err.Error())
}
// after receiving the message, log its value
var retryMessage RetryMessage
fmt.Println(retryMessage)
e := json.Unmarshal(msg.Value, &retryMessage)
if e != nil {
fmt.Print("consumeSideTopic::error while unmarshalling.\n", e)
} else {
fmt.Printf("\n\nMessage consumed by side-topic.\n topic: %s, key: %spartition: %d, offset: %d",
retryMessage.Topic, retryMessage.Key, msg.Partition, msg.Offset)
go ScheduleKafkaRetry(retryMessage)
}
}
}
func Produce(ctx context.Context, topic KAFKA_TOPIC, key []byte, value []byte) {
kafkaWriter := kafka.NewWriter(kafka.WriterConfig{
Brokers: getBrokers(1),
Topic: string(topic),
Balancer: &kafka.RoundRobin{},
})
err := kafkaWriter.WriteMessages(ctx, kafka.Message{
// create an arbitrary message payload for the value
Value: value,
})
if err != nil {
fmt.Print("could not write message " + err.Error())
}
}
func Initiate(r *resolver.Resolver) {
ctx := context.Background()
go consumeIssuedToken(ctx, r)
go consumeNewUserCreated(ctx, r)
go consumeSideTopic(ctx, r)
}