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

Side by Side Diff: filter/dscache/ds_txn_state.go

Issue 1269113005: A transparent cache for datastore, backed by memcache. (Closed) Base URL: https://github.com/luci/gae.git@add_meta
Patch Set: add test for per-model expiration Created 5 years, 4 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package dscache
6
7 import (
8 "sync"
9
10 "github.com/luci/gae/service/datastore"
11 "github.com/luci/gae/service/memcache"
12 )
13
14 type dsTxnState struct {
15 sync.Mutex
16
17 toLock []memcache.Item
18 toDelete map[string]struct{}
19 }
20
21 // Reset sets the transaction state back to its 0 state. This is used so that
22 // when a transaction retries the function, we don't accidentally leak state
23 // from one function to the next.
24 func (s *dsTxnState) Reset() {
25 s.Lock()
26 defer s.Unlock()
27 // reduce capacity back to 0, but keep the allocated array. If the trans action
28 // body retries, it'll probably end up re-allocating the same amount of space
29 // anyway.
30 s.toLock = s.toLock[:0]
31 s.toDelete = make(map[string]struct{}, len(s.toDelete))
32 }
33
34 func (s *dsTxnState) Apply(sc *supportContext) {
dnj 2015/08/05 18:32:18 Is there a reason these methods are exported? Mayb
iannucci 2015/08/06 01:54:01 Done.
35 s.Lock()
36 defer s.Unlock()
37
38 err := sc.mc.SetMulti(s.toLock)
dnj 2015/08/05 18:32:18 IMO copy "toLock" while locked, but do the SetMult
Vadim Sh. 2015/08/06 01:23:34 It took me a while to figure out what's going on.
iannucci 2015/08/06 01:54:01 That could only happen if we're ending the transac
iannucci 2015/08/06 02:37:33 Done.
39 if err != nil {
40 sc.log.Errorf("dscache: txn.Apply SetMulti: %s", err)
dnj 2015/08/05 18:32:17 Debugf?
iannucci 2015/08/06 01:54:01 this ones a hard failure
41 }
42 }
43
44 func (s *dsTxnState) Release(sc *supportContext) {
45 s.Lock()
46 defer s.Unlock()
47
48 delKeys := make([]string, 0, len(s.toDelete))
49 for k := range s.toDelete {
50 delKeys = append(delKeys, k)
51 }
52
53 err := sc.mc.DeleteMulti(delKeys)
dnj 2015/08/05 18:32:18 IMO build "delKeys" while locked, but do DelMulti
iannucci 2015/08/06 01:54:01 Also can only happen if we're ending the transacti
54 if err != nil {
55 sc.log.Errorf("dscache: txn.Release DeleteMulti: %s", err)
56 }
57 }
58
59 func (s *dsTxnState) Add(sc *supportContext, keys []datastore.Key) {
60 lockItems, lockKeys := sc.mkAllLockItems(keys)
61 if lockItems == nil {
62 return
63 }
64
65 s.Lock()
66 defer s.Unlock()
67
68 for i, li := range lockItems {
69 k := lockKeys[i]
70 if _, ok := s.toDelete[k]; !ok {
71 s.toLock = append(s.toLock, li)
72 s.toDelete[k] = struct{}{}
73 }
74 }
75 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698