-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
229 lines (193 loc) · 5.63 KB
/
client.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package kafkake
import (
"fmt"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/confluentinc/confluent-kafka-go/kafka"
)
// State represents the current state of the Kafkake client
type State int
const (
notReady State = iota // notReady indicates the client has not been initialized
online // online indicates that the client is ready for communication
offline // offline indicates that the client has been shut down
)
// ClientOption defines possible options that can be used to customize the client
type ClientOption func(client *Client)
// ClientConfig defines the parameters to be used by the client
type ClientConfig struct {
BootstrapServers string
SecurityProtocol string
SaslMechanisms string
SaslUsername string
SaslPassword string
}
// Client is the interface to interact with a kafka cluster
type Client struct {
State State
Exchanger *Exchanger
Consumer *kafka.Consumer
ConsumerTopic string
Producer *kafka.Producer
ProducerTopic string
}
// WithExchanger customizes the client to use a specified Exchanger
func WithExchanger(e *Exchanger) ClientOption {
return func(c *Client) {
c.Exchanger = e
}
}
// WithNewExchanger customizes the client to use a newly specified Exchanger
func WithNewExchanger(h ClientHandler, reqSchema, respSchema interface{}) ClientOption {
return func(c *Client) {
c.Exchanger = NewExchanger(c, h, reqSchema, respSchema)
}
}
// NewClient creates a new client object with the specified configuration and options
func NewClient(config ClientConfig, requestTopic, responseTopic string, opts ...ClientOption) (*Client, error) {
c := Client{State: notReady}
// Create Consumer instance
consumer, err := kafka.NewConsumer(&kafka.ConfigMap{
"bootstrap.servers": config.BootstrapServers,
"sasl.mechanisms": config.SaslMechanisms,
"security.protocol": config.SecurityProtocol,
"sasl.username": config.SaslUsername,
"sasl.password": config.SaslPassword,
"group.id": "go_" + strings.ReplaceAll(requestTopic, "-", "_") + "_group",
"auto.offset.reset": "earliest",
})
if err != nil {
return nil, err
}
// Subscribe to topic
err = consumer.SubscribeTopics([]string{requestTopic}, nil)
if err != nil {
return nil, err
}
c.Consumer = consumer
c.ConsumerTopic = requestTopic
// Create Producer instance
producer, err := kafka.NewProducer(&kafka.ConfigMap{
"bootstrap.servers": config.BootstrapServers,
"sasl.mechanisms": config.SaslMechanisms,
"security.protocol": config.SecurityProtocol,
"sasl.username": config.SaslUsername,
"sasl.password": config.SaslPassword})
if err != nil {
return nil, err
}
c.Producer = producer
c.ProducerTopic = responseTopic
c.State = online
// Loop through each option
for _, opt := range opts {
opt(&c)
}
return &c, nil
}
// ReadMessage waits for a message from the Kafka cluster to be received on the current request topic
func (c *Client) ReadMessage(duration time.Duration) (*kafka.Message, error) {
if c.State != online {
return nil, fmt.Errorf("client is not online")
}
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
var err error
run := true
for run {
select {
case sig := <-sigChan:
err = fmt.Errorf("caught signal %v: terminating", sig)
run = false
default:
msg, err := c.Consumer.ReadMessage(duration)
if err != nil {
continue
} else {
fmt.Println("Received message")
return msg, nil
}
}
}
return nil, err
}
// SendMessage sends a message from the Kafka cluster to be received on the current request topic
func (c *Client) SendMessage(msg *kafka.Message) error {
if c.State != online {
return fmt.Errorf("client is not online")
}
// Go-routine to handle message delivery reports and
// possibly other event types (errors, stats, etc)
go func() {
for e := range c.Producer.Events() {
switch ev := e.(type) {
case *kafka.Message:
if ev.TopicPartition.Error != nil {
fmt.Printf("Failed to deliver message: %v\n", ev.TopicPartition)
} else {
fmt.Printf("Successfully produced record to topic %s partition [%d] @ offset %v\n",
*ev.TopicPartition.Topic, ev.TopicPartition.Partition, ev.TopicPartition.Offset)
}
}
}
}()
err := c.Producer.Produce(msg, nil)
if err != nil {
return err
}
// Wait for all messages to be delivered
c.Producer.Flush(15 * 1000)
fmt.Println("Sent message")
return nil
}
// ExchangeMessage waits for a message and sends a message to the kafka cluster with the parameterized Exchanger
func (c *Client) ExchangeMessage() error {
if c.State != online {
return fmt.Errorf("client is not online")
} else if c.Exchanger == nil {
return fmt.Errorf("exchanger not available")
}
req, err := c.ReadMessage(100 * time.Second)
if err != nil {
return err
}
resp, err := c.Exchanger.Handle(req)
if err != nil {
return err
}
return c.SendMessage(resp)
}
// Shutdown safely closes the client
func (c *Client) Shutdown() error {
if c.State != online {
return fmt.Errorf("client is not online")
}
c.State = offline
c.Producer.Close()
err := c.Consumer.Close()
return err
}
// ExchangeLoop starts a loop to indefinitely wait and handle messages on the Kafka cluster
func (c *Client) ExchangeLoop() error {
// Set up a channel for handling Ctrl-C, etc
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
// Process messages
run := true
for run == true {
select {
case sig := <-sigChan:
fmt.Printf("Caught signal %v: terminating\n", sig)
run = false
default:
err := c.ExchangeMessage()
if err != nil {
return err
}
}
}
return c.Shutdown()
}