Browse Source

add request locking

joe 8 years ago
parent
commit
79edb03269
2 changed files with 29 additions and 0 deletions
  1. 27 0
      locks.go
  2. 2 0
      server.go

+ 27 - 0
locks.go

@@ -0,0 +1,27 @@
+package gold
+
+import (
+	"sync"
+)
+
+var (
+	locksL = new(sync.Mutex)
+	locks  = map[string]*sync.Mutex{}
+)
+
+func lock(key string) func() {
+	mu, ex := locks[key]
+	if !ex { // TTAS
+		locksL.Lock()
+		mu, ex = locks[key]
+		if !ex {
+			locks[key] = new(sync.Mutex)
+		}
+		locksL.Unlock()
+		mu = locks[key]
+	}
+	mu.Lock()
+	return func() {
+		mu.Unlock()
+	}
+}

+ 2 - 0
server.go

@@ -151,6 +151,8 @@ func (h *Server) ServeHTTP(w http.ResponseWriter, req0 *http.Request) {
 		log.Printf("user=%s req=%+v\n%+v\n\n", user, req, g)
 	}
 	path = h.GraphPath(g)
+	unlock := lock(path)
+	defer unlock()
 
 	// TODO: WAC
 	origin := ""