validate heartbeat in discovery cluster opts

Signed-off-by: allencloud <allen.sun@daocloud.io>
This commit is contained in:
allencloud 2016-09-01 01:07:05 +08:00
parent b42ab41b8f
commit 68fde86256
2 changed files with 24 additions and 0 deletions

View file

@ -56,6 +56,12 @@ func discoveryOpts(clusterOpts map[string]string) (time.Duration, time.Duration,
if err != nil {
return time.Duration(0), time.Duration(0), err
}
if h <= 0 {
return time.Duration(0), time.Duration(0),
fmt.Errorf("discovery.heartbeat must be positive")
}
heartbeat = time.Duration(h) * time.Second
ttl = defaultDiscoveryTTLFactor * heartbeat
}
@ -65,6 +71,12 @@ func discoveryOpts(clusterOpts map[string]string) (time.Duration, time.Duration,
if err != nil {
return time.Duration(0), time.Duration(0), err
}
if t <= 0 {
return time.Duration(0), time.Duration(0),
fmt.Errorf("discovery.ttl must be positive")
}
ttl = time.Duration(t) * time.Second
if _, ok := clusterOpts["discovery.heartbeat"]; !ok {

View file

@ -18,6 +18,18 @@ func TestDiscoveryOpts(t *testing.T) {
t.Fatalf("discovery.ttl == discovery.heartbeat must fail")
}
clusterOpts = map[string]string{"discovery.heartbeat": "-10", "discovery.ttl": "10"}
heartbeat, ttl, err = discoveryOpts(clusterOpts)
if err == nil {
t.Fatalf("negative discovery.heartbeat must fail")
}
clusterOpts = map[string]string{"discovery.heartbeat": "10", "discovery.ttl": "-10"}
heartbeat, ttl, err = discoveryOpts(clusterOpts)
if err == nil {
t.Fatalf("negative discovery.ttl must fail")
}
clusterOpts = map[string]string{"discovery.heartbeat": "invalid"}
heartbeat, ttl, err = discoveryOpts(clusterOpts)
if err == nil {