forked from HeavyHorst/remco
/
zookeeper.go
58 lines (48 loc) · 1.42 KB
/
zookeeper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
* This file is part of remco.
* © 2016 The Remco Authors
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
package backends
import (
"github.com/HeavyHorst/easykv/zookeeper"
berr "github.com/HeavyHorst/remco/pkg/backends/error"
"github.com/HeavyHorst/remco/pkg/log"
"github.com/HeavyHorst/remco/pkg/template"
"github.com/sirupsen/logrus"
)
// ZookeeperConfig represents the config for the consul backend.
type ZookeeperConfig struct {
// A list of zookeeper nodes.
Nodes []string
// A DNS server record to discover the zookeeper nodes.
SRVRecord SRVRecord `toml:"srv_record"`
template.Backend
}
// Connect creates a new zookeeperClient and fills the underlying template.Backend with the zookeeper-Backend specific data.
func (c *ZookeeperConfig) Connect() (template.Backend, error) {
if c == nil {
return template.Backend{}, berr.ErrNilConfig
}
c.Backend.Name = "zookeeper"
// No nodes are set but a SRVRecord is provided
if len(c.Nodes) == 0 && c.SRVRecord != "" {
var err error
c.Nodes, err = c.SRVRecord.GetNodesFromSRV("")
if err != nil {
return c.Backend, err
}
}
log.WithFields(logrus.Fields{
"backend": c.Backend.Name,
"nodes": c.Nodes,
}).Info("set backend nodes")
client, err := zookeeper.New(c.Nodes)
if err != nil {
return c.Backend, err
}
c.Backend.ReadWatcher = client
return c.Backend, nil
}