-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
214 lines (180 loc) · 5.42 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
// Copyright 2017-2019 Skroutz S.A.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"bufio"
"context"
"errors"
"fmt"
"log"
"net"
"os"
"strings"
"sync"
rdkafka "github.com/confluentinc/confluent-kafka-go/kafka"
redisproto "github.com/secmask/go-redisproto"
)
type Client struct {
id string
conn net.Conn
cfg Config
ctx context.Context
log *log.Logger
monitorChan chan string
consGID string
consReady bool
consWg *sync.WaitGroup
consumer *Consumer
producer *Producer
}
// NewClient returns a new client. After it's no longer needed, the client
// should be closed with Close().
func NewClient(ctx context.Context, conn net.Conn, cfg Config) *Client {
id := conn.RemoteAddr().String()
client := &Client{
id: id,
monitorChan: make(chan string, 1000),
conn: conn,
cfg: cfg,
ctx: ctx,
log: log.New(os.Stderr, fmt.Sprintf("[client-%s] ", id), log.Ldate|log.Ltime),
}
go client.monitorWriter()
return client
}
// monitorWriter streams any monitor strings written to the client's monitor channel.
func (c *Client) monitorWriter() {
writer := redisproto.NewWriter(bufio.NewWriter(c.conn))
for monitorOutput := range c.monitorChan {
writer.WriteSimpleString(monitorOutput)
writer.Flush()
}
}
// SetID sets the id for c. It returns an error if id is not in the form of
// <consumer-group>:<consumer-id>.
func (c *Client) SetID(id string) error {
if c.consReady {
return errors.New("Client ID is already set to " + c.id)
}
parts := strings.SplitN(id, ":", 2)
if len(parts) != 2 {
return errors.New("Cannot parse group.id")
}
c.id = id
c.consGID = parts[0]
c.log.SetPrefix(fmt.Sprintf("[client-%s] ", c.id))
c.consReady = true
return nil
}
func (c *Client) String() string {
return c.id
}
// registerConsumer registers a new Consumer denoted by cid.
func (c *Client) registerConsumer(
cid ConsumerID, gid string, topics []string, cfg rdkafka.ConfigMap) (*Consumer, error) {
if gid == "" {
return nil, errors.New("Failed to register Consumer (attr: `group.id` is missing)")
}
// apparently, reusing the same config between consumers silently makes them non-operational
kafkaCfg := rdkafka.ConfigMap{}
for k, v := range c.cfg.Librdkafka.Consumer {
err := kafkaCfg.SetKey(k, v)
if err != nil {
return nil, err
}
}
for k, v := range cfg {
err := kafkaCfg.SetKey(k, v)
if err != nil {
return nil, err
}
}
err := kafkaCfg.SetKey("group.id", gid)
if err != nil {
return nil, err
}
// Extract the consumer name from the client id.
// We know by client.Consumer() that cid is in the form of `<group:name>|<topics>`
cidNoTopics := strings.Split(strings.Split(string(cid), "|")[0], ":")[1]
err = kafkaCfg.SetKey("client.id", cidNoTopics)
if err != nil {
return nil, err
}
ctx, consumerCancel := context.WithCancel(c.ctx)
cons, err := NewConsumer(cid, topics, kafkaCfg)
if err != nil {
return nil, err
}
cons.cancel = consumerCancel
c.consWg.Add(1)
go func(ctx context.Context) {
defer c.consWg.Done()
cons.Run(ctx)
}(ctx)
return cons, nil
}
// Consumer method will either create a new consumer via the registerConsumer method, or it will
// simple return the already registered Consumer for the current Client.
func (c *Client) Consumer(topics []string, cfg rdkafka.ConfigMap) (*Consumer, error) {
if !c.consReady {
return nil, errors.New("Connection not ready. Identify yourself using `CLIENT SETNAME` first")
}
cid := ConsumerID(fmt.Sprintf("%s|%s", c.id, strings.Join(topics, ",")))
if c.consumer == nil {
cons, err := c.registerConsumer(cid, c.consGID, topics, cfg)
if err != nil {
return nil, fmt.Errorf("Failed to register Consumer '%s' for Client '%s'", cid, c.id)
}
c.consumer = cons
} else if cid != c.consumer.id {
return nil, fmt.Errorf("Client '%s' has Consumer '%s' registered (new consID: '%s')", c.id,
c.consumer.id, cid)
}
return c.consumer, nil
}
// Producer returns c's producer. If c does not have a producer assigned yet,
// a new one is created and assigned to it.
func (c *Client) Producer(cfg rdkafka.ConfigMap) (*Producer, error) {
var err error
if c.producer != nil {
return c.producer, nil
}
// reusing the same config between producers causes:
// fatal error: concurrent map read and map write
kafkaCfg := rdkafka.ConfigMap{}
for k, v := range cfg {
err := kafkaCfg.SetKey(k, v)
if err != nil {
c.log.Printf("Error configuring producer: %s", err)
}
}
c.producer, err = NewProducer(kafkaCfg)
if err != nil {
return nil, err
}
return c.producer, nil
}
// Close closes producers, consumers and the connection of c.
// Calling Close on an already closed client will result in a panic.
func (c *Client) Close() {
if c.consumer != nil {
c.consumer.cancel()
}
if c.producer != nil {
c.producer.Close()
}
close(c.monitorChan)
c.conn.Close()
}