Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement generic pool #351

Merged
merged 2 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading