Merge pull request #588 from snrism/fix_heartbeat_type_and_val

Fix heartbeat type and only accept > 0 value
This commit is contained in:
Victor Vieux 2015-04-09 13:58:37 -07:00
commit 19641eb431
10 changed files with 28 additions and 17 deletions

View File

@ -7,5 +7,5 @@ type Options struct {
TLSConfig *tls.Config
OvercommitRatio float64
Discovery string
Heartbeat int
Heartbeat uint64
}

View File

@ -24,7 +24,7 @@ func init() {
}
// Initialize is exported
func (s *ConsulDiscoveryService) Initialize(uris string, heartbeat int) error {
func (s *ConsulDiscoveryService) Initialize(uris string, heartbeat uint64) error {
parts := strings.SplitN(uris, "/", 2)
if len(parts) < 2 {
return fmt.Errorf("invalid format %q, missing <path>", uris)

View File

@ -33,7 +33,7 @@ type WatchCallback func(entries []*Entry)
// DiscoveryService is exported
type DiscoveryService interface {
Initialize(string, int) error
Initialize(string, uint64) error
Fetch() ([]*Entry, error)
Watch(WatchCallback)
Register(string) error
@ -73,7 +73,7 @@ func parse(rawurl string) (string, string) {
}
// New is exported
func New(rawurl string, heartbeat int) (DiscoveryService, error) {
func New(rawurl string, heartbeat uint64) (DiscoveryService, error) {
scheme, uri := parse(rawurl)
if discovery, exists := discoveries[scheme]; exists {

View File

@ -22,7 +22,7 @@ func init() {
}
// Initialize is exported
func (s *EtcdDiscoveryService) Initialize(uris string, heartbeat int) error {
func (s *EtcdDiscoveryService) Initialize(uris string, heartbeat uint64) error {
var (
// split here because uris can contain multiples ips
// like `etcd://192.168.0.1,192.168.0.2,192.168.0.3/path`

View File

@ -10,7 +10,7 @@ import (
// FileDiscoveryService is exported
type FileDiscoveryService struct {
heartbeat int
heartbeat uint64
path string
}
@ -19,7 +19,7 @@ func init() {
}
// Initialize is exported
func (s *FileDiscoveryService) Initialize(path string, heartbeat int) error {
func (s *FileDiscoveryService) Initialize(path string, heartbeat uint64) error {
s.path = path
s.heartbeat = heartbeat
return nil

View File

@ -16,7 +16,7 @@ func init() {
}
// Initialize is exported
func (s *NodesDiscoveryService) Initialize(uris string, _ int) error {
func (s *NodesDiscoveryService) Initialize(uris string, _ uint64) error {
for _, input := range strings.Split(uris, ",") {
for _, ip := range discovery.Generate(input) {
entry, err := discovery.NewEntry(ip)

View File

@ -17,7 +17,7 @@ const DiscoveryURL = "https://discovery-stage.hub.docker.com/v1"
// TokenDiscoveryService is exported
type TokenDiscoveryService struct {
heartbeat int
heartbeat uint64
url string
token string
}
@ -27,7 +27,7 @@ func init() {
}
// Initialize is exported
func (s *TokenDiscoveryService) Initialize(urltoken string, heartbeat int) error {
func (s *TokenDiscoveryService) Initialize(urltoken string, heartbeat uint64) error {
if i := strings.LastIndex(urltoken, "/"); i != -1 {
s.url = "https://" + urltoken[:i]
s.token = urltoken[i+1:]

View File

@ -15,7 +15,7 @@ import (
type ZkDiscoveryService struct {
conn *zk.Conn
path []string
heartbeat int
heartbeat uint64
}
func init() {
@ -41,7 +41,7 @@ func (s *ZkDiscoveryService) createFullpath() error {
}
// Initialize is exported
func (s *ZkDiscoveryService) Initialize(uris string, heartbeat int) error {
func (s *ZkDiscoveryService) Initialize(uris string, heartbeat uint64) error {
var (
// split here because uris can contain multiples ips
// like `zk://192.168.0.1,192.168.0.2,192.168.0.3/path`

14
join.go
View File

@ -2,6 +2,7 @@ package main
import (
"regexp"
"strconv"
"time"
log "github.com/Sirupsen/logrus"
@ -20,7 +21,12 @@ func join(c *cli.Context) {
log.Fatalf("discovery required to join a cluster. See '%s join --help'.", c.App.Name)
}
d, err := discovery.New(dflag, c.Int("heartbeat"))
hb, err := strconv.ParseUint(c.String("heartbeat"), 0, 32)
if hb < 1 || err != nil {
log.Fatal("--heartbeat should be an unsigned integer and greater than 0")
}
d, err := discovery.New(dflag, hb)
if err != nil {
log.Fatal(err)
}
@ -35,10 +41,10 @@ func join(c *cli.Context) {
log.Fatal(err)
}
hb := time.Duration(c.Int("heartbeat"))
hbval := time.Duration(hb)
for {
log.WithFields(log.Fields{"addr": addr, "discovery": dflag}).Infof("Registering on the discovery service every %d seconds...", hb)
time.Sleep(hb * time.Second)
log.WithFields(log.Fields{"addr": addr, "discovery": dflag}).Infof("Registering on the discovery service every %d seconds...", hbval)
time.Sleep(hbval * time.Second)
if err := d.Register(addr); err != nil {
log.Error(err)
}

View File

@ -6,6 +6,7 @@ import (
"fmt"
"io/ioutil"
"path"
"strconv"
log "github.com/Sirupsen/logrus"
"github.com/codegangsta/cli"
@ -120,11 +121,15 @@ func manage(c *cli.Context) {
sched := scheduler.New(s, fs)
eventsHandler := api.NewEventsHandler()
hb, err := strconv.ParseUint(c.String("heartbeat"), 0, 32)
if hb < 1 || err != nil {
log.Fatal("--heartbeat should be an unsigned integer and greater than 0")
}
options := &cluster.Options{
TLSConfig: tlsConfig,
OvercommitRatio: c.Float64("overcommit"),
Discovery: dflag,
Heartbeat: c.Int("heartbeat"),
Heartbeat: hb,
}
cluster := swarm.NewCluster(sched, store, eventsHandler, options)