mirror of https://github.com/docker/docs.git
Merge pull request #231 from chanwit/cleanup-zookeeper
Cleanup: zookeeper discovery's watcher
This commit is contained in:
commit
030c715007
|
@ -56,21 +56,30 @@ func (s *ZkDiscoveryService) Fetch() ([]*discovery.Node, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var nodes []*discovery.Node
|
return s.createNodes(addrs), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *ZkDiscoveryService) createNodes(addrs []string) (nodes []*discovery.Node) {
|
||||||
|
nodes = make([]*discovery.Node, 0)
|
||||||
|
if addrs == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
for _, addr := range addrs {
|
for _, addr := range addrs {
|
||||||
nodes = append(nodes, discovery.NewNode(addr))
|
nodes = append(nodes, discovery.NewNode(addr))
|
||||||
}
|
}
|
||||||
|
return
|
||||||
return nodes, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ZkDiscoveryService) Watch(callback discovery.WatchCallback) {
|
func (s *ZkDiscoveryService) Watch(callback discovery.WatchCallback) {
|
||||||
|
|
||||||
_, _, eventChan, err := s.conn.ChildrenW(s.path)
|
addrs, _, eventChan, err := s.conn.ChildrenW(s.path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Debugf("[ZK] Watch aborted")
|
log.Debugf("[ZK] Watch aborted")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
nodes := s.createNodes(addrs)
|
||||||
|
callback(nodes)
|
||||||
|
|
||||||
for e := range eventChan {
|
for e := range eventChan {
|
||||||
if e.Type == zk.EventNodeChildrenChanged {
|
if e.Type == zk.EventNodeChildrenChanged {
|
||||||
|
|
|
@ -3,14 +3,23 @@ package zookeeper
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/docker/swarm/discovery"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestInitialize(t *testing.T) {
|
func TestInitialize(t *testing.T) {
|
||||||
discovery := &ZkDiscoveryService{}
|
service := &ZkDiscoveryService{}
|
||||||
assert.Error(t, discovery.Initialize("127.0.0.1/path", 0))
|
assert.Error(t, service.Initialize("127.0.0.1/path", 0))
|
||||||
assert.Equal(t, discovery.path, "/path")
|
assert.Equal(t, service.path, "/path")
|
||||||
|
|
||||||
assert.Error(t, discovery.Initialize("127.0.0.1,127.0.0.2,127.0.0.3/path", 0))
|
assert.Error(t, service.Initialize("127.0.0.1,127.0.0.2,127.0.0.3/path", 0))
|
||||||
assert.Equal(t, discovery.path, "/path")
|
assert.Equal(t, service.path, "/path")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCreateNodes(t *testing.T) {
|
||||||
|
service := &ZkDiscoveryService{}
|
||||||
|
assert.Equal(t, service.createNodes(nil), []*discovery.Node{})
|
||||||
|
nodes := service.createNodes([]string{"127.0.0.1", "127.0.0.2"})
|
||||||
|
assert.Equal(t, nodes[0].String(), "http://127.0.0.1")
|
||||||
|
assert.Equal(t, nodes[1].String(), "http://127.0.0.2")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue