mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
mod_lock
This commit is contained in:
44
mod/lock/handler.go
Normal file
44
mod/lock/handler.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package lock
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"time"
|
||||
|
||||
"github.com/coreos/go-etcd/etcd"
|
||||
)
|
||||
|
||||
// handler manages the lock HTTP request.
|
||||
type handler struct {
|
||||
*mux.Router
|
||||
client string
|
||||
}
|
||||
|
||||
// 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.HandleFunc("/{key:.+}", h.getLockHandler).Methods("GET")
|
||||
h.HandleFunc("/{key:.+}", h.acquireLockHandler).Methods("PUT")
|
||||
h.HandleFunc("/{key:.+}", h.releaseLockHandler).Methods("DELETE")
|
||||
}
|
||||
|
||||
// getLockHandler retrieves whether a lock has been obtained for a given key.
|
||||
func (h *handler) getLockHandler(w http.ResponseWriter, req *http.Request) {
|
||||
// TODO
|
||||
}
|
||||
|
||||
// acquireLockHandler attempts to acquire a lock on the given key.
|
||||
// The lock is released when the connection is disconnected.
|
||||
func (h *handler) acquireLockHandler(w http.ResponseWriter, req *http.Request) {
|
||||
// TODO
|
||||
}
|
||||
|
||||
// releaseLockHandler forces the release of a lock on the given key.
|
||||
func (h *handler) releaseLockHandler(w http.ResponseWriter, req *http.Request) {
|
||||
// TODO
|
||||
}
|
||||
35
mod/lock/handler_test.go
Normal file
35
mod/lock/handler_test.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package lock
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// Ensure that a lock can be acquired and released.
|
||||
func TestModLockAcquire(t *testing.T) {
|
||||
// TODO: Acquire lock.
|
||||
// TODO: Check that it has been acquired.
|
||||
// TODO: Release lock.
|
||||
// TODO: Check that it has been released.
|
||||
}
|
||||
|
||||
// Ensure that a lock can be acquired and another process is blocked until released.
|
||||
func TestModLockAcquireBlocked(t *testing.T) {
|
||||
// TODO: Acquire lock with process #1.
|
||||
// TODO: Acquire lock with process #2.
|
||||
// TODO: Check that process #2 has not obtained lock.
|
||||
// TODO: Release lock from process #1.
|
||||
// TODO: Check that process #2 obtains the lock.
|
||||
// TODO: Release lock from process #2.
|
||||
// TODO: Check that no lock exists.
|
||||
}
|
||||
|
||||
// Ensure that an unowned lock can be released by force.
|
||||
func TestModLockForceRelease(t *testing.T) {
|
||||
// TODO: Acquire lock.
|
||||
// TODO: Check that it has been acquired.
|
||||
// TODO: Force release lock.
|
||||
// TODO: Check that it has been released.
|
||||
// TODO: Check that acquiring goroutine is notified that their lock has been released.
|
||||
}
|
||||
@@ -5,14 +5,17 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/coreos/etcd/mod/dashboard"
|
||||
"github.com/coreos/etcd/mod/lock"
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
var ServeMux *http.Handler
|
||||
|
||||
func HttpHandler() (handler http.Handler) {
|
||||
modMux := mux.NewRouter()
|
||||
modMux.PathPrefix("/dashboard/").
|
||||
r := mux.NewRouter()
|
||||
r.PathPrefix("/dashboard/").
|
||||
Handler(http.StripPrefix("/dashboard/", dashboard.HttpHandler()))
|
||||
return modMux
|
||||
r.PathPrefix("/lock/").
|
||||
Handler(http.StripPrefix("/lock", lock.NewHandler()))
|
||||
return r
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user