client_golang/metrics/eviction.go

59 lines
1.3 KiB
Go
Raw Normal View History

/*
Copyright (c) 2012, Matt T. Proud
All rights reserved.
2012-05-20 01:59:25 +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"
"github.com/matttproud/golang_instrumentation/maths"
"github.com/matttproud/golang_instrumentation/utility"
"time"
)
/*
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)
/*
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)
}
}
}
/*
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{
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(),
})
}
}