Chromium Code Reviews

Side by Side Diff: go/src/infra/gae/epservice/example/service_cas.go

Issue 1240573002: Reland: Refactor current GAE abstraction library to be free of the SDK* (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: expand coverage range to fit 32bit test expectations Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff |
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package example 5 package example
6 6
7 import ( 7 import (
8 "golang.org/x/net/context" 8 "golang.org/x/net/context"
9 » "infra/gae/libs/wrapper" 9 » "infra/gae/libs/gae"
10 » "infra/gae/libs/wrapper/gae" 10 » "infra/gae/libs/gae/prod"
11 11
12 "github.com/GoogleCloudPlatform/go-endpoints/endpoints" 12 "github.com/GoogleCloudPlatform/go-endpoints/endpoints"
13 ) 13 )
14 14
15 // CASReq is the input for the CAS RPC 15 // CASReq is the input for the CAS RPC
16 type CASReq struct { 16 type CASReq struct {
17 Name string `endpoints:"required"` 17 Name string `endpoints:"required"`
18 18
19 OldVal int64 `json:",string"` 19 OldVal int64 `json:",string"`
20 NewVal int64 `json:",string"` 20 NewVal int64 `json:",string"`
21 } 21 }
22 22
23 // CAS does an atomic compare-and-swap on a counter. 23 // CAS does an atomic compare-and-swap on a counter.
24 func (Example) CAS(c endpoints.Context, r *CASReq) (err error) { 24 func (Example) CAS(c context.Context, r *CASReq) (err error) {
25 success := false 25 success := false
26 » ds := wrapper.GetDS(gae.Use(context.Background(), c)) 26 » c = prod.Use(c)
27 » err = ds.RunInTransaction(func(context.Context) error { 27 » err = gae.GetRDS(c).RunInTransaction(func(c context.Context) error {
28 » » ctr := &Counter{ID: r.Name} 28 » » rds := gae.GetRDS(c)
29 » » if err := ds.Get(ctr); err != nil { 29 » » key := rds.NewKey("Counter", r.Name, 0, nil)
30 » » ctr := &Counter{}
31 » » if err := rds.Get(key, ctr); err != nil {
30 return err 32 return err
31 } 33 }
32 if ctr.Val == r.OldVal { 34 if ctr.Val == r.OldVal {
33 success = true 35 success = true
34 ctr.Val = r.NewVal 36 ctr.Val = r.NewVal
35 » » » _, err := ds.Put(ctr) 37 » » » _, err := rds.Put(key, ctr)
36 return err 38 return err
37 } 39 }
38 success = false 40 success = false
39 return nil 41 return nil
40 }, nil) 42 }, nil)
41 if err == nil && !success { 43 if err == nil && !success {
42 err = endpoints.ConflictError 44 err = endpoints.ConflictError
43 } 45 }
44 return 46 return
45 } 47 }
46 48
47 func init() { 49 func init() {
48 mi["CAS"] = &endpoints.MethodInfo{ 50 mi["CAS"] = &endpoints.MethodInfo{
49 Path: "counter/{Name}/cas", 51 Path: "counter/{Name}/cas",
50 Desc: "Compare and swap a counter value", 52 Desc: "Compare and swap a counter value",
51 } 53 }
52 } 54 }
OLDNEW
« no previous file with comments | « go/src/infra/gae/epservice/example/service_add.go ('k') | go/src/infra/gae/epservice/example/service_currentvalue.go » ('j') | no next file with comments »

Powered by Google App Engine