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`.
115 lines
3.6 KiB
Go
115 lines
3.6 KiB
Go
package matching
|
|
|
|
import (
|
|
"sync"
|
|
"time"
|
|
|
|
"go.temporal.io/server/common/clock"
|
|
)
|
|
|
|
const (
|
|
// The duration of each mini-bucket in the circularTaskBuffer
|
|
intervalSize = 5
|
|
// The total duration which is used to calculate the rate of tasks added/dispatched
|
|
totalIntervalSize = 30
|
|
)
|
|
|
|
// a circular array of a fixed size for tracking tasks
|
|
type circularTaskBuffer struct {
|
|
buffer []int
|
|
currentPos int
|
|
}
|
|
|
|
func newCircularTaskBuffer(size int) circularTaskBuffer {
|
|
return circularTaskBuffer{
|
|
buffer: make([]int, size),
|
|
}
|
|
}
|
|
|
|
func (cb *circularTaskBuffer) incrementTaskCount() {
|
|
cb.buffer[cb.currentPos]++
|
|
}
|
|
|
|
func (cb *circularTaskBuffer) advance() {
|
|
cb.currentPos = (cb.currentPos + 1) % len(cb.buffer)
|
|
cb.buffer[cb.currentPos] = 0 // Reset the task count for the new interval
|
|
}
|
|
|
|
// returns the total number of tasks in the buffer
|
|
func (cb *circularTaskBuffer) totalTasks() int {
|
|
totalTasks := 0
|
|
for _, count := range cb.buffer {
|
|
totalTasks += count
|
|
}
|
|
return totalTasks
|
|
}
|
|
|
|
type taskTracker struct {
|
|
lock sync.Mutex
|
|
clock clock.TimeSource
|
|
startTime time.Time // time when taskTracker was initialized
|
|
bucketStartTime time.Time // the starting time of a bucket in the buffer
|
|
bucketSize time.Duration // the duration of each bucket in the buffer
|
|
numberOfBuckets int // the total number of buckets in the buffer
|
|
totalIntervalSize time.Duration // the number of seconds over which rate of tasks are added/dispatched
|
|
tasksInInterval circularTaskBuffer
|
|
}
|
|
|
|
func newTaskTracker(timeSource clock.TimeSource) *taskTracker {
|
|
return &taskTracker{
|
|
clock: timeSource,
|
|
startTime: timeSource.Now(),
|
|
bucketStartTime: timeSource.Now(),
|
|
bucketSize: time.Duration(intervalSize) * time.Second,
|
|
numberOfBuckets: (totalIntervalSize / intervalSize) + 1,
|
|
totalIntervalSize: time.Duration(totalIntervalSize) * time.Second,
|
|
tasksInInterval: newCircularTaskBuffer((totalIntervalSize / intervalSize) + 1),
|
|
}
|
|
}
|
|
|
|
// advanceAndResetTracker advances the trackers position and clears out any expired intervals
|
|
// This method must be called with taskTracker's lock held.
|
|
func (s *taskTracker) advanceAndResetTracker(elapsed time.Duration) {
|
|
// Calculate the number of intervals elapsed since the start interval time
|
|
intervalsElapsed := int(elapsed / s.bucketSize)
|
|
|
|
for i := 0; i < min(intervalsElapsed, s.numberOfBuckets); i++ {
|
|
s.tasksInInterval.advance() // advancing our circular buffer's position until we land on the right interval
|
|
}
|
|
s.bucketStartTime = s.bucketStartTime.Add(time.Duration(intervalsElapsed) * s.bucketSize)
|
|
}
|
|
|
|
// incrementTaskCount adds/removes tasks from the current time that falls in the appropriate interval
|
|
func (s *taskTracker) incrementTaskCount() {
|
|
s.lock.Lock()
|
|
defer s.lock.Unlock()
|
|
currentTime := s.clock.Now()
|
|
|
|
// Calculate elapsed time from the latest start interval time
|
|
elapsed := currentTime.Sub(s.bucketStartTime)
|
|
s.advanceAndResetTracker(elapsed)
|
|
s.tasksInInterval.incrementTaskCount()
|
|
}
|
|
|
|
// rate returns the rate of tasks added/dispatched in a given interval
|
|
func (s *taskTracker) rate() float32 {
|
|
s.lock.Lock()
|
|
defer s.lock.Unlock()
|
|
currentTime := s.clock.Now()
|
|
|
|
// Calculate elapsed time from the latest start interval time
|
|
elapsed := currentTime.Sub(s.bucketStartTime)
|
|
s.advanceAndResetTracker(elapsed)
|
|
totalTasks := s.tasksInInterval.totalTasks()
|
|
|
|
elapsedTime := min(currentTime.Sub(s.bucketStartTime)+s.totalIntervalSize,
|
|
currentTime.Sub(s.startTime))
|
|
|
|
if elapsedTime <= 0 {
|
|
return 0
|
|
}
|
|
|
|
// rate per second
|
|
return float32(totalTasks) / float32(elapsedTime.Seconds())
|
|
}
|