mirror of
https://github.com/coredns/coredns.git
synced 2025-10-27 00:04:15 -04:00
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:
@@ -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
|
||||
}
|
||||
|
||||
uniqAddr.setAddressTodo(m.Addr)
|
||||
uniqAddr.SetTodo(m.Addr)
|
||||
|
||||
m.ln.Close()
|
||||
m.lnSetup = false
|
||||
|
||||
@@ -8,19 +8,21 @@ import (
|
||||
"github.com/coredns/coredns/coremain"
|
||||
"github.com/coredns/coredns/plugin"
|
||||
clog "github.com/coredns/coredns/plugin/pkg/log"
|
||||
"github.com/coredns/coredns/plugin/pkg/uniq"
|
||||
|
||||
"github.com/mholt/caddy"
|
||||
)
|
||||
|
||||
var log = clog.NewWithPlugin("prometheus")
|
||||
var (
|
||||
log = clog.NewWithPlugin("prometheus")
|
||||
uniqAddr = uniq.New()
|
||||
)
|
||||
|
||||
func init() {
|
||||
caddy.RegisterPlugin("prometheus", caddy.Plugin{
|
||||
ServerType: "dns",
|
||||
Action: setup,
|
||||
})
|
||||
|
||||
uniqAddr = newAddress()
|
||||
}
|
||||
|
||||
func setup(c *caddy.Controller) error {
|
||||
@@ -36,7 +38,7 @@ func setup(c *caddy.Controller) error {
|
||||
|
||||
c.OncePerServerBlock(func() error {
|
||||
c.OnStartup(func() error {
|
||||
return uniqAddr.forEachTodo()
|
||||
return uniqAddr.ForEach()
|
||||
})
|
||||
return nil
|
||||
})
|
||||
@@ -54,7 +56,7 @@ func prometheusParse(c *caddy.Controller) (*Metrics, error) {
|
||||
var met = New(defaultAddr)
|
||||
|
||||
defer func() {
|
||||
uniqAddr.setAddress(met.Addr, met.OnStartup)
|
||||
uniqAddr.Set(met.Addr, met.OnStartup)
|
||||
}()
|
||||
|
||||
i := 0
|
||||
|
||||
Reference in New Issue
Block a user