-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
113 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters