Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
黄成意
/
go_sku_server
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
6936f128
authored
May 18, 2026
by
杨树贤
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
fix
parent
c0dcd970
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
15 additions
and
20 deletions
controller/sku_controller.go
service/service_ly.go
service/service_zy.go
controller/sku_controller.go
View file @
6936f128
...
@@ -150,27 +150,10 @@ func CommonController(ctx *gin.Context) map[string]interface{} {
...
@@ -150,27 +150,10 @@ func CommonController(ctx *gin.Context) map[string]interface{} {
}
}
// 开启一个协程,等待所有任务完成,然后关闭channel
// 开启一个协程,等待所有任务完成,然后关闭channel
//
使用 context 来控制这个等待协程的生命周期
//
必须等所有 worker 协程退出后才能关闭 channel,否则 worker 向已关闭的 channel 发送会 panic
go
func
()
{
go
func
()
{
// 创建一个 done channel 用于通知 wg.Wait() 完成
wg
.
Wait
()
done
:=
make
(
chan
struct
{})
close
(
ch
)
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
)
}
}()
}()
//异步map最后转成map
//异步map最后转成map
...
...
service/service_ly.go
View file @
6936f128
...
@@ -372,6 +372,12 @@ func (ls *LyService) LyGoodsDetail(ctx context.Context, params RequestParams, go
...
@@ -372,6 +372,12 @@ func (ls *LyService) LyGoodsDetail(ctx context.Context, params RequestParams, go
}
}
// 发送结果时也要检查 context,避免在超时后阻塞
// 发送结果时也要检查 context,避免在超时后阻塞
// 使用 recover 防止向已关闭 channel 发送时 panic
defer
func
()
{
if
r
:=
recover
();
r
!=
nil
{
logger
.
Log
(
"LyGoodsDetail: 发送结果时channel已关闭,忽略"
,
"sku"
,
1
)
}
}()
select
{
select
{
case
<-
ctx
.
Done
()
:
case
<-
ctx
.
Done
()
:
logger
.
Log
(
"LyGoodsDetail: 发送结果前context已取消,直接返回"
,
"sku"
,
1
)
logger
.
Log
(
"LyGoodsDetail: 发送结果前context已取消,直接返回"
,
"sku"
,
1
)
...
...
service/service_zy.go
View file @
6936f128
...
@@ -414,6 +414,12 @@ func (qs *ZiyingService) ZyGoodsDetail(ctx context.Context, params RequestParams
...
@@ -414,6 +414,12 @@ func (qs *ZiyingService) ZyGoodsDetail(ctx context.Context, params RequestParams
(
GoodsRes
)
.
Store
(
goodsId
,
A
)
(
GoodsRes
)
.
Store
(
goodsId
,
A
)
}
}
// 发送结果时也要检查 context,避免在超时后阻塞
// 发送结果时也要检查 context,避免在超时后阻塞
// 使用 recover 防止向已关闭 channel 发送时 panic
defer
func
()
{
if
r
:=
recover
();
r
!=
nil
{
logger
.
Log
(
"ZyGoodsDetail: 发送结果时channel已关闭,忽略"
,
"sku"
,
1
)
}
}()
select
{
select
{
case
<-
ctx
.
Done
()
:
case
<-
ctx
.
Done
()
:
logger
.
Log
(
"ZyGoodsDetail: 发送结果前context已取消,直接返回"
,
"sku"
,
1
)
logger
.
Log
(
"ZyGoodsDetail: 发送结果前context已取消,直接返回"
,
"sku"
,
1
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment