Commit bf50e227 by hcy

sku管道

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