|
@@ -8,9 +8,9 @@ import (
|
|
|
"strconv"
|
|
"strconv"
|
|
|
"time"
|
|
"time"
|
|
|
|
|
|
|
|
- "github.com/gorilla/mux"
|
|
|
|
|
- "github.com/coreos/go-etcd/etcd"
|
|
|
|
|
etcdErr "github.com/coreos/etcd/error"
|
|
etcdErr "github.com/coreos/etcd/error"
|
|
|
|
|
+ "github.com/coreos/go-etcd/etcd"
|
|
|
|
|
+ "github.com/gorilla/mux"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
// acquireHandler attempts to acquire a lock on the given key.
|
|
// acquireHandler attempts to acquire a lock on the given key.
|
|
@@ -75,7 +75,7 @@ func (h *handler) acquireHandler(w http.ResponseWriter, req *http.Request) error
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// createNode creates a new lock node and watches it until it is acquired or acquisition fails.
|
|
// createNode creates a new lock node and watches it until it is acquired or acquisition fails.
|
|
|
-func (h *handler) createNode(keypath string, value string, ttl int, closeChan <- chan bool, stopChan chan bool) (int, error) {
|
|
|
|
|
|
|
+func (h *handler) createNode(keypath string, value string, ttl int, closeChan <-chan bool, stopChan chan bool) (int, error) {
|
|
|
// Default the value to "-" if it is blank.
|
|
// Default the value to "-" if it is blank.
|
|
|
if len(value) == 0 {
|
|
if len(value) == 0 {
|
|
|
value = "-"
|
|
value = "-"
|
|
@@ -133,7 +133,7 @@ func (h *handler) findExistingNode(keypath string, value string) (*etcd.Node, in
|
|
|
func (h *handler) ttlKeepAlive(k string, value string, ttl int, stopChan chan bool) {
|
|
func (h *handler) ttlKeepAlive(k string, value string, ttl int, stopChan chan bool) {
|
|
|
for {
|
|
for {
|
|
|
select {
|
|
select {
|
|
|
- case <-time.After(time.Duration(ttl / 2) * time.Second):
|
|
|
|
|
|
|
+ case <-time.After(time.Duration(ttl/2) * time.Second):
|
|
|
h.client.Update(k, value, uint64(ttl))
|
|
h.client.Update(k, value, uint64(ttl))
|
|
|
case <-stopChan:
|
|
case <-stopChan:
|
|
|
return
|
|
return
|
|
@@ -143,14 +143,14 @@ func (h *handler) ttlKeepAlive(k string, value string, ttl int, stopChan chan bo
|
|
|
|
|
|
|
|
// watch continuously waits for a given lock index to be acquired or until lock fails.
|
|
// watch continuously waits for a given lock index to be acquired or until lock fails.
|
|
|
// Returns a boolean indicating success.
|
|
// Returns a boolean indicating success.
|
|
|
-func (h *handler) watch(keypath string, index int, closeChan <- chan bool) error {
|
|
|
|
|
|
|
+func (h *handler) watch(keypath string, index int, closeChan <-chan bool) error {
|
|
|
// Wrap close chan so we can pass it to Client.Watch().
|
|
// Wrap close chan so we can pass it to Client.Watch().
|
|
|
stopWatchChan := make(chan bool)
|
|
stopWatchChan := make(chan bool)
|
|
|
go func() {
|
|
go func() {
|
|
|
select {
|
|
select {
|
|
|
- case <- closeChan:
|
|
|
|
|
|
|
+ case <-closeChan:
|
|
|
stopWatchChan <- true
|
|
stopWatchChan <- true
|
|
|
- case <- stopWatchChan:
|
|
|
|
|
|
|
+ case <-stopWatchChan:
|
|
|
}
|
|
}
|
|
|
}()
|
|
}()
|
|
|
defer close(stopWatchChan)
|
|
defer close(stopWatchChan)
|