forked from GoogleCloudPlatform/golang-samples
/
counter.go
123 lines (112 loc) · 2.87 KB
/
counter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright 2011 Google Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package sharded_counter
import (
"fmt"
"math/rand"
"golang.org/x/net/context"
"google.golang.org/appengine/datastore"
"google.golang.org/appengine/memcache"
)
type counterConfig struct {
Shards int
}
type shard struct {
Name string
Count int
}
const (
defaultShards = 20
configKind = "GeneralCounterShardConfig"
shardKind = "GeneralCounterShard"
)
func memcacheKey(name string) string {
return shardKind + ":" + name
}
// Count retrieves the value of the named counter.
func Count(ctx context.Context, name string) (int, error) {
total := 0
mkey := memcacheKey(name)
if _, err := memcache.JSON.Get(ctx, mkey, &total); err == nil {
return total, nil
}
q := datastore.NewQuery(shardKind).Filter("Name =", name)
for t := q.Run(ctx); ; {
var s shard
_, err := t.Next(&s)
if err == datastore.Done {
break
}
if err != nil {
return total, err
}
total += s.Count
}
memcache.JSON.Set(ctx, &memcache.Item{
Key: mkey,
Object: &total,
Expiration: 60,
})
return total, nil
}
// Increment increments the named counter.
func Increment(ctx context.Context, name string) error {
// Get counter config.
var cfg counterConfig
ckey := datastore.NewKey(ctx, configKind, name, 0, nil)
err := datastore.RunInTransaction(ctx, func(ctx context.Context) error {
err := datastore.Get(ctx, ckey, &cfg)
if err == datastore.ErrNoSuchEntity {
cfg.Shards = defaultShards
_, err = datastore.Put(ctx, ckey, &cfg)
}
return err
}, nil)
if err != nil {
return err
}
var s shard
err = datastore.RunInTransaction(ctx, func(ctx context.Context) error {
shardName := fmt.Sprintf("%s-shard%d", name, rand.Intn(cfg.Shards))
key := datastore.NewKey(ctx, shardKind, shardName, 0, nil)
err := datastore.Get(ctx, key, &s)
// A missing entity and a present entity will both work.
if err != nil && err != datastore.ErrNoSuchEntity {
return err
}
s.Name = name
s.Count++
_, err = datastore.Put(ctx, key, &s)
return err
}, nil)
if err != nil {
return err
}
memcache.IncrementExisting(ctx, memcacheKey(name), 1)
return nil
}
// IncreaseShards increases the number of shards for the named counter to n.
// It will never decrease the number of shards.
func IncreaseShards(ctx context.Context, name string, n int) error {
ckey := datastore.NewKey(ctx, configKind, name, 0, nil)
return datastore.RunInTransaction(ctx, func(ctx context.Context) error {
var cfg counterConfig
mod := false
err := datastore.Get(ctx, ckey, &cfg)
if err == datastore.ErrNoSuchEntity {
cfg.Shards = defaultShards
mod = true
} else if err != nil {
return err
}
if cfg.Shards < n {
cfg.Shards = n
mod = true
}
if mod {
_, err = datastore.Put(ctx, ckey, &cfg)
}
return err
}, nil)
}