AlexStocks commented on code in PR #992:
URL: https://github.com/apache/dubbo-go-pixiu/pull/992#discussion_r4505147258
##########
pkg/client/mq/kafka_facade.go:
##########
@@ -60,47 +65,85 @@ func NewKafkaConsumerFacade(config KafkaConsumerConfig,
consumerGroup string) (*
}
c.Version = version
}
- client, err := sarama.NewConsumerGroup(config.Brokers, consumerGroup, c)
+ return sarama.NewConsumerGroup(config.Brokers, consumerGroup, c)
+}
+
+// NewKafkaConsumerFacade creates a KafkaConsumerFacade backed by a real sarama
+// consumer group.
+func NewKafkaConsumerFacade(config KafkaConsumerConfig, consumerGroup string)
(*KafkaConsumerFacade, error) {
+ return newKafkaConsumerFacade(config, consumerGroup,
defaultConsumerGroupFactory)
+}
+
+// newKafkaConsumerFacade is the testable constructor: it accepts a consumer
+// group factory so the consumerManager initialization and shutdown behavior
can
+// be exercised without a live Kafka broker.
+func newKafkaConsumerFacade(config KafkaConsumerConfig, consumerGroup string,
factory consumerGroupFactory) (*KafkaConsumerFacade, error) {
+ client, err := factory(config, consumerGroup)
if err != nil {
return nil, err
}
- return &KafkaConsumerFacade{consumerGroup: client, httpClient:
&http.Client{Timeout: 5 * time.Second}, done: make(chan struct{})}, nil
+ return &KafkaConsumerFacade{
+ consumerGroup: client,
+ consumerManager: make(map[string]func()),
+ httpClient: &http.Client{Timeout: 5 * time.Second},
+ done: make(chan struct{}),
+ }, nil
}
type KafkaConsumerFacade struct {
consumerGroup sarama.ConsumerGroup
consumerManager map[string]func()
+ mu sync.RWMutex // protects consumerManager
httpClient *http.Client
wg sync.WaitGroup
done chan struct{}
+ stopOnce sync.Once
}
func (f *KafkaConsumerFacade) Subscribe(ctx context.Context, opts ...Option)
error {
cOpt := DefaultOptions()
cOpt.ApplyOpts(opts...)
+ // c is the cancellable child context shared by both the consume loop
and
+ // the health-check goroutine. The cancel func is stored in
consumerManager
+ // so either an explicit Stop() or an unhealthy-check can stop the
consume
+ // loop, not just the health check.
c, cancel := context.WithCancel(ctx)
key := GetConsumerManagerKey(cOpt.TopicList, cOpt.ConsumerGroup)
+ f.mu.Lock()
Review Comment:
🟡 **`Subscribe` 不是幂等的,重复订阅会互相踩 cancel。**
这里无条件覆盖 map 项:
```go
f.mu.Lock()
f.consumerManager[key] = cancel
f.mu.Unlock()
```
而 `removeConsumer(key)`(第 199 行)是「按 key 删除,不校验归属」。于是同 key 两次 `Subscribe` 之后:
1. 旧的一对 goroutine 先退出 → `removeConsumer(key)` 删掉的是**新订阅**的 cancel;
2. 新订阅的 `consumeLoop` 仍阻塞在 `ConsumerGroup.Consume(ctx)`,而 `Stop()` 已经遍历不到它的
cancel;
3. `Stop()` 里 `wg.Wait()` 无界等待 → 永久挂起。
也就是说,这个 PR 修掉的「Stop 挂死」在重复订阅场景下会换一种形态回来。当前 HTTP 入口用 `consumerFacadeMap` 按
consumerGroup 去重(`mq.go:126`),所以线上暂时不可达;但 `ConsumerFacade` 是导出接口,别处直接调用就会踩。
两种收法,任选其一:
```go
// 方案 A:重复订阅先停旧的
f.mu.Lock()
if prev, ok := f.consumerManager[key]; ok {
prev() // stop the previous subscription for the same key
}
f.consumerManager[key] = cancel
f.mu.Unlock()
```
```go
// 方案 B:给每条订阅一个身份,删的时候比对
type consumerEntry struct {
id uint64
cancel func()
}
// removeConsumer(key, id) 只删自己那条
```
如果决定「同 key 重复订阅属于未定义行为」,那至少在 `Subscribe` 里显式拒绝并返回错误——静默覆盖是最危险的选项。
##########
pkg/client/mq/kafka_facade.go:
##########
@@ -199,8 +260,23 @@ func (f *KafkaConsumerFacade) UnSubscribe(opts ...Option)
error {
}
func (f *KafkaConsumerFacade) Stop() {
- close(f.done)
+ f.stopOnce.Do(func() {
+ // Cancel any still-registered consumers before signaling
shutdown so the
+ // consume loops can exit their Consume() calls promptly.
+ f.mu.Lock()
+ for key, cancel := range f.consumerManager {
+ cancel()
+ delete(f.consumerManager, key)
+ }
+ f.mu.Unlock()
+
+ close(f.done)
+ })
+ // Wait for the consume loop and health check of every subscription to
exit.
f.wg.Wait()
+ if err := f.consumerGroup.Close(); err != nil {
Review Comment:
💭 `consumerGroup.Close()` 在 `stopOnce.Do` **之外**,所以第二次 `Stop()` 会再 `Close()`
一次。sarama 的 consumer group 内部有 `closeOnce` 兜底,实际不会出错;但这里有个更值得注意的顺序问题:
`Stop()` 现在先 cancel 所有订阅 → `close(f.done)` → `wg.Wait()` → 才 `Close()`
消费组。如果某个 `consumeLoop` 卡在 `ConsumerGroup.Consume()` 里且不响应 ctx(sarama 在 session
setup 阶段理论上有这个窗口),`wg.Wait()` 会一直等,`Close()` 永远执行不到——而 `Close()` 恰恰是那个能让卡住的
`Consume()` 返回的手段。可以考虑加一个上限:`wg.Wait()` 之前的某个超时分支里先 `Close()` 强制打断,再等 goroutine
收尾。
不改也行(现有超时由 sarama 自身负责),但值得在注释里说明「谁负责保证 Consume 一定会返回」,否则这段的顺序看起来像随意排列的。
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]