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

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

Issue 1153473008: A client/server helper wrapper for endpoints in Go. (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: rm newline Created 5 years, 6 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 example
6
7 import (
8 "golang.org/x/net/context"
9 "infra/gae/libs/wrapper"
10 "infra/gae/libs/wrapper/gae"
11
12 "github.com/GoogleCloudPlatform/go-endpoints/endpoints"
13 )
14
15 // CASReq is the input for the CAS RPC
16 type CASReq struct {
17 Name string `endpoints:"required"`
18
19 OldVal int64 `json:",string"`
20 NewVal int64 `json:",string"`
21 }
22
23 // CAS does an atomic compare-and-swap on a counter.
24 func (Example) CAS(c endpoints.Context, r *CASReq) (err error) {
25 success := false
26 ds := wrapper.GetDS(gae.Use(context.Background(), c))
27 err = ds.RunInTransaction(func(context.Context) error {
28 ctr := &Counter{ID: r.Name}
29 if err := ds.Get(ctr); err != nil {
30 return err
31 }
32 if ctr.Val == r.OldVal {
33 success = true
34 ctr.Val = r.NewVal
35 _, err := ds.Put(ctr)
36 return err
37 }
38 success = false
39 return nil
40 }, nil)
41 if err == nil && !success {
42 err = endpoints.ConflictError
43 }
44 return
45 }
46
47 func init() {
48 mi["CAS"] = &endpoints.MethodInfo{
49 Path: "counter/{Name}/cas",
50 Desc: "Compare and swap a counter value",
51 }
52 }
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
This is Rietveld 408576698