Close kafka internal consumer&producer properly (#24998)

Signed-off-by: Congqi Xia <congqi.xia@zilliz.com>
pull/25047/head
congqixia 2023-06-20 21:14:41 +08:00 committed by GitHub
parent 5bd93f2432
commit 4fda4f3951
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 0 deletions

View File

@ -24,6 +24,7 @@ type Consumer struct {
chanOnce sync.Once
closeOnce sync.Once
closeCh chan struct{}
wg sync.WaitGroup
}
const timeout = 3000
@ -118,7 +119,9 @@ func (kc *Consumer) Chan() <-chan mqwrapper.Message {
panic("failed to chan a kafka consumer without assign")
}
kc.chanOnce.Do(func() {
kc.wg.Add(1)
go func() {
defer kc.wg.Done()
for {
select {
case <-kc.closeCh:
@ -248,5 +251,7 @@ func (kc *Consumer) CheckTopicValid(topic string) error {
func (kc *Consumer) Close() {
kc.closeOnce.Do(func() {
close(kc.closeCh)
kc.wg.Wait()
kc.c.Close()
})
}

View File

@ -67,5 +67,7 @@ func (kp *kafkaProducer) Close() {
if cost > 500 {
log.Debug("kafka producer is closed", zap.Any("topic", kp.topic), zap.Int64("time cost(ms)", cost))
}
kp.p.Close()
})
}