-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterface.go
76 lines (64 loc) · 1.05 KB
/
interface.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package keeper
import (
"time"
"os"
"github.com/coreos/etcd/client"
)
type (
Config struct {
Endpoints []string
Username string
Password string
Leader_key string
Node string
TTL time.Duration
}
KeeperImpl struct {
config Config
cli client.KeysAPI
leader bool
}
Keeper interface {
Start() error
IsLeader() bool
}
)
const (
DefautlEtcd = "http://127.0.0.1:2379"
DefaultTTL = time.Minute
)
func NewKeeper(config *Config) (k Keeper, err error) {
if config == nil {
config = new(Config)
}
if len(config.Endpoints) <= 0 {
config.Endpoints = []string{DefautlEtcd}
}
if len(config.Node) <= 0 {
hostname, err := os.Hostname()
if err != nil {
panic(err)
}
config.Node = hostname
}
if config.TTL <= 0 {
config.TTL = DefaultTTL
}
k = &KeeperImpl{
config: *config,
leader: false,
}
return
}
func (k *KeeperImpl) Start() (err error) {
err = k.connect()
if err != nil {
return
}
go k.observe()
// go keeper.WatchNode()
return
}
func (k *KeeperImpl) IsLeader() bool {
return k.leader
}