Skip to content

Commit

Permalink
opt: streamline pool implementation to reduce duplicated code
Browse files Browse the repository at this point in the history
  • Loading branch information
panjf2000 committed Jan 12, 2025
1 parent 4f33c6e commit 5763fad
Show file tree
Hide file tree
Showing 16 changed files with 476 additions and 777 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ import (

var sum int32

func myFunc(i interface{}) {
func myFunc(i any) {
n := i.(int32)
atomic.AddInt32(&sum, n)
fmt.Printf("run with %d\n", n)
Expand Down Expand Up @@ -110,7 +110,7 @@ func main() {

// Use the pool with a function,
// set 10 to the capacity of goroutine pool and 1 second for expired duration.
p, _ := ants.NewPoolWithFunc(10, func(i interface{}) {
p, _ := ants.NewPoolWithFunc(10, func(i any) {
myFunc(i)
wg.Done()
})
Expand Down Expand Up @@ -141,7 +141,7 @@ func main() {
fmt.Printf("finish all tasks.\n")

// Use the MultiPoolFunc and set the capacity of 10 goroutine pools to (runTimes/10).
mpf, _ := ants.NewMultiPoolWithFunc(10, runTimes/10, func(i interface{}) {
mpf, _ := ants.NewMultiPoolWithFunc(10, runTimes/10, func(i any) {
myFunc(i)
wg.Done()
}, ants.LeastTasks)
Expand Down Expand Up @@ -186,7 +186,7 @@ type Options struct {

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

// Logger is the customized logger for logging info, if it is not set,
// default standard logger from log package is used.
Expand Down Expand Up @@ -229,7 +229,7 @@ func WithNonblocking(nonblocking bool) Option {
}

// WithPanicHandler sets up panic handler.
func WithPanicHandler(panicHandler func(interface{})) Option {
func WithPanicHandler(panicHandler func(any)) Option {
return func(opts *Options) {
opts.PanicHandler = panicHandler
}
Expand Down
10 changes: 5 additions & 5 deletions README_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ import (

var sum int32

func myFunc(i interface{}) {
func myFunc(i any) {
n := i.(int32)
atomic.AddInt32(&sum, n)
fmt.Printf("run with %d\n", n)
Expand Down Expand Up @@ -110,7 +110,7 @@ func main() {

// Use the pool with a function,
// set 10 to the capacity of goroutine pool and 1 second for expired duration.
p, _ := ants.NewPoolWithFunc(10, func(i interface{}) {
p, _ := ants.NewPoolWithFunc(10, func(i any) {
myFunc(i)
wg.Done()
})
Expand Down Expand Up @@ -141,7 +141,7 @@ func main() {
fmt.Printf("finish all tasks.\n")

// Use the MultiPoolFunc and set the capacity of 10 goroutine pools to (runTimes/10).
mpf, _ := ants.NewMultiPoolWithFunc(10, runTimes/10, func(i interface{}) {
mpf, _ := ants.NewMultiPoolWithFunc(10, runTimes/10, func(i any) {
myFunc(i)
wg.Done()
}, ants.LeastTasks)
Expand Down Expand Up @@ -186,7 +186,7 @@ type Options struct {

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

// Logger is the customized logger for logging info, if it is not set,
// default standard logger from log package is used.
Expand Down Expand Up @@ -229,7 +229,7 @@ func WithNonblocking(nonblocking bool) Option {
}

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

0 comments on commit 5763fad

Please sign in to comment.