plugin/pkg/uniq: add (#1733)
Spin this out the metrics package so we can use it in the health one of well to fix some reload bugs.
This commit is contained in:
parent
ce084012df
commit
5e6114b797
6 changed files with 77 additions and 75 deletions
|
@ -1,52 +0,0 @@
|
||||||
package metrics
|
|
||||||
|
|
||||||
// addrs keeps track on which addrs we listen, so we only start one listener, is
|
|
||||||
// prometheus is used in multiple Server Blocks.
|
|
||||||
type addrs struct {
|
|
||||||
a map[string]value
|
|
||||||
}
|
|
||||||
|
|
||||||
type value struct {
|
|
||||||
state int
|
|
||||||
f func() error
|
|
||||||
}
|
|
||||||
|
|
||||||
var uniqAddr addrs
|
|
||||||
|
|
||||||
func newAddress() addrs {
|
|
||||||
return addrs{a: make(map[string]value)}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a addrs) setAddress(addr string, f func() error) {
|
|
||||||
if a.a[addr].state == done {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
a.a[addr] = value{todo, f}
|
|
||||||
}
|
|
||||||
|
|
||||||
// setAddressTodo sets addr to 'todo' again.
|
|
||||||
func (a addrs) setAddressTodo(addr string) {
|
|
||||||
v, ok := a.a[addr]
|
|
||||||
if !ok {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
v.state = todo
|
|
||||||
a.a[addr] = v
|
|
||||||
}
|
|
||||||
|
|
||||||
// forEachTodo iterates for a and executes f for each element that is 'todo' and sets it to 'done'.
|
|
||||||
func (a addrs) forEachTodo() error {
|
|
||||||
for k, v := range a.a {
|
|
||||||
if v.state == todo {
|
|
||||||
v.f()
|
|
||||||
}
|
|
||||||
v.state = done
|
|
||||||
a.a[k] = v
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
const (
|
|
||||||
todo = 1
|
|
||||||
done = 2
|
|
||||||
)
|
|
|
@ -1,17 +0,0 @@
|
||||||
package metrics
|
|
||||||
|
|
||||||
import "testing"
|
|
||||||
|
|
||||||
func TestForEachTodo(t *testing.T) {
|
|
||||||
a, i := newAddress(), 0
|
|
||||||
a.setAddress("test", func() error { i++; return nil })
|
|
||||||
|
|
||||||
a.forEachTodo()
|
|
||||||
if i != 1 {
|
|
||||||
t.Errorf("Failed to executed f for %s", "test")
|
|
||||||
}
|
|
||||||
a.forEachTodo()
|
|
||||||
if i != 1 {
|
|
||||||
t.Errorf("Executed f twice instead of once")
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -106,7 +106,7 @@ func (m *Metrics) OnRestart() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
uniqAddr.setAddressTodo(m.Addr)
|
uniqAddr.SetTodo(m.Addr)
|
||||||
|
|
||||||
m.ln.Close()
|
m.ln.Close()
|
||||||
m.lnSetup = false
|
m.lnSetup = false
|
||||||
|
|
|
@ -8,19 +8,21 @@ import (
|
||||||
"github.com/coredns/coredns/coremain"
|
"github.com/coredns/coredns/coremain"
|
||||||
"github.com/coredns/coredns/plugin"
|
"github.com/coredns/coredns/plugin"
|
||||||
clog "github.com/coredns/coredns/plugin/pkg/log"
|
clog "github.com/coredns/coredns/plugin/pkg/log"
|
||||||
|
"github.com/coredns/coredns/plugin/pkg/uniq"
|
||||||
|
|
||||||
"github.com/mholt/caddy"
|
"github.com/mholt/caddy"
|
||||||
)
|
)
|
||||||
|
|
||||||
var log = clog.NewWithPlugin("prometheus")
|
var (
|
||||||
|
log = clog.NewWithPlugin("prometheus")
|
||||||
|
uniqAddr = uniq.New()
|
||||||
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
caddy.RegisterPlugin("prometheus", caddy.Plugin{
|
caddy.RegisterPlugin("prometheus", caddy.Plugin{
|
||||||
ServerType: "dns",
|
ServerType: "dns",
|
||||||
Action: setup,
|
Action: setup,
|
||||||
})
|
})
|
||||||
|
|
||||||
uniqAddr = newAddress()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func setup(c *caddy.Controller) error {
|
func setup(c *caddy.Controller) error {
|
||||||
|
@ -36,7 +38,7 @@ func setup(c *caddy.Controller) error {
|
||||||
|
|
||||||
c.OncePerServerBlock(func() error {
|
c.OncePerServerBlock(func() error {
|
||||||
c.OnStartup(func() error {
|
c.OnStartup(func() error {
|
||||||
return uniqAddr.forEachTodo()
|
return uniqAddr.ForEach()
|
||||||
})
|
})
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
@ -54,7 +56,7 @@ func prometheusParse(c *caddy.Controller) (*Metrics, error) {
|
||||||
var met = New(defaultAddr)
|
var met = New(defaultAddr)
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
uniqAddr.setAddress(met.Addr, met.OnStartup)
|
uniqAddr.Set(met.Addr, met.OnStartup)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
i := 0
|
i := 0
|
||||||
|
|
52
plugin/pkg/uniq/uniq.go
Normal file
52
plugin/pkg/uniq/uniq.go
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
// 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.
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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) {
|
||||||
|
if _, ok := u.u[key]; ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
u.u[key] = item{todo, f}
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetTodo sets key to 'todo' again.
|
||||||
|
func (u U) SetTodo(key string) {
|
||||||
|
v, ok := u.u[key]
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
v.state = todo
|
||||||
|
u.u[key] = v
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
)
|
17
plugin/pkg/uniq/uniq_test.go
Normal file
17
plugin/pkg/uniq/uniq_test.go
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
package uniq
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestForEach(t *testing.T) {
|
||||||
|
u, i := New(), 0
|
||||||
|
u.Set("test", func() error { i++; return nil })
|
||||||
|
|
||||||
|
u.ForEach()
|
||||||
|
if i != 1 {
|
||||||
|
t.Errorf("Failed to executed f for %s", "test")
|
||||||
|
}
|
||||||
|
u.ForEach()
|
||||||
|
if i != 1 {
|
||||||
|
t.Errorf("Executed f twice instead of once")
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue