Skip to content

Commit fbb5614

Browse files
committed
optimize concurrency
Signed-off-by: roc <[email protected]>
1 parent de3151c commit fbb5614

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

pkg/clb/batch.go

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func init() {
1616
}
1717
// RegisterTargets 一次性最大支持同时绑定 20 个 target: https://cloud.tencent.com/document/api/214/30676
1818
go startRegisterTargetsProccessor(20)
19-
go startCreateListenerProccessor(concurrency)
19+
go startCreateListenerProccessor(800)
2020
// DescribeListeners 一次性最大支持查 100 个监听器: https://cloud.tencent.com/document/api/214/30686
2121
go startDescribeListenerProccessor(100)
2222
// DescribeTargets 一次性最大支持同时查 20 个监听器: https://cloud.tencent.com/document/api/214/30684
@@ -40,7 +40,9 @@ type Task interface {
4040
GetRegion() string
4141
}
4242

43-
func StartBatchProccessor[T Task](maxAccumulatedTask int, apiName string, writeOp bool, taskChan chan T, doBatch func(region, lbId string, tasks []T)) {
43+
const maxAccumulatedTask = 800
44+
45+
func StartBatchProccessor[T Task](maxTaskOneBatch int, apiName string, writeOp bool, taskChan chan T, doBatch func(region, lbId string, tasks []T)) {
4446
tasks := []T{}
4547
timer := time.NewTimer(MaxBatchInternal)
4648
batchRequest := func() {
@@ -60,14 +62,19 @@ func StartBatchProccessor[T Task](maxAccumulatedTask int, apiName string, writeO
6062
// 将合并后的 task 通过 clb 的 BatchXXX 接口批量操作
6163
// TODO: 能否细化到部分成功的场景?
6264
for lb, tasks := range groupTasks {
63-
go func(region, lbId string, tasks []T) {
64-
if writeOp { // 写操作加实例锁
65-
mu := getLbLock(lbId)
66-
mu.Lock()
67-
defer mu.Unlock()
68-
}
69-
doBatch(region, lbId, tasks)
70-
}(lb.Region, lb.LbId, tasks)
65+
for len(tasks) > 0 {
66+
num := min(len(tasks), maxTaskOneBatch)
67+
t := tasks[:num]
68+
tasks = tasks[num:]
69+
go func(region, lbId string, tasks []T) {
70+
if writeOp { // 写操作加实例锁
71+
mu := getLbLock(lbId)
72+
mu.Lock()
73+
defer mu.Unlock()
74+
}
75+
doBatch(region, lbId, tasks)
76+
}(lb.Region, lb.LbId, t)
77+
}
7178
}
7279
}
7380
for {

0 commit comments

Comments
 (0)