Skip to content

Commit 222a395

Browse files
committed
opt: set up the PanicHandler and Logger for pkg/pool/goroutine
1 parent 453e26b commit 222a395

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

pkg/pool/goroutine/goroutine.go

+19-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import (
1818
"time"
1919

2020
"github.com/panjf2000/ants/v2"
21+
22+
"github.com/panjf2000/gnet/v2/pkg/logging"
2123
)
2224

2325
const (
@@ -40,9 +42,25 @@ func init() {
4042
// Pool is the alias of ants.Pool.
4143
type Pool = ants.Pool
4244

45+
type antsLogger struct {
46+
logging.Logger
47+
}
48+
49+
// Printf implements the ants.Logger interface.
50+
func (l antsLogger) Printf(format string, args ...interface{}) {
51+
l.Infof(format, args...)
52+
}
53+
4354
// Default instantiates a non-blocking *WorkerPool with the capacity of DefaultAntsPoolSize.
4455
func Default() *Pool {
45-
options := ants.Options{ExpiryDuration: ExpiryDuration, Nonblocking: Nonblocking}
56+
options := ants.Options{
57+
ExpiryDuration: ExpiryDuration,
58+
Nonblocking: Nonblocking,
59+
Logger: &antsLogger{logging.GetDefaultLogger()},
60+
PanicHandler: func(i interface{}) {
61+
logging.Errorf("goroutine pool panic: %v", i)
62+
},
63+
}
4664
defaultAntsPool, _ := ants.NewPool(DefaultAntsPoolSize, ants.WithOptions(options))
4765
return defaultAntsPool
4866
}

0 commit comments

Comments
 (0)