mirror of
https://github.com/hanzoai/tasks.git
synced 2026-11-03 03:56:04 +00:00
Full module path rebrand from Temporal to Hanzo Tasks: - go.mod module: github.com/hanzoai/tasks - All internal Go imports updated (1906 files) - cmd/server renamed to cmd/tasksd - Binary name: tasksd (was temporal-server) - Docker images: hanzoai/tasks (was temporalio/server) - README rewritten with Hanzo Tasks branding - LLM.md created with integration notes - Makefile, goreleaser, CI workflows updated - go.temporal.io/sdk and go.temporal.io/api imports preserved (separate repos) Fork of Temporal.io (MIT license).
105 lines
2.1 KiB
Go
105 lines
2.1 KiB
Go
package calculator
|
|
|
|
import (
|
|
"sync"
|
|
"sync/atomic"
|
|
|
|
"github.com/hanzoai/tasks/common/log"
|
|
"github.com/hanzoai/tasks/common/log/tag"
|
|
)
|
|
|
|
var (
|
|
_ Calculator = (*LoggedCalculator)(nil)
|
|
_ NamespaceCalculator = (*LoggedNamespaceCalculator)(nil)
|
|
)
|
|
|
|
type (
|
|
LoggedCalculator struct {
|
|
calculator Calculator
|
|
|
|
quotaLogger *quotaLogger[float64]
|
|
}
|
|
|
|
LoggedNamespaceCalculator struct {
|
|
calculator NamespaceCalculator
|
|
logger log.Logger
|
|
|
|
quotaLoggersLock sync.Mutex
|
|
quotaLoggers map[string]*quotaLogger[float64]
|
|
}
|
|
|
|
quotaLogger[T comparable] struct {
|
|
logger log.Logger
|
|
currentValue atomic.Value
|
|
}
|
|
)
|
|
|
|
func NewLoggedCalculator(
|
|
calculator Calculator,
|
|
logger log.Logger,
|
|
) *LoggedCalculator {
|
|
return &LoggedCalculator{
|
|
quotaLogger: newQuotaLogger(logger),
|
|
calculator: calculator,
|
|
}
|
|
}
|
|
|
|
func (c *LoggedCalculator) GetQuota() float64 {
|
|
quota := c.calculator.GetQuota()
|
|
c.quotaLogger.updateQuota(quota)
|
|
return quota
|
|
}
|
|
|
|
func NewLoggedNamespaceCalculator(
|
|
calculator NamespaceCalculator,
|
|
logger log.Logger,
|
|
) *LoggedNamespaceCalculator {
|
|
return &LoggedNamespaceCalculator{
|
|
calculator: calculator,
|
|
logger: logger,
|
|
quotaLoggers: make(map[string]*quotaLogger[float64]),
|
|
}
|
|
}
|
|
|
|
func (c *LoggedNamespaceCalculator) GetQuota(namespace string) float64 {
|
|
quota := c.calculator.GetQuota(namespace)
|
|
c.getOrCreateQuotaLogger(namespace).updateQuota(quota)
|
|
return quota
|
|
}
|
|
|
|
func (c *LoggedNamespaceCalculator) getOrCreateQuotaLogger(
|
|
namespace string,
|
|
) *quotaLogger[float64] {
|
|
c.quotaLoggersLock.Lock()
|
|
defer c.quotaLoggersLock.Unlock()
|
|
|
|
quotaLogger, ok := c.quotaLoggers[namespace]
|
|
if !ok {
|
|
quotaLogger = newQuotaLogger(log.With(c.logger, tag.WorkflowNamespace(namespace)))
|
|
c.quotaLoggers[namespace] = quotaLogger
|
|
}
|
|
|
|
return quotaLogger
|
|
}
|
|
|
|
func newQuotaLogger(
|
|
logger log.Logger,
|
|
) *quotaLogger[float64] {
|
|
return "aLogger[float64]{
|
|
logger: logger,
|
|
}
|
|
}
|
|
|
|
func (l *quotaLogger[T]) updateQuota(newQuota T) {
|
|
currentQuota := l.currentValue.Swap(newQuota)
|
|
|
|
if currentQuota != nil && newQuota == currentQuota.(T) {
|
|
return
|
|
}
|
|
|
|
l.logger.Info("Quota changed",
|
|
tag.Any("current-quota", currentQuota),
|
|
tag.Any("new-quota", newQuota),
|
|
)
|
|
}
|