Skip to content

Commit

Permalink
opt: set up the PanicHandler and Logger for pkg/pool/goroutine
Browse files Browse the repository at this point in the history
  • Loading branch information
panjf2000 committed May 18, 2023
1 parent 453e26b commit 222a395
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion pkg/pool/goroutine/goroutine.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"time"

"github.com/panjf2000/ants/v2"

"github.com/panjf2000/gnet/v2/pkg/logging"
)

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

type antsLogger struct {
logging.Logger
}

// Printf implements the ants.Logger interface.
func (l antsLogger) Printf(format string, args ...interface{}) {
l.Infof(format, args...)
}

// Default instantiates a non-blocking *WorkerPool with the capacity of DefaultAntsPoolSize.
func Default() *Pool {
options := ants.Options{ExpiryDuration: ExpiryDuration, Nonblocking: Nonblocking}
options := ants.Options{
ExpiryDuration: ExpiryDuration,
Nonblocking: Nonblocking,
Logger: &antsLogger{logging.GetDefaultLogger()},
PanicHandler: func(i interface{}) {
logging.Errorf("goroutine pool panic: %v", i)
},
}
defaultAntsPool, _ := ants.NewPool(DefaultAntsPoolSize, ants.WithOptions(options))
return defaultAntsPool
}

0 comments on commit 222a395

Please sign in to comment.