coredns/plugin/pkg/uniq/uniq.go
Francois Tur 05204ef142 Metrics registered on wrong prometheus registry ()
* - UT on metrics verifying that all plugins of all blocs have their metrics collectors declared

* - fix error msg

* - redirect Registry of metric to the one that handle the listener
- allow duplicate of metrics collector on the same Registry (case of same plugin in 2 blocs listening metrics on the same address)

* - fix change of signature

* - ensure cleaning metrics before starting the test (metrics collectors are global vars .. and re-used by several tests)

* - I think I fixed this test. Ensure correct mn of hits and clean metrics before test.

* - fix typo in error msg - proposed at review

* - fix typo in comment

* - remove ResetMetrics functions
- change a way to test the numeric metrics : get the diff between begin and end of test

* - oops. removing debug logs
2018-11-01 19:56:00 +00:00

51 lines
1.1 KiB
Go

// Package uniq keeps track of "thing" that are either "todo" or "done". Multiple
// identical events will only be processed once.
package uniq
// U keeps track of item to be done.
type U struct {
u map[string]item
}
type item struct {
state int // either todo or done
f func() error // function to be executed.
obj interface{} // any object to return when needed
}
// New returns a new initialized U.
func New() U { return U{u: make(map[string]item)} }
// Set sets function f in U under key. If the key already exists
// it is not overwritten.
func (u U) Set(key string, f func() error, o interface{}) interface{} {
if item, ok := u.u[key]; ok {
return item.obj
}
u.u[key] = item{todo, f, o}
return o
}
// Unset removes the key.
func (u U) Unset(key string) {
if _, ok := u.u[key]; ok {
delete(u.u, key)
}
}
// ForEach iterates for u executes f for each element that is 'todo' and sets it to 'done'.
func (u U) ForEach() error {
for k, v := range u.u {
if v.state == todo {
v.f()
}
v.state = done
u.u[k] = v
}
return nil
}
const (
todo = 1
done = 2
)