forked from etcd-io/etcd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
release_handler.go
51 lines (43 loc) · 1.35 KB
/
release_handler.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
package v2
import (
"path"
"net/http"
"github.com/gorilla/mux"
)
// releaseLockHandler deletes the lock.
func (h *handler) releaseLockHandler(w http.ResponseWriter, req *http.Request) {
h.client.SyncCluster()
vars := mux.Vars(req)
keypath := path.Join(prefix, vars["key"])
// Read index and value parameters.
index := req.FormValue("index")
value := req.FormValue("value")
if len(index) == 0 && len(value) == 0 {
http.Error(w, "release lock error: index or value required", http.StatusInternalServerError)
return
} else if len(index) != 0 && len(value) != 0 {
http.Error(w, "release lock error: index and value cannot both be specified", http.StatusInternalServerError)
return
}
// Look up index by value if index is missing.
if len(index) == 0 {
resp, err := h.client.Get(keypath, true, true)
if err != nil {
http.Error(w, "release lock index error: " + err.Error(), http.StatusInternalServerError)
return
}
nodes := lockNodes{resp.Node.Nodes}
node, _ := nodes.FindByValue(value)
if node == nil {
http.Error(w, "release lock error: cannot find: " + value, http.StatusInternalServerError)
return
}
index = path.Base(node.Key)
}
// Delete the lock.
_, err := h.client.Delete(path.Join(keypath, index), false)
if err != nil {
http.Error(w, "release lock error: " + err.Error(), http.StatusInternalServerError)
return
}
}