Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1767)

Unified Diff: appengine/datastorecache/lock.go

Issue 2645763002: datastorecache: Add Locker field, public settings. (Closed)
Patch Set: Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « appengine/datastorecache/handler.go ('k') | appengine/datastorecache/model.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: appengine/datastorecache/lock.go
diff --git a/appengine/datastorecache/lock.go b/appengine/datastorecache/lock.go
new file mode 100644
index 0000000000000000000000000000000000000000..c816e47c8c79045f8455bdb82774c980b2b2782e
--- /dev/null
+++ b/appengine/datastorecache/lock.go
@@ -0,0 +1,62 @@
+// Copyright 2016 The LUCI Authors. All rights reserved.
+// Use of this source code is governed under the Apache License, Version 2.0
+// that can be found in the LICENSE file.
+
+package datastorecache
+
+import (
+ "strings"
+
+ "github.com/luci/luci-go/appengine/memlock"
+
+ "github.com/luci/gae/service/info"
+
+ "golang.org/x/net/context"
+)
+
+// ErrFailedToLock is a sentinel error returned by Locker.TryWithLock if the
+// lock is already held by another entity.
+var ErrFailedToLock = memlock.ErrFailedToLock
+
+// Locker is an interface to a generic locking function.
+type Locker interface {
+ // TryWithLock blocks on acquiring a lock for the specified key, invokes the
+ // supplied function while holding the lock, and releases the lock before
+ // returning.
+ //
+ // If the lock is already held, TryWithLock should return ErrFailedToLock.
+ // Otherwise, TryWithLock will forward the return value of fn.
+ TryWithLock(c context.Context, key string, fn func(context.Context) error) error
+}
+
+// memLocker is a Locker implementation that uses the memlock library.
+type memLocker struct {
+ clientID string
+}
+
+// MemLocker returns a Locker instance that uses a memcache lock bound to the
+// current request ID.
+func MemLocker(c context.Context) Locker {
+ return &memLocker{
+ clientID: strings.Join([]string{
+ "datastore_cache",
+ info.RequestID(c),
+ }, "\x00"),
+ }
+}
+
+func (m *memLocker) TryWithLock(c context.Context, key string, fn func(context.Context) error) error {
+ switch err := memlock.TryWithLock(c, key, m.clientID, fn); err {
+ case memlock.ErrFailedToLock:
+ return ErrFailedToLock
+ default:
+ return err
+ }
+}
+
+// nopLocker is a Locker instance that performs no actual locking.
+type nopLocker struct{}
+
+func (nopLocker) TryWithLock(c context.Context, key string, fn func(context.Context) error) error {
+ return fn(c)
+}
« no previous file with comments | « appengine/datastorecache/handler.go ('k') | appengine/datastorecache/model.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698