2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2022-12-16 21:49:26 +00:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
2023-05-05 14:38:22 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/consul/controller/queue"
|
2022-12-16 21:49:26 +00:00
|
|
|
)
|
|
|
|
|
2023-05-05 14:38:22 +00:00
|
|
|
type countingWorkQueue[T queue.ItemType] struct {
|
2022-12-16 21:49:26 +00:00
|
|
|
getCounter uint64
|
|
|
|
addCounter uint64
|
|
|
|
addAfterCounter uint64
|
|
|
|
addRateLimitedCounter uint64
|
|
|
|
forgetCounter uint64
|
|
|
|
doneCounter uint64
|
|
|
|
|
2023-05-05 14:38:22 +00:00
|
|
|
inner queue.WorkQueue[T]
|
2022-12-16 21:49:26 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 14:38:22 +00:00
|
|
|
func newCountingWorkQueue[T queue.ItemType](inner queue.WorkQueue[T]) *countingWorkQueue[T] {
|
|
|
|
return &countingWorkQueue[T]{
|
2022-12-16 21:49:26 +00:00
|
|
|
inner: inner,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-05 14:38:22 +00:00
|
|
|
func (c *countingWorkQueue[T]) reset() {
|
2022-12-16 21:49:26 +00:00
|
|
|
atomic.StoreUint64(&c.getCounter, 0)
|
|
|
|
atomic.StoreUint64(&c.addCounter, 0)
|
|
|
|
atomic.StoreUint64(&c.addAfterCounter, 0)
|
|
|
|
atomic.StoreUint64(&c.addRateLimitedCounter, 0)
|
|
|
|
atomic.StoreUint64(&c.forgetCounter, 0)
|
|
|
|
atomic.StoreUint64(&c.doneCounter, 0)
|
|
|
|
}
|
|
|
|
|
2023-05-05 14:38:22 +00:00
|
|
|
func (c *countingWorkQueue[T]) requeues() uint64 {
|
2022-12-16 21:49:26 +00:00
|
|
|
return c.addAfters() + c.addRateLimiteds()
|
|
|
|
}
|
|
|
|
|
2023-05-05 14:38:22 +00:00
|
|
|
func (c *countingWorkQueue[T]) Get() (item T, shutdown bool) {
|
2022-12-16 21:49:26 +00:00
|
|
|
item, err := c.inner.Get()
|
|
|
|
atomic.AddUint64(&c.getCounter, 1)
|
|
|
|
return item, err
|
|
|
|
}
|
|
|
|
|
2023-05-05 14:38:22 +00:00
|
|
|
func (c *countingWorkQueue[T]) gets() uint64 {
|
2022-12-16 21:49:26 +00:00
|
|
|
return atomic.LoadUint64(&c.getCounter)
|
|
|
|
}
|
|
|
|
|
2023-05-05 14:38:22 +00:00
|
|
|
func (c *countingWorkQueue[T]) Add(item T) {
|
2022-12-16 21:49:26 +00:00
|
|
|
c.inner.Add(item)
|
|
|
|
atomic.AddUint64(&c.addCounter, 1)
|
|
|
|
}
|
|
|
|
|
2023-05-05 14:38:22 +00:00
|
|
|
func (c *countingWorkQueue[T]) adds() uint64 {
|
2022-12-16 21:49:26 +00:00
|
|
|
return atomic.LoadUint64(&c.addCounter)
|
|
|
|
}
|
|
|
|
|
2023-05-05 14:38:22 +00:00
|
|
|
func (c *countingWorkQueue[T]) AddAfter(item T, duration time.Duration) {
|
2022-12-16 21:49:26 +00:00
|
|
|
c.inner.AddAfter(item, duration)
|
|
|
|
atomic.AddUint64(&c.addAfterCounter, 1)
|
|
|
|
}
|
|
|
|
|
2023-05-05 14:38:22 +00:00
|
|
|
func (c *countingWorkQueue[T]) addAfters() uint64 {
|
2022-12-16 21:49:26 +00:00
|
|
|
return atomic.LoadUint64(&c.addAfterCounter)
|
|
|
|
}
|
|
|
|
|
2023-05-05 14:38:22 +00:00
|
|
|
func (c *countingWorkQueue[T]) AddRateLimited(item T) {
|
2022-12-16 21:49:26 +00:00
|
|
|
c.inner.AddRateLimited(item)
|
|
|
|
atomic.AddUint64(&c.addRateLimitedCounter, 1)
|
|
|
|
}
|
|
|
|
|
2023-05-05 14:38:22 +00:00
|
|
|
func (c *countingWorkQueue[T]) addRateLimiteds() uint64 {
|
2022-12-16 21:49:26 +00:00
|
|
|
return atomic.LoadUint64(&c.addRateLimitedCounter)
|
|
|
|
}
|
|
|
|
|
2023-05-05 14:38:22 +00:00
|
|
|
func (c *countingWorkQueue[T]) Forget(item T) {
|
2022-12-16 21:49:26 +00:00
|
|
|
c.inner.Forget(item)
|
|
|
|
atomic.AddUint64(&c.forgetCounter, 1)
|
|
|
|
}
|
|
|
|
|
2023-05-05 14:38:22 +00:00
|
|
|
func (c *countingWorkQueue[T]) forgets() uint64 {
|
2022-12-16 21:49:26 +00:00
|
|
|
return atomic.LoadUint64(&c.forgetCounter)
|
|
|
|
}
|
|
|
|
|
2023-05-05 14:38:22 +00:00
|
|
|
func (c *countingWorkQueue[T]) Done(item T) {
|
2022-12-16 21:49:26 +00:00
|
|
|
c.inner.Done(item)
|
|
|
|
atomic.AddUint64(&c.doneCounter, 1)
|
|
|
|
}
|
|
|
|
|
2023-05-05 14:38:22 +00:00
|
|
|
func (c *countingWorkQueue[T]) dones() uint64 {
|
2022-12-16 21:49:26 +00:00
|
|
|
return atomic.LoadUint64(&c.doneCounter)
|
|
|
|
}
|