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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: go/src/infra/gae/epservice/example/service_cas.go
diff --git a/go/src/infra/gae/epservice/example/service_cas.go b/go/src/infra/gae/epservice/example/service_cas.go
new file mode 100644
index 0000000000000000000000000000000000000000..f01804ec488218dcbfcd47732a3885d8319f929f
--- /dev/null
+++ b/go/src/infra/gae/epservice/example/service_cas.go
@@ -0,0 +1,52 @@
+// 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 example
+
+import (
+ "golang.org/x/net/context"
+ "infra/gae/libs/wrapper"
+ "infra/gae/libs/wrapper/gae"
+
+ "github.com/GoogleCloudPlatform/go-endpoints/endpoints"
+)
+
+// CASReq is the input for the CAS RPC
+type CASReq struct {
+ Name string `endpoints:"required"`
+
+ OldVal int64 `json:",string"`
+ NewVal int64 `json:",string"`
+}
+
+// CAS does an atomic compare-and-swap on a counter.
+func (Example) CAS(c endpoints.Context, r *CASReq) (err error) {
+ success := false
+ ds := wrapper.GetDS(gae.Use(context.Background(), c))
+ err = ds.RunInTransaction(func(context.Context) error {
+ ctr := &Counter{ID: r.Name}
+ if err := ds.Get(ctr); err != nil {
+ return err
+ }
+ if ctr.Val == r.OldVal {
+ success = true
+ ctr.Val = r.NewVal
+ _, err := ds.Put(ctr)
+ return err
+ }
+ success = false
+ return nil
+ }, nil)
+ if err == nil && !success {
+ err = endpoints.ConflictError
+ }
+ return
+}
+
+func init() {
+ mi["CAS"] = &endpoints.MethodInfo{
+ Path: "counter/{Name}/cas",
+ Desc: "Compare and swap a counter value",
+ }
+}
« 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