firebase - Firestore - 单个测试用户导致 50K+ 读取

标签 firebase google-cloud-firestore firebase-security

我最近尝试了一些 Firestore 安全规则,作为单个用户,我发现自己在设置所述规则的第一个小时内就造成了超过 10 万次拒绝。

我还注意到,在过去 30 天里,我的单次使用已经产生了近 1M 的许可,我想我预计一个月内我的平均使用量只会达到 1 - 5K 左右。

我显然预计在规则测试期间会遭到更多拒绝,但为什么数量如此之高?在过去 7 天内,我的数据库总读取数仅达到 916 次,但不知何故也超出了我的使用限制,所以现在我无法访问任何集合。

我的规则集,我打算完善它,因为我无法正确测试它,所以我知道它的错误。

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
     match /users/{userID} {
      allow update, write, delete: if request.auth != null && 
        request.auth.uid == userID;
      allow read, create: if request.auth != null;
      match /public_trips/{tripID}/{likedBy} {
        allow read, write: if request.auth.uid != null &&
            request.resource.data.likedBy == likedBy;
      }
    }
  }
}

image of firestore dashboard, 24 hour period, 7.7K allows, 106K denies, 608 errors

image of firestore dashboard, 30 day period, 862K allows, 642 denies, 608 errors

下图是我根据我执行的测试量所期望的使用情况(以及实际报告的内容)。

image of firestore dashboard 916 reads, 71 writes

image of google quota dashboard, daily operations, 0.05M/0.05M ops used, 100%

以上情况是预料之外的。

按照此配额读取,我已经超出了当天 50K 的读取限制,但正如我所说,firestore 只报告 >1K?

TL;博士

为什么我遭到单个用户超过 10 万次拒绝?

这是因为我的规则不可靠,还是因为我的应用程序效率低下?

最佳答案

TL;DR

您的应用效率低下。


您的规则不会访问数据库中的其他文档,因此不会导致额外的读取。所以你收到如此多的拒绝的原因是你提出了太多的请求。这很可能是因为您对数据进行建模的方式效率不高。

例如,对于餐厅应用程序,您可能会获取所有评论文档来计算平均评分和评分总数。相反,您应该将它们存储为餐厅文档中的字段,并让云函数在添加、更新或删除评论时更新它们。

关于firebase - Firestore - 单个测试用户导致 50K+ 读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64955399/

相关文章:

javascript - 如何将 Firebase 与 Ionic 集成?

javascript - 为什么 img.src 不在每个 "li"中定义 img-tag 的来源?

swift - 是否可以在 firestore 查询上有一个完成 block ?

firebase - Flutter Firestore 地理查询

firebase - 暂时停用 Firebase 数据库

ios - Firebase 控制台在验证 iOS 应用程序上的安装时卡住

javascript - firebase是否支持数组查询?

java - 如何将加载的数据从 Firestore 保存到 Arraylist

cross-domain - Firebase 的跨域政策是什么?

firebase - 在 Firebase 上获取最近创建的用户的 UID