Commit 710bbabc by hcy

sku管道

parent bf50e227
Showing with 18 additions and 14 deletions
......@@ -54,7 +54,7 @@ func CommonController(ctx *gin.Context) map[string]interface{} {
}
var wg sync.WaitGroup
ch := make(chan sync.Map) //管道
ch := make(chan sync.Map, 50) //管道
zyGoodsId := make([]string, 0, goodsSliceCount)
lyGoodsId := make([]string, 0, goodsSliceCount)
......@@ -73,13 +73,13 @@ func CommonController(ctx *gin.Context) map[string]interface{} {
common.PrintDebugHtml(ctx, zyGoodsId)
wg.Add(1) //协程计数一
// 必须创建一个新的slice副本,否则数据会被覆盖
idsToProcess := make([]string, len(zyGoodsId))
copy(idsToProcess, zyGoodsId)
go func() {
go func(ctx_in *gin.Context, goodsIds_in []string, chs_in chan sync.Map) {
defer wg.Done()
zyService.ZyGoodsDetail(ctx, idsToProcess, ch)
}()
zyService.ZyGoodsDetail(ctx_in, goodsIds_in, chs_in)
}(ctx, idsToProcess, ch)
zyGoodsId = zyGoodsId[:0]
}
} else { //联营
......@@ -88,12 +88,14 @@ func CommonController(ctx *gin.Context) map[string]interface{} {
common.PrintDebugHtml(ctx, "ly增加协程1002:")
common.PrintDebugHtml(ctx, lyGoodsId)
wg.Add(1)
idsToProcess := make([]string, len(lyGoodsId))
copy(idsToProcess, lyGoodsId)
go func() {
go func(ctx_in *gin.Context, goodsIds_in []string, chs_in chan sync.Map) {
defer wg.Done()
lyService.LyGoodsDetail(ctx, idsToProcess, ch)
}()
lyService.LyGoodsDetail(ctx_in, goodsIds_in, chs_in)
}(ctx, idsToProcess, ch)
lyGoodsId = lyGoodsId[:0]
}
}
......@@ -103,24 +105,26 @@ func CommonController(ctx *gin.Context) map[string]interface{} {
common.PrintDebugHtml(ctx, "zy增加协程1003:")
common.PrintDebugHtml(ctx, zyGoodsId)
wg.Add(1) //协程计数一
idsToProcess := make([]string, len(zyGoodsId))
copy(idsToProcess, zyGoodsId)
go func() {
go func(ctx_in *gin.Context, goodsIds_in []string, chs_in chan sync.Map) {
defer wg.Done()
zyService.ZyGoodsDetail(ctx, idsToProcess, ch)
}()
zyService.ZyGoodsDetail(ctx_in, goodsIds_in, chs_in)
}(ctx, zyGoodsId, ch)
}
if len(lyGoodsId) > 0 {
common.PrintDebugHtml(ctx, "ly增加协程1004:")
common.PrintDebugHtml(ctx, lyGoodsId)
wg.Add(1)
idsToProcess := make([]string, len(lyGoodsId))
copy(idsToProcess, lyGoodsId)
go func() {
go func(ctx_in *gin.Context, goodsIds_in []string, chs_in chan sync.Map) {
defer wg.Done()
lyService.LyGoodsDetail(ctx, idsToProcess, ch)
}()
lyService.LyGoodsDetail(ctx_in, goodsIds_in, chs_in)
}(ctx, idsToProcess, ch)
}
// 开启一个协程,等待所有任务完成,然后关闭channel
......
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