Index: filter/dscache/ds_txn_state.go |
diff --git a/filter/dscache/ds_txn_state.go b/filter/dscache/ds_txn_state.go |
new file mode 100644 |
index 0000000000000000000000000000000000000000..88345f82f7c16851a0b25654b6a6c012014eb532 |
--- /dev/null |
+++ b/filter/dscache/ds_txn_state.go |
@@ -0,0 +1,65 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+package dscache |
+ |
+import ( |
+ "sync" |
+ |
+ "github.com/luci/gae/service/datastore" |
+ "github.com/luci/gae/service/memcache" |
+ "github.com/luci/luci-go/common/logging" |
+) |
+ |
+type dsTxnState struct { |
+ sync.Mutex |
+ |
+ toLock []memcache.Item |
+ toDelete []string |
+} |
+ |
+// Reset sets the transaction state back to its 0 state. This is used so that |
+// when a transaction retries the function, we don't accidentally leak state |
+// from one function to the next. |
+func (s *dsTxnState) Reset() { |
+ s.Lock() |
+ defer s.Unlock() |
+ // reduce capacity back to 0, but keep the allocated array. If the transaction |
+ // body retries, it'll probably end up re-allocating the same amount of space |
+ // anyway. |
+ s.toLock = s.toLock[:0] |
+ s.toDelete = s.toDelete[:0] |
+} |
+ |
+func (s *dsTxnState) Apply(mc memcache.Interface, log logging.Logger) { |
+ s.Lock() |
+ defer s.Unlock() |
+ |
+ err := mc.SetMulti(s.toLock) |
+ if err != nil { |
+ log.Errorf("dscache: txn.Apply SetMulti: %s", err) |
+ } |
+} |
+ |
+func (s *dsTxnState) Release(mc memcache.Interface, log logging.Logger) { |
+ s.Lock() |
+ defer s.Unlock() |
+ |
+ err := mc.DeleteMulti(s.toDelete) |
+ if err != nil { |
+ log.Errorf("dscache: txn.Release DeleteMulti: %s", err) |
+ } |
+} |
+ |
+func (s *dsTxnState) Add(mc memcache.Interface, keys []datastore.Key) { |
+ lockItems, lockKeys := mkAllLockItems(mc, keys) |
+ if lockItems == nil { |
+ return |
+ } |
+ |
+ s.Lock() |
+ defer s.Unlock() |
+ s.toLock = append(s.toLock, lockItems...) |
+ s.toDelete = append(s.toDelete, lockKeys...) |
+} |