/
vulcand.go
138 lines (121 loc) · 2.74 KB
/
vulcand.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Copyright © 2015 Jason Smith <jasonrichardsmith@gmail.com>.
//
// Use of this source code is governed by the LGPL-3
// license that can be found in the LICENSE file.
package vulcand
import (
"encoding/json"
"flag"
"fmt"
"log"
"os"
"strconv"
"time"
"github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
"github.com/coreos/etcd/client"
"github.com/duckbunny/etcd"
"github.com/duckbunny/herald"
"github.com/duckbunny/service"
)
var (
KVPath string = "/vulcand/backends"
TTL int = 15
// Title for specifying herald in flags
Title string = "vulcand"
)
type Backend struct {
Type string `json:"Type"`
}
type Server struct {
URL string `json:"URL"`
}
func init() {
ttl := os.Getenv("VULCAND_TTL")
if ttl != "" {
newttl, err := strconv.Atoi(ttl)
if err != nil {
log.Fatal(err)
}
TTL = newttl
}
flag.IntVar(&TTL, "vulcand-ttl", TTL, "TTL for vulcand microservice heartbeats.")
}
type Vulcand struct {
*etcd.Etcd
heartBeatKill chan bool
}
func New() *Vulcand {
e := etcd.New()
c := make(chan bool)
v := &Vulcand{e, c}
return v
}
func (v *Vulcand) Start(s *service.Service) error {
key := BackendPath(s)
b := Backend{s.Protocol}
js, err := json.Marshal(b)
if err != nil {
return err
}
_, err = v.KeysAPI.Set(context.Background(), key, string(js), nil)
if err != nil {
return etcd.ProcessEtcdErrors(err)
}
err = v.setServer(s)
if err != nil {
return err
}
go v.heartBeat(s)
return nil
}
func (v *Vulcand) heartBeat(s *service.Service) {
for _ = range time.Tick(time.Duration(TTL-1) * time.Second) {
select {
case <-v.heartBeatKill:
v.Stop(s)
return
default:
}
v.setServer(s)
}
}
func (v *Vulcand) setServer(s *service.Service) error {
key := ServerPath(s)
serv := Server{Url(s)}
so := client.SetOptions{TTL: time.Duration(TTL) * time.Second}
js, err := json.Marshal(serv)
if err != nil {
return err
}
_, err = v.KeysAPI.Set(context.Background(), key, string(js), &so)
if err != nil {
return etcd.ProcessEtcdErrors(err)
}
return nil
}
func (v *Vulcand) Stop(s *service.Service) error {
key := ServerPath(s)
_, err := v.KeysAPI.Delete(context.Background(), key, nil)
if err != nil {
return etcd.ProcessEtcdErrors(err)
}
return nil
}
func BackendPath(s *service.Service) string {
return fmt.Sprintf("%v/backend", BasePath(s))
}
func ServerPath(s *service.Service) string {
return fmt.Sprintf("%v/servers/%v.%v", BasePath(s), s.Host, s.Port)
}
func BasePath(s *service.Service) string {
return fmt.Sprintf("%v/%v.%v.%v", KVPath, s.Domain, s.Title, s.Version)
}
func Url(s *service.Service) string {
return fmt.Sprintf("%v:%v", s.Host, s.Port)
}
// Register this herald with consul
func Register() {
c := New()
herald.AddPool(Title, c)
herald.AddDeclaration(Title, c)
}