-
Notifications
You must be signed in to change notification settings - Fork 784
/
kv_get.go
115 lines (94 loc) · 2.46 KB
/
kv_get.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
package dependency
import (
"fmt"
"log"
"net/url"
"regexp"
"github.com/pkg/errors"
)
var (
// Ensure implements
_ Dependency = (*KVGetQuery)(nil)
// KVGetQueryRe is the regular expression to use.
KVGetQueryRe = regexp.MustCompile(`\A` + keyRe + dcRe + `\z`)
)
// KVGetQuery queries the KV store for a single key.
type KVGetQuery struct {
stopCh chan struct{}
dc string
key string
blockOnNil bool
}
// NewKVGetQuery parses a string into a dependency.
func NewKVGetQuery(s string) (*KVGetQuery, error) {
if s != "" && !KVGetQueryRe.MatchString(s) {
return nil, fmt.Errorf("kv.get: invalid format: %q", s)
}
m := regexpMatch(KVGetQueryRe, s)
return &KVGetQuery{
stopCh: make(chan struct{}, 1),
dc: m["dc"],
key: m["key"],
}, nil
}
// Fetch queries the Consul API defined by the given client.
func (d *KVGetQuery) Fetch(clients *ClientSet, opts *QueryOptions) (interface{}, *ResponseMetadata, error) {
select {
case <-d.stopCh:
return nil, nil, ErrStopped
default:
}
opts = opts.Merge(&QueryOptions{
Datacenter: d.dc,
})
log.Printf("[TRACE] %s: GET %s", d, &url.URL{
Path: "/v1/kv/" + d.key,
RawQuery: opts.String(),
})
// NOTE that the Consul HTTP KV API returns a 404 on failed gets, but the
// Consul Go API Package ignores those for KV Gets, returning nil data and
// an error.
pair, qm, err := clients.Consul().KV().Get(d.key, opts.ToConsulOpts())
if err != nil {
return nil, nil, errors.Wrap(err, d.String())
}
rm := &ResponseMetadata{
LastIndex: qm.LastIndex,
LastContact: qm.LastContact,
BlockOnNil: d.blockOnNil,
}
if pair == nil {
log.Printf("[TRACE] %s: returned nil", d)
return nil, rm, nil
}
value := string(pair.Value)
log.Printf("[TRACE] %s: returned %q", d, value)
return value, rm, nil
}
// EnableBlocking turns this into a blocking KV query.
func (d *KVGetQuery) EnableBlocking() {
d.blockOnNil = true
}
// CanShare returns a boolean if this dependency is shareable.
func (d *KVGetQuery) CanShare() bool {
return true
}
// String returns the human-friendly version of this dependency.
func (d *KVGetQuery) String() string {
key := d.key
if d.dc != "" {
key = key + "@" + d.dc
}
if d.blockOnNil {
return fmt.Sprintf("kv.block(%s)", key)
}
return fmt.Sprintf("kv.get(%s)", key)
}
// Stop halts the dependency's fetch function.
func (d *KVGetQuery) Stop() {
close(d.stopCh)
}
// Type returns the type of this dependency.
func (d *KVGetQuery) Type() Type {
return TypeConsul
}