mirror of
https://github.com/hanzoai/tasks.git
synced 2026-10-04 02:04:03 +00:00
## What changed?
<!-- Describe what has changed in this PR -->
Remove license header from every file. Because it is really hard to
follow in this PR here is the summary:
1. License header is removed from all `.go` and `.proto` files
:fireworks::fireworks:🎆.
2. `LICENSE` file in the root directory has only Temporal and Uber
copyrights.
3. 5 other `LICENSE` files added to the packages which have copyrights
different from Temporal and Uber: Datadog, Xargin, "Mat Ryer, Tyler
Bunnell and contributors".
4. `license_file` flag is removed from all code generation tools.
5. `copyright_file` flag is removed from `go:generate mockgen`
directive.
6. All copyright related targets are removed from `Makefile`.
7. Updated Temporal copyright year to 2025 everywhere.
## Why?
<!-- Tell your future self why have you made these changes -->
I double checked with legal department that it is not needed to have
license header in every file. One file per repo is enough. I put all
copyrights to the root `LICENSE` file and removed header from all other
files. Also updated tools and `Makefile`.
139 lines
2.8 KiB
Go
139 lines
2.8 KiB
Go
package goro
|
|
|
|
import (
|
|
"math/rand"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
"go.temporal.io/server/common/clock"
|
|
)
|
|
|
|
type (
|
|
// AdaptivePool manages a pool of goroutines to handle small items of work.
|
|
// The size of the pool starts with minWorkers but can grow if needed, up to maxWorkers,
|
|
// and can shrink back down to minWorkers.
|
|
AdaptivePool struct {
|
|
ts clock.TimeSource
|
|
minWorkers int
|
|
maxWorkers int
|
|
targetDelay time.Duration
|
|
shrinkFactor float64
|
|
|
|
ch chan func()
|
|
stopCh chan struct{}
|
|
workers atomic.Int64
|
|
}
|
|
)
|
|
|
|
func NewAdaptivePool(
|
|
ts clock.TimeSource,
|
|
minWorkers int,
|
|
maxWorkers int,
|
|
targetDelay time.Duration,
|
|
shrinkFactor float64,
|
|
) *AdaptivePool {
|
|
p := &AdaptivePool{
|
|
ts: ts,
|
|
minWorkers: minWorkers,
|
|
maxWorkers: maxWorkers,
|
|
targetDelay: targetDelay,
|
|
shrinkFactor: shrinkFactor,
|
|
ch: make(chan func()),
|
|
stopCh: make(chan struct{}),
|
|
}
|
|
for i := 0; i < minWorkers; i++ {
|
|
go p.work()
|
|
}
|
|
p.workers.Store(int64(minWorkers))
|
|
return p
|
|
}
|
|
|
|
// Stops workers. Note that this does not wait for workers to exit.
|
|
// When Stop is called, concurrent calls to Do may or may not call their function, and future
|
|
// calls definitely won't.
|
|
func (p *AdaptivePool) Stop() {
|
|
close(p.stopCh)
|
|
}
|
|
|
|
// Do calls f() on a worker goroutine. If the call can't be started within targetDelay, it adds
|
|
// another worker. If Stop is called concurrently, Do may or may not call f. If Stop has been
|
|
// called already, Do does nothing.
|
|
func (p *AdaptivePool) Do(f func()) {
|
|
// try send first
|
|
select {
|
|
case p.ch <- f:
|
|
return
|
|
default:
|
|
}
|
|
|
|
// we might want to add a worker, send with timeout
|
|
have := p.workers.Load()
|
|
if have < int64(p.maxWorkers) {
|
|
timech, timer := p.ts.NewTimer(p.targetDelay)
|
|
select {
|
|
case <-p.stopCh:
|
|
timer.Stop()
|
|
return
|
|
case p.ch <- f:
|
|
timer.Stop()
|
|
return
|
|
case <-timech:
|
|
}
|
|
|
|
if p.workers.CompareAndSwap(have, have+1) {
|
|
go p.work()
|
|
}
|
|
}
|
|
|
|
// blocking send
|
|
select {
|
|
case p.ch <- f:
|
|
case <-p.stopCh:
|
|
}
|
|
}
|
|
|
|
func (p *AdaptivePool) work() {
|
|
for {
|
|
// try receive first
|
|
select {
|
|
case f := <-p.ch:
|
|
f()
|
|
continue
|
|
default:
|
|
}
|
|
|
|
have := p.workers.Load()
|
|
if have > int64(p.minWorkers) {
|
|
// we might want to exit, receive with timeout
|
|
// jitter this so we shrink slower than we grow
|
|
timech, timer := p.ts.NewTimer(time.Duration(float64(p.targetDelay) * p.shrinkFactor * rand.Float64()))
|
|
select {
|
|
case <-p.stopCh:
|
|
timer.Stop()
|
|
return
|
|
case f := <-p.ch:
|
|
timer.Stop()
|
|
f()
|
|
continue
|
|
case <-timech:
|
|
}
|
|
if p.workers.CompareAndSwap(have, have-1) {
|
|
return
|
|
}
|
|
}
|
|
|
|
// blocking receive
|
|
select {
|
|
case <-p.stopCh:
|
|
return
|
|
case f := <-p.ch:
|
|
f()
|
|
}
|
|
}
|
|
}
|
|
|
|
// NumWorkers returns the current number of workers. Probably only useful for testing.
|
|
func (p *AdaptivePool) NumWorkers() int {
|
|
return int(p.workers.Load())
|
|
}
|