forked from coreos/etcdctl
/
watch.go
119 lines (92 loc) · 2.52 KB
/
watch.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 etcd
import (
"encoding/json"
"errors"
"fmt"
"github.com/coreos/etcd/store"
"io/ioutil"
"net/http"
"net/url"
"path"
)
type respAndErr struct {
resp *http.Response
err error
}
// Errors introduced by the Watch command.
var (
ErrWatchStoppedByUser = errors.New("Watch stopped by the user via stop channel")
)
// Watch any change under the given prefix.
// When a sinceIndex is given, watch will try to scan from that index to the last index
// and will return any changes under the given prefix during the history
// If a receiver channel is given, it will be a long-term watch. Watch will block at the
// channel. And after someone receive the channel, it will go on to watch that prefix.
// If a stop channel is given, client can close long-term watch using the stop channel
func (c *Client) Watch(prefix string, sinceIndex uint64, receiver chan *store.Response, stop chan bool) (*store.Response, error) {
logger.Debugf("watch %s [%s]", prefix, c.cluster.Leader)
if receiver == nil {
return c.watchOnce(prefix, sinceIndex, stop)
} else {
for {
resp, err := c.watchOnce(prefix, sinceIndex, stop)
if resp != nil {
sinceIndex = resp.Index + 1
receiver <- resp
} else {
return nil, err
}
}
}
return nil, nil
}
// helper func
// return when there is change under the given prefix
func (c *Client) watchOnce(key string, sinceIndex uint64, stop chan bool) (*store.Response, error) {
var resp *http.Response
var err error
if stop != nil {
ch := make(chan respAndErr)
go func() {
resp, err = c.sendWatchRequest(key, sinceIndex)
ch <- respAndErr{resp, err}
}()
// select at stop or continue to receive
select {
case res := <-ch:
resp, err = res.resp, res.err
case <-stop:
resp, err = nil, ErrWatchStoppedByUser
}
} else {
resp, err = c.sendWatchRequest(key, sinceIndex)
}
if err != nil {
return nil, err
}
b, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, handleError(b)
}
var result store.Response
err = json.Unmarshal(b, &result)
if err != nil {
return nil, err
}
return &result, nil
}
func (c *Client) sendWatchRequest(key string, sinceIndex uint64) (*http.Response, error) {
if sinceIndex == 0 {
resp, err := c.sendRequest("GET", path.Join("watch", key), "")
return resp, err
} else {
v := url.Values{}
v.Set("index", fmt.Sprintf("%v", sinceIndex))
resp, err := c.sendRequest("POST", path.Join("watch", key), v.Encode())
return resp, err
}
}