forked from coreos/nsproxy
/
backends.go
119 lines (92 loc) · 2.02 KB
/
backends.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
package main
import (
"encoding/json"
"log"
"net"
"strconv"
"sync"
"github.com/coreos/go-etcd/etcd"
)
type service struct {
Host string `json:"host"`
Port int `json:"port"`
}
type host struct {
key string
addr string
}
type backends struct {
path string
hosts []host
lastIndex int
watchIndex uint64
lock sync.RWMutex
}
func (b *backends) Dump(action string ) {
for _, v := range(b.hosts) {
log.Printf("Dump after %s %s -> %s", action, v.key, v.addr)
}
}
func (b *backends) Remove(key string) {
match := -1
for k, v := range(b.hosts) {
if v.key == key {
match = k
}
}
b.hosts = append(b.hosts[:match], b.hosts[match+1:]...)
b.Dump("remove")
}
func (b *backends) Update(node *etcd.Node, action string) {
b.lock.Lock()
defer b.lock.Unlock()
log.Printf("key: %s action: %s value: %s", node.Key, action, string(node.Value))
s := &service{}
if action == "delete" || action == "expire" {
b.Remove(node.Key)
return
}
err := json.Unmarshal([]byte(node.Value), s)
if err != nil {
panic(err)
}
addr := net.JoinHostPort(s.Host, strconv.Itoa(s.Port))
// TODO: create an actual set data structure
for _, v := range(b.hosts) {
if v.key == node.Key {
b.Dump(action)
return
}
}
b.hosts = append(b.hosts, host{addr: addr, key: node.Key})
}
func (b *backends) Watch(client *etcd.Client) {
receiver := make(chan *etcd.Response)
go client.Watch(b.path, uint64(b.watchIndex), true, receiver, nil)
for {
resp := <-receiver
b.Update(resp.Node, resp.Action)
}
}
func (b *backends) Sync(client *etcd.Client) error {
resp, err := client.Get(b.path, false, true)
if err != nil {
return err
}
for _, n := range(resp.Node.Nodes) {
b.Update(&n, resp.Action)
}
// Begin the watch after this sync from the next sync
b.watchIndex = resp.EtcdIndex + 1
return nil
}
func (b *backends) Next() string {
b.lock.RLock()
defer b.lock.RUnlock()
if len(b.hosts) == 0 {
return ""
}
index := (b.lastIndex + 1) % len(b.hosts)
b.lastIndex = index
return b.hosts[index].addr
}