coredns/plugin/pkg/uniq/uniq.go
Miek Gieben a1c97f82a6
plugin/ready: fix starts and restarts (#2814)
Add OnRestartFailed to the ready plugin and some various cleanups.

Document slightly better how things are supposed to work with multiple
`ready`'s in the multiple Server Blocks.

All manually tested with this Corefile:
~~~
. {
    log
    ready
}

example.org {
    log
    chaos
    ready
}
~~~
And then `kill -SIGUSR1` and curling the ready endpoint. This works
well, the FailedReload is triggered by adding a syntax error in the
Corefile.

See #2659

Signed-off-by: Miek Gieben <miek@miek.nl>
2019-06-09 08:10:15 +01:00

46 lines
967 B
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.
}
// 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}
}
// Unset removes the key.
func (u U) Unset(key string) {
delete(u.u, key)
}
// ForEach iterates over u and 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
)