Skip to content

Commit

Permalink
feat: implement generic pool (#351)
Browse files Browse the repository at this point in the history
  • Loading branch information
panjf2000 authored Jan 12, 2025
1 parent 9a1446b commit 60bd4c4
Show file tree
Hide file tree
Showing 16 changed files with 1,136 additions and 316 deletions.
4 changes: 4 additions & 0 deletions ants.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ var (
// ErrInvalidLoadBalancingStrategy will be returned when trying to create a MultiPool with an invalid load-balancing strategy.
ErrInvalidLoadBalancingStrategy = errors.New("invalid load-balancing strategy")

// ErrInvalidMultiPoolSize will be returned when trying to create a MultiPool with an invalid size.
ErrInvalidMultiPoolSize = errors.New("invalid size for multiple pool")

// workerChanCap determines whether the channel of a worker should be a buffered channel
// to get the best performance. Inspired by fasthttp at
// https://github.com/valyala/fasthttp/blob/master/workerpool.go#L139
Expand Down Expand Up @@ -387,6 +390,7 @@ func (p *poolCommon) Release() {
p.lock.Lock()
p.workers.reset()
p.lock.Unlock()

// There might be some callers waiting in retrieveWorker(), so we need to wake them up to prevent
// those callers blocking infinitely.
p.cond.Broadcast()
Expand Down
18 changes: 9 additions & 9 deletions ants_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ func demoPoolFunc(args any) {
time.Sleep(time.Duration(n) * time.Millisecond)
}

func demoPoolFuncInt(n int) {
time.Sleep(time.Duration(n) * time.Millisecond)
}

var stopLongRunningFunc int32

func longRunningFunc() {
Expand All @@ -56,16 +60,12 @@ func longRunningFunc() {
}
}

var stopLongRunningPoolFunc int32

func longRunningPoolFunc(arg any) {
if ch, ok := arg.(chan struct{}); ok {
<-ch
return
}
for atomic.LoadInt32(&stopLongRunningPoolFunc) == 0 {
runtime.Gosched()
}
<-arg.(chan struct{})
}

func longRunningPoolFuncCh(ch chan struct{}) {
<-ch
}

func BenchmarkGoroutines(b *testing.B) {
Expand Down
Loading

0 comments on commit 60bd4c4

Please sign in to comment.