-
Notifications
You must be signed in to change notification settings - Fork 6.4k
/
distributed_counters.py
81 lines (63 loc) · 2.64 KB
/
distributed_counters.py
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
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# [START firestore_solution_sharded_counter_custom_type_async]
import random
from google.cloud import firestore
class Shard:
"""
A shard is a distributed counter. Each shard can support being incremented
once per second. Multiple shards are needed within a Counter to allow
more frequent incrementing.
"""
def __init__(self):
self._count = 0
def to_dict(self):
return {"count": self._count}
class Counter:
"""
A counter stores a collection of shards which are
summed to return a total count. This allows for more
frequent incrementing than a single document.
"""
def __init__(self, num_shards):
self._num_shards = num_shards
# [END firestore_solution_sharded_counter_custom_type_async]
# [START firestore_solution_sharded_counter_create_async]
async def init_counter(self, doc_ref):
"""
Create a given number of shards as
subcollection of specified document.
"""
col_ref = doc_ref.collection("shards")
# Initialize each shard with count=0
for num in range(self._num_shards):
shard = Shard()
await col_ref.document(str(num)).set(shard.to_dict())
# [END firestore_solution_sharded_counter_create_async]
# [START firestore_solution_sharded_counter_increment_async]
async def increment_counter(self, doc_ref):
"""Increment a randomly picked shard."""
doc_id = random.randint(0, self._num_shards - 1)
shard_ref = doc_ref.collection("shards").document(str(doc_id))
return await shard_ref.update({"count": firestore.Increment(1)})
# [END firestore_solution_sharded_counter_increment_async]
# [START firestore_solution_sharded_counter_get_async]
async def get_count(self, doc_ref):
"""Return a total count across all shards."""
total = 0
shards = doc_ref.collection("shards").list_documents()
async for shard in shards:
total += (await shard.get()).to_dict().get("count", 0)
return total
# [END firestore_solution_sharded_counter_get_async]