acquire_handler.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package lock
  2. import (
  3. "net/http"
  4. "path"
  5. "strconv"
  6. "github.com/gorilla/mux"
  7. )
  8. // acquireHandler attempts to acquire a lock on the given key.
  9. func (h *handler) acquireHandler(w http.ResponseWriter, req *http.Request) {
  10. vars := mux.Vars(req)
  11. keypath := path.Join(prefix, vars["key"])
  12. ttl, err := strconv.Atoi(vars["ttl"])
  13. if err != nil {
  14. http.Error(w, "invalid ttl: " + err.Error(), http.StatusInternalServerError)
  15. return
  16. }
  17. // Create an incrementing id for the lock.
  18. resp, err := h.client.AddChild(keypath, "X", ttl)
  19. if err != nil {
  20. http.Error(w, "add lock index error: " + err.Error(), http.StatusInternalServerError)
  21. return
  22. }
  23. // Extract the lock index.
  24. index, _ := strconv.Atoi(path.Base(resp.Key))
  25. // Read all indices.
  26. resp, err = h.client.GetAll(key)
  27. if err != nil {
  28. http.Error(w, "lock children lookup error: " + err.Error(), http.StatusInternalServerError)
  29. return
  30. }
  31. indices := extractResponseIndices(resp)
  32. // TODO: child_keys := parse_and_sort_child_keys
  33. // TODO: if index == min(child_keys) then return 200
  34. // TODO: else:
  35. // TODO: h.client.WatchAll(key)
  36. // TODO: if next_lowest_key is deleted
  37. // TODO: get_all_keys
  38. // TODO: if index == min(child_keys) then return 200
  39. // TODO: rinse_and_repeat until we're the lowest.
  40. // TODO:
  41. }