-
Notifications
You must be signed in to change notification settings - Fork 0
/
gopool.go
61 lines (51 loc) · 1.35 KB
/
gopool.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package routinepool
import (
"context"
"fmt"
"sync"
)
// defaultPool is the global default pool.
var defaultPool Pool
var poolMap sync.Map
func init() {
defaultPool = NewPool("gopool.DefaultPool", 10000, NewConfig())
}
// Go is an alternative to the go keyword, which is able to recover panic.
//
// gopool.Go(func(arg interface{}){
// ...
// }(nil))
func Go(f RoutineFunc) {
CtxGo(context.Background(), f)
}
// CtxGo is preferred than Go.
func CtxGo(ctx context.Context, f RoutineFunc) {
defaultPool.CtxGo(ctx, f)
}
// SetCap is not recommended to be called, this func changes the global pool's capacity which will affect other callers.
func SetCap(cap int32) {
defaultPool.SetCap(cap)
}
// SetPanicHandler sets the panic handler for the global pool.
func SetPanicHandler(f func(context.Context, error)) {
defaultPool.SetPanicHandler(f)
}
// RegisterPool registers a new pool to the global map.
// GetPool can be used to get the registered pool by name.
// returns error if the same name is registered.
func RegisterPool(p Pool) error {
_, loaded := poolMap.LoadOrStore(p.Name(), p)
if loaded {
return fmt.Errorf("name: %s already registered", p.Name())
}
return nil
}
// GetPool gets the registered pool by name.
// Returns nil if not registered.
func GetPool(name string) Pool {
p, ok := poolMap.Load(name)
if !ok {
return nil
}
return p.(Pool)
}