Remove prefixes from DiscoveryService implementations

All of these structs are public. Previously when they were used, they'd
look like the following:

 - `consul.ConsulDiscoveryService`
 - `etcd.EtcdDiscoveryService`
 - `file.FileDiscoveryService`
 - `nodes.NodesDiscoveryService`
 - `token.TokenDiscoveryService`
 - `zookeeper.ZkDiscoveryService`

As a result, they stuttered when read. This commit removes the
duplication of the package names on the struct so that they read better.

See also: https://golang.org/ref/spec#Import_declarations

Signed-off-by: Kushal Pisavadia <kushi.p@gmail.com>
This commit is contained in:
Kushal Pisavadia 2015-04-15 20:37:22 +01:00
parent 7218193922
commit 78dba5d623
13 changed files with 57 additions and 57 deletions

View File

@ -11,8 +11,8 @@ import (
consul "github.com/hashicorp/consul/api" consul "github.com/hashicorp/consul/api"
) )
// ConsulDiscoveryService is exported // DiscoveryService is exported
type ConsulDiscoveryService struct { type DiscoveryService struct {
heartbeat time.Duration heartbeat time.Duration
client *consul.Client client *consul.Client
prefix string prefix string
@ -20,11 +20,11 @@ type ConsulDiscoveryService struct {
} }
func init() { func init() {
discovery.Register("consul", &ConsulDiscoveryService{}) discovery.Register("consul", &DiscoveryService{})
} }
// Initialize is exported // Initialize is exported
func (s *ConsulDiscoveryService) Initialize(uris string, heartbeat uint64) error { func (s *DiscoveryService) Initialize(uris string, heartbeat uint64) error {
parts := strings.SplitN(uris, "/", 2) parts := strings.SplitN(uris, "/", 2)
if len(parts) < 2 { if len(parts) < 2 {
return fmt.Errorf("invalid format %q, missing <path>", uris) return fmt.Errorf("invalid format %q, missing <path>", uris)
@ -56,7 +56,7 @@ func (s *ConsulDiscoveryService) Initialize(uris string, heartbeat uint64) error
} }
// Fetch is exported // Fetch is exported
func (s *ConsulDiscoveryService) Fetch() ([]*discovery.Entry, error) { func (s *DiscoveryService) Fetch() ([]*discovery.Entry, error) {
kv := s.client.KV() kv := s.client.KV()
pairs, _, err := kv.List(s.prefix, nil) pairs, _, err := kv.List(s.prefix, nil)
if err != nil { if err != nil {
@ -75,7 +75,7 @@ func (s *ConsulDiscoveryService) Fetch() ([]*discovery.Entry, error) {
} }
// Watch is exported // Watch is exported
func (s *ConsulDiscoveryService) Watch(callback discovery.WatchCallback) { func (s *DiscoveryService) Watch(callback discovery.WatchCallback) {
for _ = range s.waitForChange() { for _ = range s.waitForChange() {
log.WithField("name", "consul").Debug("Discovery watch triggered") log.WithField("name", "consul").Debug("Discovery watch triggered")
entries, err := s.Fetch() entries, err := s.Fetch()
@ -86,14 +86,14 @@ func (s *ConsulDiscoveryService) Watch(callback discovery.WatchCallback) {
} }
// Register is exported // Register is exported
func (s *ConsulDiscoveryService) Register(addr string) error { func (s *DiscoveryService) Register(addr string) error {
kv := s.client.KV() kv := s.client.KV()
p := &consul.KVPair{Key: path.Join(s.prefix, addr), Value: []byte(addr)} p := &consul.KVPair{Key: path.Join(s.prefix, addr), Value: []byte(addr)}
_, err := kv.Put(p, nil) _, err := kv.Put(p, nil)
return err return err
} }
func (s *ConsulDiscoveryService) waitForChange() <-chan uint64 { func (s *DiscoveryService) waitForChange() <-chan uint64 {
c := make(chan uint64) c := make(chan uint64)
go func() { go func() {
for { for {

View File

@ -7,7 +7,7 @@ import (
) )
func TestInitialize(t *testing.T) { func TestInitialize(t *testing.T) {
discovery := &ConsulDiscoveryService{} discovery := &DiscoveryService{}
assert.Equal(t, discovery.Initialize("127.0.0.1", 0).Error(), "invalid format \"127.0.0.1\", missing <path>") assert.Equal(t, discovery.Initialize("127.0.0.1", 0).Error(), "invalid format \"127.0.0.1\", missing <path>")

View File

@ -10,19 +10,19 @@ import (
"github.com/docker/swarm/discovery" "github.com/docker/swarm/discovery"
) )
// EtcdDiscoveryService is exported // DiscoveryService is exported
type EtcdDiscoveryService struct { type DiscoveryService struct {
ttl uint64 ttl uint64
client *etcd.Client client *etcd.Client
path string path string
} }
func init() { func init() {
discovery.Register("etcd", &EtcdDiscoveryService{}) discovery.Register("etcd", &DiscoveryService{})
} }
// Initialize is exported // Initialize is exported
func (s *EtcdDiscoveryService) Initialize(uris string, heartbeat uint64) error { func (s *DiscoveryService) Initialize(uris string, heartbeat uint64) error {
var ( var (
// split here because uris can contain multiples ips // split here because uris can contain multiples ips
// like `etcd://192.168.0.1,192.168.0.2,192.168.0.3/path` // like `etcd://192.168.0.1,192.168.0.2,192.168.0.3/path`
@ -56,7 +56,7 @@ func (s *EtcdDiscoveryService) Initialize(uris string, heartbeat uint64) error {
} }
// Fetch is exported // Fetch is exported
func (s *EtcdDiscoveryService) Fetch() ([]*discovery.Entry, error) { func (s *DiscoveryService) Fetch() ([]*discovery.Entry, error) {
resp, err := s.client.Get(s.path, true, true) resp, err := s.client.Get(s.path, true, true)
if err != nil { if err != nil {
return nil, err return nil, err
@ -70,7 +70,7 @@ func (s *EtcdDiscoveryService) Fetch() ([]*discovery.Entry, error) {
} }
// Watch is exported // Watch is exported
func (s *EtcdDiscoveryService) Watch(callback discovery.WatchCallback) { func (s *DiscoveryService) Watch(callback discovery.WatchCallback) {
watchChan := make(chan *etcd.Response) watchChan := make(chan *etcd.Response)
go s.client.Watch(s.path, 0, true, watchChan, nil) go s.client.Watch(s.path, 0, true, watchChan, nil)
for _ = range watchChan { for _ = range watchChan {
@ -83,7 +83,7 @@ func (s *EtcdDiscoveryService) Watch(callback discovery.WatchCallback) {
} }
// Register is exported // Register is exported
func (s *EtcdDiscoveryService) Register(addr string) error { func (s *DiscoveryService) Register(addr string) error {
_, err := s.client.Set(path.Join(s.path, addr), addr, s.ttl) _, err := s.client.Set(path.Join(s.path, addr), addr, s.ttl)
return err return err
} }

View File

@ -7,7 +7,7 @@ import (
) )
func TestInitialize(t *testing.T) { func TestInitialize(t *testing.T) {
discovery := &EtcdDiscoveryService{} discovery := &DiscoveryService{}
assert.Equal(t, discovery.Initialize("127.0.0.1", 0).Error(), "invalid format \"127.0.0.1\", missing <path>") assert.Equal(t, discovery.Initialize("127.0.0.1", 0).Error(), "invalid format \"127.0.0.1\", missing <path>")

View File

@ -8,18 +8,18 @@ import (
"github.com/docker/swarm/discovery" "github.com/docker/swarm/discovery"
) )
// FileDiscoveryService is exported // DiscoveryService is exported
type FileDiscoveryService struct { type DiscoveryService struct {
heartbeat uint64 heartbeat uint64
path string path string
} }
func init() { func init() {
discovery.Register("file", &FileDiscoveryService{}) discovery.Register("file", &DiscoveryService{})
} }
// Initialize is exported // Initialize is exported
func (s *FileDiscoveryService) Initialize(path string, heartbeat uint64) error { func (s *DiscoveryService) Initialize(path string, heartbeat uint64) error {
s.path = path s.path = path
s.heartbeat = heartbeat s.heartbeat = heartbeat
return nil return nil
@ -47,7 +47,7 @@ func parseFileContent(content []byte) []string {
} }
// Fetch is exported // Fetch is exported
func (s *FileDiscoveryService) Fetch() ([]*discovery.Entry, error) { func (s *DiscoveryService) Fetch() ([]*discovery.Entry, error) {
fileContent, err := ioutil.ReadFile(s.path) fileContent, err := ioutil.ReadFile(s.path)
if err != nil { if err != nil {
return nil, err return nil, err
@ -56,7 +56,7 @@ func (s *FileDiscoveryService) Fetch() ([]*discovery.Entry, error) {
} }
// Watch is exported // Watch is exported
func (s *FileDiscoveryService) Watch(callback discovery.WatchCallback) { func (s *DiscoveryService) Watch(callback discovery.WatchCallback) {
for _ = range time.Tick(time.Duration(s.heartbeat) * time.Second) { for _ = range time.Tick(time.Duration(s.heartbeat) * time.Second) {
entries, err := s.Fetch() entries, err := s.Fetch()
if err == nil { if err == nil {
@ -66,6 +66,6 @@ func (s *FileDiscoveryService) Watch(callback discovery.WatchCallback) {
} }
// Register is exported // Register is exported
func (s *FileDiscoveryService) Register(addr string) error { func (s *DiscoveryService) Register(addr string) error {
return discovery.ErrNotImplemented return discovery.ErrNotImplemented
} }

View File

@ -7,7 +7,7 @@ import (
) )
func TestInitialize(t *testing.T) { func TestInitialize(t *testing.T) {
discovery := &FileDiscoveryService{} discovery := &DiscoveryService{}
discovery.Initialize("/path/to/file", 0) discovery.Initialize("/path/to/file", 0)
assert.Equal(t, discovery.path, "/path/to/file") assert.Equal(t, discovery.path, "/path/to/file")
} }
@ -26,7 +26,7 @@ func TestContent(t *testing.T) {
} }
func TestRegister(t *testing.T) { func TestRegister(t *testing.T) {
discovery := &FileDiscoveryService{path: "/path/to/file"} discovery := &DiscoveryService{path: "/path/to/file"}
assert.Error(t, discovery.Register("0.0.0.0")) assert.Error(t, discovery.Register("0.0.0.0"))
} }

View File

@ -6,17 +6,17 @@ import (
"github.com/docker/swarm/discovery" "github.com/docker/swarm/discovery"
) )
// NodesDiscoveryService is exported // DiscoveryService is exported
type NodesDiscoveryService struct { type DiscoveryService struct {
entries []*discovery.Entry entries []*discovery.Entry
} }
func init() { func init() {
discovery.Register("nodes", &NodesDiscoveryService{}) discovery.Register("nodes", &DiscoveryService{})
} }
// Initialize is exported // Initialize is exported
func (s *NodesDiscoveryService) Initialize(uris string, _ uint64) error { func (s *DiscoveryService) Initialize(uris string, _ uint64) error {
for _, input := range strings.Split(uris, ",") { for _, input := range strings.Split(uris, ",") {
for _, ip := range discovery.Generate(input) { for _, ip := range discovery.Generate(input) {
entry, err := discovery.NewEntry(ip) entry, err := discovery.NewEntry(ip)
@ -31,15 +31,15 @@ func (s *NodesDiscoveryService) Initialize(uris string, _ uint64) error {
} }
// Fetch is exported // Fetch is exported
func (s *NodesDiscoveryService) Fetch() ([]*discovery.Entry, error) { func (s *DiscoveryService) Fetch() ([]*discovery.Entry, error) {
return s.entries, nil return s.entries, nil
} }
// Watch is exported // Watch is exported
func (s *NodesDiscoveryService) Watch(callback discovery.WatchCallback) { func (s *DiscoveryService) Watch(callback discovery.WatchCallback) {
} }
// Register is exported // Register is exported
func (s *NodesDiscoveryService) Register(addr string) error { func (s *DiscoveryService) Register(addr string) error {
return discovery.ErrNotImplemented return discovery.ErrNotImplemented
} }

View File

@ -7,7 +7,7 @@ import (
) )
func TestInitialise(t *testing.T) { func TestInitialise(t *testing.T) {
discovery := &NodesDiscoveryService{} discovery := &DiscoveryService{}
discovery.Initialize("1.1.1.1:1111,2.2.2.2:2222", 0) discovery.Initialize("1.1.1.1:1111,2.2.2.2:2222", 0)
assert.Equal(t, len(discovery.entries), 2) assert.Equal(t, len(discovery.entries), 2)
assert.Equal(t, discovery.entries[0].String(), "1.1.1.1:1111") assert.Equal(t, discovery.entries[0].String(), "1.1.1.1:1111")
@ -15,7 +15,7 @@ func TestInitialise(t *testing.T) {
} }
func TestInitialiseWithPattern(t *testing.T) { func TestInitialiseWithPattern(t *testing.T) {
discovery := &NodesDiscoveryService{} discovery := &DiscoveryService{}
discovery.Initialize("1.1.1.[1:2]:1111,2.2.2.[2:4]:2222", 0) discovery.Initialize("1.1.1.[1:2]:1111,2.2.2.[2:4]:2222", 0)
assert.Equal(t, len(discovery.entries), 5) assert.Equal(t, len(discovery.entries), 5)
assert.Equal(t, discovery.entries[0].String(), "1.1.1.1:1111") assert.Equal(t, discovery.entries[0].String(), "1.1.1.1:1111")
@ -26,6 +26,6 @@ func TestInitialiseWithPattern(t *testing.T) {
} }
func TestRegister(t *testing.T) { func TestRegister(t *testing.T) {
discovery := &NodesDiscoveryService{} discovery := &DiscoveryService{}
assert.Error(t, discovery.Register("0.0.0.0")) assert.Error(t, discovery.Register("0.0.0.0"))
} }

View File

@ -15,19 +15,19 @@ import (
// DiscoveryUrl is exported // DiscoveryUrl is exported
const DiscoveryURL = "https://discovery-stage.hub.docker.com/v1" const DiscoveryURL = "https://discovery-stage.hub.docker.com/v1"
// TokenDiscoveryService is exported // DiscoveryService is exported
type TokenDiscoveryService struct { type DiscoveryService struct {
heartbeat uint64 heartbeat uint64
url string url string
token string token string
} }
func init() { func init() {
discovery.Register("token", &TokenDiscoveryService{}) discovery.Register("token", &DiscoveryService{})
} }
// Initialize is exported // Initialize is exported
func (s *TokenDiscoveryService) Initialize(urltoken string, heartbeat uint64) error { func (s *DiscoveryService) Initialize(urltoken string, heartbeat uint64) error {
if i := strings.LastIndex(urltoken, "/"); i != -1 { if i := strings.LastIndex(urltoken, "/"); i != -1 {
s.url = "https://" + urltoken[:i] s.url = "https://" + urltoken[:i]
s.token = urltoken[i+1:] s.token = urltoken[i+1:]
@ -45,7 +45,7 @@ func (s *TokenDiscoveryService) Initialize(urltoken string, heartbeat uint64) er
} }
// Fetch returns the list of entries for the discovery service at the specified endpoint // Fetch returns the list of entries for the discovery service at the specified endpoint
func (s *TokenDiscoveryService) Fetch() ([]*discovery.Entry, error) { func (s *DiscoveryService) Fetch() ([]*discovery.Entry, error) {
resp, err := http.Get(fmt.Sprintf("%s/%s/%s", s.url, "clusters", s.token)) resp, err := http.Get(fmt.Sprintf("%s/%s/%s", s.url, "clusters", s.token))
if err != nil { if err != nil {
@ -67,7 +67,7 @@ func (s *TokenDiscoveryService) Fetch() ([]*discovery.Entry, error) {
} }
// Watch is exported // Watch is exported
func (s *TokenDiscoveryService) Watch(callback discovery.WatchCallback) { func (s *DiscoveryService) Watch(callback discovery.WatchCallback) {
for _ = range time.Tick(time.Duration(s.heartbeat) * time.Second) { for _ = range time.Tick(time.Duration(s.heartbeat) * time.Second) {
entries, err := s.Fetch() entries, err := s.Fetch()
if err == nil { if err == nil {
@ -77,7 +77,7 @@ func (s *TokenDiscoveryService) Watch(callback discovery.WatchCallback) {
} }
// Register adds a new entry identified by the into the discovery service // Register adds a new entry identified by the into the discovery service
func (s *TokenDiscoveryService) Register(addr string) error { func (s *DiscoveryService) Register(addr string) error {
buf := strings.NewReader(addr) buf := strings.NewReader(addr)
resp, err := http.Post(fmt.Sprintf("%s/%s/%s", s.url, resp, err := http.Post(fmt.Sprintf("%s/%s/%s", s.url,
@ -92,7 +92,7 @@ func (s *TokenDiscoveryService) Register(addr string) error {
} }
// CreateCluster returns a unique cluster token // CreateCluster returns a unique cluster token
func (s *TokenDiscoveryService) CreateCluster() (string, error) { func (s *DiscoveryService) CreateCluster() (string, error) {
resp, err := http.Post(fmt.Sprintf("%s/%s", s.url, "clusters"), "", nil) resp, err := http.Post(fmt.Sprintf("%s/%s", s.url, "clusters"), "", nil)
if err != nil { if err != nil {
return "", err return "", err

View File

@ -7,7 +7,7 @@ import (
) )
func TestInitialize(t *testing.T) { func TestInitialize(t *testing.T) {
discovery := &TokenDiscoveryService{} discovery := &DiscoveryService{}
err := discovery.Initialize("token", 0) err := discovery.Initialize("token", 0)
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, discovery.token, "token") assert.Equal(t, discovery.token, "token")
@ -23,7 +23,7 @@ func TestInitialize(t *testing.T) {
} }
func TestRegister(t *testing.T) { func TestRegister(t *testing.T) {
discovery := &TokenDiscoveryService{token: "TEST_TOKEN", url: DiscoveryURL} discovery := &DiscoveryService{token: "TEST_TOKEN", url: DiscoveryURL}
expected := "127.0.0.1:2675" expected := "127.0.0.1:2675"
assert.NoError(t, discovery.Register(expected)) assert.NoError(t, discovery.Register(expected))

View File

@ -11,22 +11,22 @@ import (
"github.com/samuel/go-zookeeper/zk" "github.com/samuel/go-zookeeper/zk"
) )
// ZkDiscoveryService is exported // DiscoveryService is exported
type ZkDiscoveryService struct { type DiscoveryService struct {
conn *zk.Conn conn *zk.Conn
path []string path []string
heartbeat uint64 heartbeat uint64
} }
func init() { func init() {
discovery.Register("zk", &ZkDiscoveryService{}) discovery.Register("zk", &DiscoveryService{})
} }
func (s *ZkDiscoveryService) fullpath() string { func (s *DiscoveryService) fullpath() string {
return "/" + strings.Join(s.path, "/") return "/" + strings.Join(s.path, "/")
} }
func (s *ZkDiscoveryService) createFullpath() error { func (s *DiscoveryService) createFullpath() error {
for i := 1; i <= len(s.path); i++ { for i := 1; i <= len(s.path); i++ {
newpath := "/" + strings.Join(s.path[:i], "/") newpath := "/" + strings.Join(s.path[:i], "/")
_, err := s.conn.Create(newpath, []byte{1}, 0, zk.WorldACL(zk.PermAll)) _, err := s.conn.Create(newpath, []byte{1}, 0, zk.WorldACL(zk.PermAll))
@ -41,7 +41,7 @@ func (s *ZkDiscoveryService) createFullpath() error {
} }
// Initialize is exported // Initialize is exported
func (s *ZkDiscoveryService) Initialize(uris string, heartbeat uint64) error { func (s *DiscoveryService) Initialize(uris string, heartbeat uint64) error {
var ( var (
// split here because uris can contain multiples ips // split here because uris can contain multiples ips
// like `zk://192.168.0.1,192.168.0.2,192.168.0.3/path` // like `zk://192.168.0.1,192.168.0.2,192.168.0.3/path`
@ -75,7 +75,7 @@ func (s *ZkDiscoveryService) Initialize(uris string, heartbeat uint64) error {
} }
// Fetch is exported // Fetch is exported
func (s *ZkDiscoveryService) Fetch() ([]*discovery.Entry, error) { func (s *DiscoveryService) Fetch() ([]*discovery.Entry, error) {
addrs, _, err := s.conn.Children(s.fullpath()) addrs, _, err := s.conn.Children(s.fullpath())
if err != nil { if err != nil {
@ -86,7 +86,7 @@ func (s *ZkDiscoveryService) Fetch() ([]*discovery.Entry, error) {
} }
// Watch is exported // Watch is exported
func (s *ZkDiscoveryService) Watch(callback discovery.WatchCallback) { func (s *DiscoveryService) Watch(callback discovery.WatchCallback) {
addrs, _, eventChan, err := s.conn.ChildrenW(s.fullpath()) addrs, _, eventChan, err := s.conn.ChildrenW(s.fullpath())
if err != nil { if err != nil {
@ -112,7 +112,7 @@ func (s *ZkDiscoveryService) Watch(callback discovery.WatchCallback) {
} }
// Register is exported // Register is exported
func (s *ZkDiscoveryService) Register(addr string) error { func (s *DiscoveryService) Register(addr string) error {
nodePath := path.Join(s.fullpath(), addr) nodePath := path.Join(s.fullpath(), addr)
// check existing for the parent path first // check existing for the parent path first

View File

@ -7,7 +7,7 @@ import (
) )
func TestInitialize(t *testing.T) { func TestInitialize(t *testing.T) {
service := &ZkDiscoveryService{} service := &DiscoveryService{}
assert.Equal(t, service.Initialize("127.0.0.1", 0).Error(), "invalid format \"127.0.0.1\", missing <path>") assert.Equal(t, service.Initialize("127.0.0.1", 0).Error(), "invalid format \"127.0.0.1\", missing <path>")

View File

@ -64,7 +64,7 @@ func main() {
ShortName: "c", ShortName: "c",
Usage: "create a cluster", Usage: "create a cluster",
Action: func(c *cli.Context) { Action: func(c *cli.Context) {
discovery := &token.TokenDiscoveryService{} discovery := &token.DiscoveryService{}
discovery.Initialize("", 0) discovery.Initialize("", 0)
token, err := discovery.CreateCluster() token, err := discovery.CreateCluster()
if len(c.Args()) != 0 { if len(c.Args()) != 0 {