2012-05-24 22:02:44 +04:00
|
|
|
/*
|
|
|
|
Copyright (c) 2012, Matt T. Proud
|
|
|
|
All rights reserved.
|
2012-05-20 01:59:25 +04:00
|
|
|
|
2012-05-24 22:02:44 +04:00
|
|
|
Use of this source code is governed by a BSD-style
|
|
|
|
license that can be found in the LICENSE file.
|
|
|
|
*/
|
2012-05-20 01:59:25 +04:00
|
|
|
|
|
|
|
package metrics
|
|
|
|
|
|
|
|
import (
|
|
|
|
"container/heap"
|
2013-01-25 20:50:41 +04:00
|
|
|
"github.com/prometheus/client_golang/maths"
|
|
|
|
"github.com/prometheus/client_golang/utility"
|
2012-05-20 01:59:25 +04:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2012-05-24 22:02:44 +04:00
|
|
|
/*
|
|
|
|
EvictionPolicy implements some sort of garbage collection methodology for
|
|
|
|
an underlying heap.Interface. This is presently only used for
|
|
|
|
AccumulatingBucket.
|
|
|
|
*/
|
2012-05-20 01:59:25 +04:00
|
|
|
type EvictionPolicy func(h heap.Interface)
|
|
|
|
|
2012-05-24 22:02:44 +04:00
|
|
|
/*
|
|
|
|
As the name implies, this evicts the oldest x objects from the heap.
|
|
|
|
*/
|
2012-05-20 01:59:25 +04:00
|
|
|
func EvictOldest(count int) EvictionPolicy {
|
|
|
|
return func(h heap.Interface) {
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
heap.Pop(h)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-24 22:02:44 +04:00
|
|
|
/*
|
|
|
|
This factory produces an EvictionPolicy that applies some standardized
|
|
|
|
reduction methodology on the to-be-terminated values.
|
|
|
|
*/
|
2012-05-20 01:59:25 +04:00
|
|
|
func EvictAndReplaceWith(count int, reducer maths.ReductionMethod) EvictionPolicy {
|
|
|
|
return func(h heap.Interface) {
|
|
|
|
oldValues := make([]float64, count)
|
|
|
|
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
oldValues[i] = heap.Pop(h).(*utility.Item).Value.(float64)
|
|
|
|
}
|
|
|
|
|
|
|
|
reduced := reducer(oldValues)
|
|
|
|
|
|
|
|
heap.Push(h, &utility.Item{
|
2012-05-24 22:02:44 +04:00
|
|
|
Value: reduced,
|
|
|
|
/*
|
|
|
|
TODO(mtp): Parameterize the priority generation since these tools are useful.
|
|
|
|
*/
|
2012-05-20 01:59:25 +04:00
|
|
|
Priority: -1 * time.Now().UnixNano(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|