
分布式计数器

Made by Firebase
大规模记录事件计数器,以适应对 Cloud Firestore 执行的高速写入操作。
此扩展程序的运作方式
Use this extension to add a highly scalable counter service to your app. This is ideal for applications that count viral actions or any very high-velocity action such as views, likes, or shares.
Since Cloud Firestore has a limit of one sustained write per second, per document, this extension instead shards your writes across documents in a
_counter_shards_
subcollection. Each client only increments their own unique shard while the background workers (provided by this extension) monitor and aggregate these shards into a main document.
Here are some features of this extension:
- Scales from 0 updates per second to a maximum of 10,000 per second.
- Supports an arbitrary number of counters in your app.
- Works offline and provides latency compensation for the main counter.
Note that this extension requires client-side logic to work. We provide a TypeScript client sample implementation and its compiled minified JavaScript . You can use this extension on other platforms if you'd like to develop your own client code based on the provided client sample.
We also provide a Node.js admin sample implementation
Additional setup
Before installing this extension, make sure that you've set up a Cloud Firestore database in your Firebase project.
After installing this extension, you'll need to:
- Update your database security rules .
- Use the provided client sample , the provided Node.js admin sample or your own client code to specify your document path and increment values.
Detailed information for these post-installation tasks are provided after you install this extension.
Billing
To install an extension, your project must be on the Blaze (pay as you go) plan
- You will be charged a small amount (typically around $0.01/month) for the Firebase resources required by this extension (even if it is not used).
- This extension uses other Firebase and Google Cloud Platform services, which have associated charges if you exceed the service’s free tier:
- Cloud Firestore
- Cloud Functions (Node.js 10+ runtime. See FAQs )
如何安装此扩展程序
使用 Firebase CLI
要安装和管理扩展程序,您还可以使用 Firebase CLI:
步骤 1 :运行以下 npm 命令来安装 CLI 或将 CLI 更新到最新版本。
npm install -g firebase-tools不能运行?请参阅 Firebase CLI 参考文档 或者更改您的 npm 权限 。
第 2 步 : 设置一个新的 Firebase 项目目录 或转到现有目录
第 3 步 :将此扩展程序添加到 扩展程序清单 中
firebase ext:install firebase/firestore-counter --local --project=projectId_or_alias
Step 4 (Optional): Test this extension locally with the Firebase Emulator Suite
firebase emulators:start
第 5 步 :将清单中的扩展程序 部署 到项目中
firebase deploy --only extensions --project=projectId_or_alias