agent: Adding minimum check interval. Fixes #64.

This commit is contained in:
Armon Dadgar 2014-04-21 14:42:42 -07:00
parent 0513c3c0a1
commit b1bfd97438
3 changed files with 44 additions and 0 deletions

View File

@ -417,6 +417,11 @@ func (a *Agent) AddCheck(check *structs.HealthCheck, chkType *CheckType) error {
if _, ok := a.checkMonitors[check.CheckID]; ok { if _, ok := a.checkMonitors[check.CheckID]; ok {
return fmt.Errorf("CheckID is already registered") return fmt.Errorf("CheckID is already registered")
} }
if chkType.Interval < MinInterval {
a.logger.Println(fmt.Sprintf("[WARN] agent: check '%s' has interval below minimum of %v",
check.CheckID, MinInterval))
chkType.Interval = MinInterval
}
monitor := &CheckMonitor{ monitor := &CheckMonitor{
Notify: &a.state, Notify: &a.state,

View File

@ -205,6 +205,39 @@ func TestAgent_AddCheck(t *testing.T) {
} }
} }
func TestAgent_AddCheck_MinInterval(t *testing.T) {
dir, agent := makeAgent(t, nextConfig())
defer os.RemoveAll(dir)
defer agent.Shutdown()
health := &structs.HealthCheck{
Node: "foo",
CheckID: "mem",
Name: "memory util",
Status: structs.HealthUnknown,
}
chk := &CheckType{
Script: "exit 0",
Interval: time.Microsecond,
}
err := agent.AddCheck(health, chk)
if err != nil {
t.Fatalf("err: %v", err)
}
// Ensure we have a check mapping
if _, ok := agent.state.Checks()["mem"]; !ok {
t.Fatalf("missing mem check")
}
// Ensure a TTL is setup
if mon, ok := agent.checkMonitors["mem"]; !ok {
t.Fatalf("missing mem monitor")
} else if mon.Interval != MinInterval {
t.Fatalf("bad mem monitor interval")
}
}
func TestAgent_RemoveCheck(t *testing.T) { func TestAgent_RemoveCheck(t *testing.T) {
dir, agent := makeAgent(t, nextConfig()) dir, agent := makeAgent(t, nextConfig())
defer os.RemoveAll(dir) defer os.RemoveAll(dir)

View File

@ -12,6 +12,12 @@ import (
"time" "time"
) )
const (
// Do not allow for a interval below this value.
// Otherwise we risk fork bombing a system.
MinInterval = time.Second
)
// CheckType is used to create either the CheckMonitor // CheckType is used to create either the CheckMonitor
// or the CheckTTL. Only one of TTL or Script/Interval // or the CheckTTL. Only one of TTL or Script/Interval
// needs to be provided // needs to be provided