Skip to content

Commit

Permalink
Support customized logger
Browse files Browse the repository at this point in the history
  • Loading branch information
panjf2000 committed Mar 12, 2020
1 parent ef20703 commit e507ae3
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 80 deletions.
73 changes: 8 additions & 65 deletions ants.go
Expand Up @@ -24,7 +24,9 @@ package ants

import (
"errors"
"log"
"math"
"os"
"runtime"
"time"
)
Expand Down Expand Up @@ -81,75 +83,16 @@ var (
return 1
}()

defaultLogger = Logger(log.New(os.Stderr, "", log.LstdFlags))

// Init a instance pool when importing ants.
defaultAntsPool, _ = NewPool(DefaultAntsPoolSize)
)

// Option represents the optional function.
type Option func(opts *Options)

// Options contains all options which will be applied when instantiating a ants pool.
type Options struct {
// ExpiryDuration sets the expired time of every worker.
ExpiryDuration time.Duration

// PreAlloc indicates whether to make memory pre-allocation when initializing Pool.
PreAlloc bool

// Max number of goroutine blocking on pool.Submit.
// 0 (default value) means no such limit.
MaxBlockingTasks int

// When Nonblocking is true, Pool.Submit will never be blocked.
// ErrPoolOverload will be returned when Pool.Submit cannot be done at once.
// When Nonblocking is true, MaxBlockingTasks is inoperative.
Nonblocking bool

// PanicHandler is used to handle panics from each worker goroutine.
// if nil, panics will be thrown out again from worker goroutines.
PanicHandler func(interface{})
}

// WithOptions accepts the whole options config.
func WithOptions(options Options) Option {
return func(opts *Options) {
*opts = options
}
}

// WithExpiryDuration sets up the interval time of cleaning up goroutines.
func WithExpiryDuration(expiryDuration time.Duration) Option {
return func(opts *Options) {
opts.ExpiryDuration = expiryDuration
}
}

// WithPreAlloc indicates whether it should malloc for workers.
func WithPreAlloc(preAlloc bool) Option {
return func(opts *Options) {
opts.PreAlloc = preAlloc
}
}

// WithMaxBlockingTasks sets up the maximum number of goroutines that are blocked when it reaches the capacity of pool.
func WithMaxBlockingTasks(maxBlockingTasks int) Option {
return func(opts *Options) {
opts.MaxBlockingTasks = maxBlockingTasks
}
}

// WithNonblocking indicates that pool will return nil when there is no available workers.
func WithNonblocking(nonblocking bool) Option {
return func(opts *Options) {
opts.Nonblocking = nonblocking
}
}

// WithPanicHandler sets up panic handler.
func WithPanicHandler(panicHandler func(interface{})) Option {
return func(opts *Options) {
opts.PanicHandler = panicHandler
}
// Logger is used for logging formatted messages.
type Logger interface {
// Printf must have the same semantics as log.Printf.
Printf(format string, args ...interface{})
}

// Submit submits a task to pool.
Expand Down
4 changes: 3 additions & 1 deletion ants_test.go
Expand Up @@ -23,6 +23,8 @@
package ants

import (
"log"
"os"
"runtime"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -546,7 +548,7 @@ func TestRestCodeCoverage(t *testing.T) {
poolOpts, _ := NewPool(1, WithOptions(options))
t.Logf("Pool with options, capacity: %d", poolOpts.Cap())

p0, _ := NewPool(TestSize)
p0, _ := NewPool(TestSize, WithLogger(log.New(os.Stderr, "", log.LstdFlags)))
defer func() {
_ = p0.Submit(demoFunc)
}()
Expand Down
88 changes: 88 additions & 0 deletions options.go
@@ -0,0 +1,88 @@
package ants

import "time"

// Option represents the optional function.
type Option func(opts *Options)

func loadOptions(options ...Option) *Options {
opts := new(Options)
for _, option := range options {
option(opts)
}
return opts
}

// Options contains all options which will be applied when instantiating a ants pool.
type Options struct {
// ExpiryDuration sets the expired time of every worker.
ExpiryDuration time.Duration

// PreAlloc indicates whether to make memory pre-allocation when initializing Pool.
PreAlloc bool

// Max number of goroutine blocking on pool.Submit.
// 0 (default value) means no such limit.
MaxBlockingTasks int

// When Nonblocking is true, Pool.Submit will never be blocked.
// ErrPoolOverload will be returned when Pool.Submit cannot be done at once.
// When Nonblocking is true, MaxBlockingTasks is inoperative.
Nonblocking bool

// PanicHandler is used to handle panics from each worker goroutine.
// if nil, panics will be thrown out again from worker goroutines.
PanicHandler func(interface{})

// Logger is the customized logger for logging info, if it is not set, default standard logger from log package is used.
Logger Logger
}

// WithOptions accepts the whole options config.
func WithOptions(options Options) Option {
return func(opts *Options) {
*opts = options
}
}

// WithExpiryDuration sets up the interval time of cleaning up goroutines.
func WithExpiryDuration(expiryDuration time.Duration) Option {
return func(opts *Options) {
opts.ExpiryDuration = expiryDuration
}
}

// WithPreAlloc indicates whether it should malloc for workers.
func WithPreAlloc(preAlloc bool) Option {
return func(opts *Options) {
opts.PreAlloc = preAlloc
}
}

// WithMaxBlockingTasks sets up the maximum number of goroutines that are blocked when it reaches the capacity of pool.
func WithMaxBlockingTasks(maxBlockingTasks int) Option {
return func(opts *Options) {
opts.MaxBlockingTasks = maxBlockingTasks
}
}

// WithNonblocking indicates that pool will return nil when there is no available workers.
func WithNonblocking(nonblocking bool) Option {
return func(opts *Options) {
opts.Nonblocking = nonblocking
}
}

// WithPanicHandler sets up panic handler.
func WithPanicHandler(panicHandler func(interface{})) Option {
return func(opts *Options) {
opts.PanicHandler = panicHandler
}
}

// WithLogger sets up a customized logger.
func WithLogger(logger Logger) Option {
return func(opts *Options) {
opts.Logger = logger
}
}
9 changes: 5 additions & 4 deletions pool.go
Expand Up @@ -96,17 +96,18 @@ func NewPool(size int, options ...Option) (*Pool, error) {
return nil, ErrInvalidPoolSize
}

opts := new(Options)
for _, option := range options {
option(opts)
}
opts := loadOptions(options...)

if expiry := opts.ExpiryDuration; expiry < 0 {
return nil, ErrInvalidPoolExpiry
} else if expiry == 0 {
opts.ExpiryDuration = DefaultCleanIntervalTime
}

if opts.Logger == nil {
opts.Logger = defaultLogger
}

p := &Pool{
capacity: int32(size),
lock: internal.NewSpinLock(),
Expand Down
9 changes: 5 additions & 4 deletions pool_func.go
Expand Up @@ -117,17 +117,18 @@ func NewPoolWithFunc(size int, pf func(interface{}), options ...Option) (*PoolWi
return nil, ErrLackPoolFunc
}

opts := new(Options)
for _, option := range options {
option(opts)
}
opts := loadOptions(options...)

if expiry := opts.ExpiryDuration; expiry < 0 {
return nil, ErrInvalidPoolExpiry
} else if expiry == 0 {
opts.ExpiryDuration = DefaultCleanIntervalTime
}

if opts.Logger == nil {
opts.Logger = defaultLogger
}

p := &PoolWithFunc{
capacity: int32(size),
poolFunc: pf,
Expand Down
5 changes: 2 additions & 3 deletions worker.go
Expand Up @@ -23,7 +23,6 @@
package ants

import (
"log"
"runtime"
"time"
)
Expand Down Expand Up @@ -54,10 +53,10 @@ func (w *goWorker) run() {
if ph := w.pool.options.PanicHandler; ph != nil {
ph(p)
} else {
log.Printf("worker exits from a panic: %v\n", p)
w.pool.options.Logger.Printf("worker exits from a panic: %v\n", p)
var buf [4096]byte
n := runtime.Stack(buf[:], false)
log.Printf("worker exits from panic: %s\n", string(buf[:n]))
w.pool.options.Logger.Printf("worker exits from panic: %s\n", string(buf[:n]))
}
}
}()
Expand Down
5 changes: 2 additions & 3 deletions worker_func.go
Expand Up @@ -23,7 +23,6 @@
package ants

import (
"log"
"runtime"
"time"
)
Expand Down Expand Up @@ -54,10 +53,10 @@ func (w *goWorkerWithFunc) run() {
if ph := w.pool.options.PanicHandler; ph != nil {
ph(p)
} else {
log.Printf("worker with func exits from a panic: %v\n", p)
w.pool.options.Logger.Printf("worker with func exits from a panic: %v\n", p)
var buf [4096]byte
n := runtime.Stack(buf[:], false)
log.Printf("worker with func exits from panic: %s\n", string(buf[:n]))
w.pool.options.Logger.Printf("worker with func exits from panic: %s\n", string(buf[:n]))
}
}
}()
Expand Down

0 comments on commit e507ae3

Please sign in to comment.