goroutine和channel学习
实例代码
func doPatch(
ctx *gin.Context,
addrs []string,
request *graph_raw.SmartAmountListReq, txnCnt, rival *int64, level uint, sTime int64) ([]graph_raw.CapitalAddressEdge, error) {
wg := &sync.WaitGroup{}
mutex := sync.Mutex{}
res := make([]graph_raw.CapitalAddressEdge, 0)
limit := make(chan bool, 5)
for i := 0; i < len(addrs); i += 800 {
wg.Add(1)
start := i
go func() {
defer wg.Done()
limit <- true
group := collections.SliceSubRange(addrs, start, 800)
edgeList, _ := multiGetAddressData(ctx, group, request)
<-limit
mutex.Lock()
defer mutex.Unlock()
for _, v := range edgeList {
*txnCnt += int64(v.Count)
}
*rival += int64(len(edgeList))
slog.Infof(ctx, "req_level: %d, curr_level: %d, txnCnt : %d, rival: %d, stime: %d ", request.Level, level, *txnCnt, *rival, sTime)
res = append(res, edgeList...)
}()
cTime := time.Now().Unix()
timeout := int64(9 * 60)
totalTime := cTime - sTime
slog.Infof(ctx, "curr_time: %d, s_time: %d, total_time: %d", cTime, sTime, totalTime)
if totalTime >= timeout {
return res, serror.CustomErr("查询超时", serror.CustomErrCode)
}
}
wg.Wait()
return res, nil
}
这段代码使用gorouting、waitgroup、sync.Mutex、channel,来实现并发下安全控制,
func doPatch(
ctx *gin.Context,
addrs []string,
request *graph_raw.SmartAmountListReq, txnCnt, rival *int64, level uint, sTime int64) ([]graph_raw.CapitalAddressEdge, error) {
wg := &sync.WaitGroup{}
mutex := sync.Mutex{}
res := make([]graph_raw.CapitalAddressEdge, 0)
limit := make(chan bool, 5)
timeout := int64(9 * 60)
var err error
for i := 0; i < len(addrs); i += 800 {
wg.Add(1)
start := i
go func(start int) {
defer wg.Done()
limit <- true
if _, ok := ctx.Get("request_canceled"); ok {
slog.Infof(ctx, "request_canceled: %v", request)
err = serror.CustomErr("查询取消", serror.CustomErrCode)
return
}
cTime := time.Now().Unix()
totalTime := cTime - sTime
slog.Infof(ctx, "curr_time: %d, s_time: %d, total_time: %d, i: %d, level: %d", cTime, sTime, totalTime, start, level)
if totalTime >= timeout {
slog.Warnf(ctx, "smart超时: %v, curr_time: %d, s_time: %d, total_time: %d, i: %d, level: %d, req_level: %d, txnCnt : %d, rival: %d", request.Address, cTime, sTime, totalTime, start, level, request.Level, *txnCnt, *rival)
err = serror.CustomErr("查询超时", serror.CustomErrCode)
return
}
group := collections.SliceSubRange(addrs, start, 800)
edgeList, _ := multiGetAddressData(ctx, group, request)
<-limit
mutex.Lock()
defer mutex.Unlock()
for _, v := range edgeList {
*txnCnt += int64(v.Count)
}
*rival += int64(len(edgeList))
slog.Infof(ctx, "req_level: %d, curr_level: %d, txnCnt : %d, rival: %d, stime: %d ", request.Level, level, *txnCnt, *rival, sTime)
res = append(res, edgeList...)
}(start)
if err != nil {
return res, err
}
}
wg.Wait()
return res, err
}
新车新盘 嘎嘎稳 嘎嘎靠谱
新盘新项目,不再等待,现在就是最佳上车机会!
2025年10月新盘 做第一批吃螃蟹的人coinsrore.com
新车新盘 嘎嘎稳 嘎嘎靠谱coinsrore.com
新车首发,新的一年,只带想赚米的人coinsrore.com
新盘 上车集合 留下 我要发发 立马进裙coinsrore.com
做了几十年的项目 我总结了最好的一个盘(纯干货)coinsrore.com
新车上路,只带前10个人coinsrore.com
新盘首开 新盘首开 征召客户!!!coinsrore.com
新项目准备上线,寻找志同道合的合作伙伴coinsrore.com
新车即将上线 真正的项目,期待你的参与coinsrore.com
新盘新项目,不再等待,现在就是最佳上车机会!coinsrore.com
新盘新盘 这个月刚上新盘 新车第一个吃螃蟹!coinsrore.com
2025年10月新盘 做第一批吃螃蟹的人coinsrore.com
新车新盘 嘎嘎稳 嘎嘎靠谱coinsrore.com
新车首发,新的一年,只带想赚米的人coinsrore.com
新盘 上车集合 留下 我要发发 立马进裙coinsrore.com
做了几十年的项目 我总结了最好的一个盘(纯干货)coinsrore.com
新车上路,只带前10个人coinsrore.com
新盘首开 新盘首开 征召客户!!!coinsrore.com
新项目准备上线,寻找志同道合的合作伙伴coinsrore.com
新车即将上线 真正的项目,期待你的参与coinsrore.com
新盘新项目,不再等待,现在就是最佳上车机会!coinsrore.com
新盘新盘 这个月刚上新盘 新车第一个吃螃蟹!coinsrore.com