This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathdata_store.go
173 lines (154 loc) · 3.97 KB
/
data_store.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package main
import (
"encoding/json"
"errors"
"log"
"path"
"github.com/coreos/go-etcd/etcd"
"github.com/flynn/strowger/types"
)
type EtcdClient interface {
Create(key string, value string, ttl uint64) (*etcd.Response, error)
Set(key string, value string, ttl uint64) (*etcd.Response, error)
Get(key string, sort, recursive bool) (*etcd.Response, error)
Delete(key string, recursive bool) (*etcd.Response, error)
Watch(prefix string, waitIndex uint64, recursive bool, receiver chan *etcd.Response, stop chan bool) (*etcd.Response, error)
}
type DataStore interface {
Add(route *strowger.Route) error
Set(route *strowger.Route) error
Get(id string) (*strowger.Route, error)
List() ([]*strowger.Route, error)
Remove(id string) error
Sync(h SyncHandler, started chan<- error)
StopSync()
}
type DataStoreReader interface {
Get(id string) (*strowger.Route, error)
List() ([]*strowger.Route, error)
}
type SyncHandler interface {
Set(route *strowger.Route) error
Remove(id string) error
}
func NewEtcdDataStore(etcd EtcdClient, prefix string) DataStore {
return &etcdDataStore{
prefix: prefix,
etcd: etcd,
stopSync: make(chan bool),
}
}
type etcdDataStore struct {
prefix string
etcd EtcdClient
stopSync chan bool
}
var ErrExists = errors.New("strowger: route already exists")
var ErrNotFound = errors.New("strowger: route not found")
func (s *etcdDataStore) Add(r *strowger.Route) error {
data, err := json.Marshal(r)
if err != nil {
return err
}
_, err = s.etcd.Create(s.path(r.ID), string(data), 0)
if e, ok := err.(*etcd.EtcdError); ok && e.ErrorCode == 105 {
err = ErrExists
}
return err
}
func (s *etcdDataStore) Set(r *strowger.Route) error {
data, err := json.Marshal(r)
if err != nil {
return err
}
_, err = s.etcd.Set(s.path(r.ID), string(data), 0)
return err
}
func (s *etcdDataStore) Remove(id string) error {
_, err := s.etcd.Delete(s.path(id), true)
if e, ok := err.(*etcd.EtcdError); ok && e.ErrorCode == 100 {
return ErrNotFound
}
return err
}
func (s *etcdDataStore) Get(id string) (*strowger.Route, error) {
res, err := s.etcd.Get(s.path(id), false, false)
if err != nil {
if e, ok := err.(*etcd.EtcdError); ok && e.ErrorCode == 100 {
err = ErrNotFound
}
return nil, err
}
r := &strowger.Route{}
err = json.Unmarshal([]byte(res.Node.Value), r)
return r, err
}
func (s *etcdDataStore) List() ([]*strowger.Route, error) {
res, err := s.etcd.Get(s.prefix, false, true)
if err != nil {
if e, ok := err.(*etcd.EtcdError); ok && e.ErrorCode == 100 {
err = nil
}
return nil, err
}
routes := make([]*strowger.Route, len(res.Node.Nodes))
for i, node := range res.Node.Nodes {
r := &strowger.Route{}
if err := json.Unmarshal([]byte(node.Value), r); err != nil {
return nil, err
}
routes[i] = r
}
return routes, nil
}
func (s *etcdDataStore) path(id string) string {
return s.prefix + path.Base(id)
}
func (s *etcdDataStore) Sync(h SyncHandler, started chan<- error) {
var since uint64
data, err := s.etcd.Get(s.prefix, false, true)
if e, ok := err.(*etcd.EtcdError); ok && e.ErrorCode == 100 {
// key not found, ignore
goto watch
}
if err != nil {
started <- err
return
}
since = data.EtcdIndex
for _, node := range data.Node.Nodes {
route := &strowger.Route{}
if err := json.Unmarshal([]byte(node.Value), route); err != nil {
started <- err
return
}
if err := h.Set(route); err != nil {
started <- err
return
}
}
watch:
started <- nil
stream := make(chan *etcd.Response)
go s.etcd.Watch(s.prefix, since, true, stream, s.stopSync)
for res := range stream {
id := path.Base(res.Node.Key)
var err error
if res.Action == "delete" {
err = h.Remove(id)
} else {
route := &strowger.Route{}
if err = json.Unmarshal([]byte(res.Node.Value), route); err != nil {
goto fail
}
err = h.Set(route)
}
fail:
if err != nil {
log.Printf("Error while processing update from etcd: %s, %#v", err, res.Node)
}
}
}
func (s *etcdDataStore) StopSync() {
close(s.stopSync)
}