Commit 6936f128 by 杨树贤

fix

parent c0dcd970
......@@ -150,27 +150,10 @@ func CommonController(ctx *gin.Context) map[string]interface{} {
}
// 开启一个协程,等待所有任务完成,然后关闭channel
// 使用 context 来控制这个等待协程的生命周期
// 必须等所有 worker 协程退出后才能关闭 channel,否则 worker 向已关闭的 channel 发送会 panic
go func() {
// 创建一个 done channel 用于通知 wg.Wait() 完成
done := make(chan struct{})
go func() {
wg.Wait()
close(done)
}()
// 等待 wg.Wait() 完成或 context 取消
select {
case <-done:
// 所有协程都正常完成
close(ch)
case <-ctxWithTimeout.Done():
// context 超时,不再等待剩余协程
// 等待一小段时间让协程有机会退出
time.Sleep(300 * time.Millisecond)
close(ch)
logger.Log("等待协程完成超时,强制关闭channel", "sku", 1)
}
wg.Wait()
close(ch)
}()
//异步map最后转成map
......
......@@ -372,6 +372,12 @@ func (ls *LyService) LyGoodsDetail(ctx context.Context, params RequestParams, go
}
// 发送结果时也要检查 context,避免在超时后阻塞
// 使用 recover 防止向已关闭 channel 发送时 panic
defer func() {
if r := recover(); r != nil {
logger.Log("LyGoodsDetail: 发送结果时channel已关闭,忽略", "sku", 1)
}
}()
select {
case <-ctx.Done():
logger.Log("LyGoodsDetail: 发送结果前context已取消,直接返回", "sku", 1)
......
......@@ -414,6 +414,12 @@ func (qs *ZiyingService) ZyGoodsDetail(ctx context.Context, params RequestParams
(GoodsRes).Store(goodsId, A)
}
// 发送结果时也要检查 context,避免在超时后阻塞
// 使用 recover 防止向已关闭 channel 发送时 panic
defer func() {
if r := recover(); r != nil {
logger.Log("ZyGoodsDetail: 发送结果时channel已关闭,忽略", "sku", 1)
}
}()
select {
case <-ctx.Done():
logger.Log("ZyGoodsDetail: 发送结果前context已取消,直接返回", "sku", 1)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment