-
Notifications
You must be signed in to change notification settings - Fork 2
/
kop_produce.go
41 lines (39 loc) · 1.3 KB
/
kop_produce.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
package kop
import (
"fmt"
"github.com/protocol-laboratory/kafka-codec-go/codec"
"github.com/protocol-laboratory/kop-proxy-go/constant"
"github.com/sirupsen/logrus"
)
func (b *Broker) ReactProduce(ctx *NetworkContext, req *codec.ProduceReq, config *Config) (*codec.ProduceResp, error) {
if !b.checkSasl(ctx) {
return nil, fmt.Errorf("connection is not authed")
}
logrus.Debugf("produce req: %+v", req)
result := &codec.ProduceResp{
BaseResp: codec.BaseResp{
CorrelationId: req.CorrelationId,
},
TopicRespList: make([]*codec.ProduceTopicResp, len(req.TopicReqList)),
}
for i, topicReq := range req.TopicReqList {
if !b.checkSaslTopic(ctx, topicReq.Topic, constant.ProducerPermissionType) {
return nil, fmt.Errorf("check topic write permission failed: %v", topicReq.Topic)
}
f := &codec.ProduceTopicResp{
Topic: topicReq.Topic,
PartitionRespList: make([]*codec.ProducePartitionResp, 0),
}
for _, partitionReq := range topicReq.PartitionReqList {
partition, err := b.ProduceAction(ctx.Addr, topicReq.Topic, partitionReq.PartitionId, partitionReq)
if err != nil {
return nil, fmt.Errorf("produce failed: %v", err)
}
if partition != nil {
f.PartitionRespList = append(f.PartitionRespList, partition)
}
}
result.TopicRespList[i] = f
}
return result, nil
}