2
0
mirror of https://github.com/etcd-io/etcd.git synced 2024-09-27 06:25:44 +00:00
2013-11-27 14:36:14 -07:00

56 lines
1.2 KiB
Go

package lock
import (
"fmt"
"net/http"
"path"
"github.com/gorilla/mux"
"github.com/coreos/go-etcd/etcd"
)
const prefix = "/_etcd/locks"
// handler manages the lock HTTP request.
type handler struct {
*mux.Router
client *etcd.Client
}
// NewHandler creates an HTTP handler that can be registered on a router.
func NewHandler(addr string) (http.Handler) {
h := &handler{
Router: mux.NewRouter(),
client: etcd.NewClient([]string{addr}),
}
h.StrictSlash(false)
h.HandleFunc("/{key:.*}", h.acquireHandler).Methods("POST")
h.HandleFunc("/{key_with_index:.*}", h.renewLockHandler).Methods("PUT")
h.HandleFunc("/{key_with_index:.*}", h.releaseLockHandler).Methods("DELETE")
return h
}
// extractResponseIndices extracts a sorted list of indicies from a response.
func extractResponseIndices(resp *etcd.Response) []int {
var indices []int
for _, kv := range resp.Kvs {
if index, _ := strconv.Atoi(path.Base(kv.Key)); index > 0 {
indicies = append(indices, index)
}
}
return indices
}
// findPrevIndex retrieves the previous index before the given index.
func findPrevIndex(indices []int, idx int) int {
var prevIndex int
for _, index := range indices {
if index == idx {
break
}
prevIndex = index
}
return prevIndex
}