forked from diamanticom/etcd-lock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
etcdutils.go
106 lines (93 loc) · 4.31 KB
/
etcdutils.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
/*
Copyright 2014 Datawise Systems Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package utils
import (
"flag"
"github.com/etcd-io/etcd"
)
const (
EtcdErrorCodeNotFound = 100
EtcdErrorCodeIndexCleared = 401
)
var (
EtcdErrorNotFound = &etcd.EtcdError{ErrorCode: EtcdErrorCodeNotFound}
)
// IsEtcdNotFound checks if the err is a not found error.
func IsEtcdNotFound(err error) bool {
etcdErr, ok := err.(*etcd.EtcdError)
return ok && etcdErr != nil && etcdErr.ErrorCode == EtcdErrorCodeNotFound
}
// IsEtcdWatchStoppedByUser checks if err indicates watch stopped by user.
func IsEtcdWatchStoppedByUser(err error) bool {
return etcd.ErrWatchStoppedByUser == err
}
func IsEtcdEventIndexCleared(err error) bool {
etcdErr, ok := err.(*etcd.EtcdError)
return ok && etcdErr != nil && etcdErr.ErrorCode == EtcdErrorCodeIndexCleared
}
// Etcd client interface.
type Registry interface {
// Add a new file with a random etcd-generated key under the given path
AddChild(key string, value string, ttl uint64) (*etcd.Response, error)
// Get gets the file or directory associated with the given key.
// If the key points to a directory, files and directories under
// it will be returned in sorted or unsorted order, depending on
// the sort flag.
// If recursive is set to false, contents under child directories
// will not be returned.
// If recursive is set to true, all the contents will be returned.
Get(key string, sort, recursive bool) (*etcd.Response, error)
// Set sets the given key to the given value.
// It will create a new key value pair or replace the old one.
// It will not replace a existing directory.
Set(key string, value string, ttl uint64) (*etcd.Response, error)
// Update updates the given key to the given value. It succeeds only if the given key
// already exists.
Update(key string, value string, ttl uint64) (*etcd.Response, error)
// Create creates a file with the given value under the given key. It succeeds
// only if the given key does not yet exist.
Create(key string, value string, ttl uint64) (*etcd.Response, error)
// CreateInOrder creates a file with a key that's guaranteed to be higher than other
// keys in the given directory. It is useful for creating queues.
CreateInOrder(dir string, value string, ttl uint64) (*etcd.Response, error)
// CreateDir create a driectory. It succeeds only if the given key
// does not yet exist.
CreateDir(key string, ttl uint64) (*etcd.Response, error)
// Compare and swap only if prevValue & prevIndex match
CompareAndSwap(key string, value string, ttl uint64, prevValue string,
prevIndex uint64) (*etcd.Response, error)
// Delete deletes the given key.
// When recursive set to false, if the key points to a
// directory the method will fail.
// When recursive set to true, if the key points to a file,
// the file will be deleted; if the key points to a directory,
// then everything under the directory (including all child directories)
// will be deleted.
Delete(key string, recursive bool) (*etcd.Response, error)
// If recursive is set to true the watch returns the first change under the
// given prefix since the given index.
// If recursive is set to false the watch returns the first change to the
// given key since the given index.
// To watch for the latest change, set waitIndex = 0.
// If a receiver channel is given, it will be a long-term watch. Watch will
// block at the channel. After someone receives the channel, it will go on
// to watch that prefix. If a stop channel is given, the client can close
// long-term watch using the stop channel.
Watch(prefix string, waitIndex uint64, recursive bool,
receiver chan *etcd.Response, stop chan bool) (*etcd.Response, error)
}
var etcdServer = flag.String("etcd-server", "http://127.0.0.1:4001",
"Etcd service location")
func NewEtcdRegistry() Registry {
return etcd.NewClient([]string{*etcdServer})
}