-
Notifications
You must be signed in to change notification settings - Fork 5
/
taskpool.go
47 lines (41 loc) · 1.05 KB
/
taskpool.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//
//
// Tencent is pleased to support the open source community by making tRPC available.
//
// Copyright (C) 2023 THL A29 Limited, a Tencent company.
// All rights reserved.
//
// If you have downloaded a copy of the tRPC source code from Tencent,
// please note that tRPC source code is licensed under the Apache 2.0 License,
// A copy of the Apache 2.0 License is included in this file.
//
//
package tnet
import (
"github.com/panjf2000/ants/v2"
"trpc.group/trpc-go/tnet/metrics"
)
var (
maxRoutines = 0 // meaning INT32_MAX.
sysPool, _ = ants.NewPoolWithFunc(maxRoutines, taskHandler)
usrPool, _ = ants.NewPool(maxRoutines)
)
func taskHandler(v any) {
switch conn := v.(type) {
case *tcpconn:
tcpAsyncHandler(conn)
case *udpconn:
udpAsyncHandler(conn)
}
}
func doTask(args any) error {
metrics.Add(metrics.TaskAssigned, 1)
return sysPool.Invoke(args)
}
// Submit submits a task to usrPool.
//
// Users can use this API to submit a task to
// the default user business goroutine pool.
func Submit(task func()) error {
return usrPool.Submit(task)
}