通过HTTP请求调用函数 | 通过HTTP请求触发函数 | 限制

huangapple go评论59阅读模式
英文:

Call functions via HTTP requests | Trigger a function with an HTTP request | Limitations

问题

我因为云函数文档(用法和限制)而在构建我的应用时遇到了很大的困难。我不明白。

我有一个问题,
在我的应用程序中,我不希望允许直接的客户端端读取请求到 Firestore 数据库。 (客户端端 -> Firestore 数据库,通过安全规则)

match /users/{document=**} {
  allow read: if false;
  allow write: if false;
}

但是,我必须允许来自客户端端用户的请求,通过调用异步请求或调用请求。(最终用户 -> 云函数 -> Firestore 数据库)

问题是,云函数是否通过异步请求或应用程序 onCall 请求来管理可伸缩性和负载平衡技术,面对大量同时的流量(100k 用户读取请求)?

例如,如果有 100k 用户同时请求获取用户,会发生什么?

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

exports.getUsers = functions.https.onRequest(async (req, res) => {
  try {
    const usersCollection = admin.firestore().collection('users');

    // 查询在线男性用户,限制为 5 个
    const querySnapshot = await usersCollection
      .where('isOnline', '==', 'online')
      .where('gender', '==', 'male')
      .orderBy('last-active', 'desc')
      .limit(5)
      .get();

    const userList = [];
    querySnapshot.forEach((doc) => {
      const user = doc.data();
      userList.push(user);
    });

    res.status(200).json(userList);
  } catch (error) {
    console.error('Error retrieving users:', error);
    res.status(500).send('Error occurred');
  }
});
英文:

I really have hard time to build my app because of cloud function documentation (usage and limitations). I don't understand

I have a question,
In my app, I don't want to allow direct client-side read requests to firestore database. (client-side -> firestore database with security rules)

match /users/{document=**} {
  allow read: if false;
  allow write: if false;
}

But, I have to allow requests from client-side user through invocating a http async request or on call request. (end-user -> cloud-functions -> firestore database)

question is, Does cloud function manage scalability and load balancing techniques facing lot of traffics(100k user read requests) at a same time via http async request or app onCall request?

Example, what if 100k user request to getusers in same time?

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

exports.getUsers = functions.https.onRequest(async (req, res) => {
  try {
    const usersCollection = admin.firestore().collection('users');

    // Query for online male users with limit 5
    const querySnapshot = await usersCollection
      .where('isOnline', '==', 'online')
      .where('gender', '==', 'male')
      .orderBy('last-active', 'desc')
      .limit(5)
      .get();

    const userList = [];
    querySnapshot.forEach((doc) => {
      const user = doc.data();
      userList.push(user);
    });

    res.status(200).json(userList);
  } catch (error) {
    console.error('Error retrieving users:', error);
    res.status(500).send('Error occurred');
  }
});

答案1

得分: 0

是的,它会根据需要进行扩展以处理请求。

您应该查看文档以了解其行为:

云函数实现了无服务器范例,您可以在其中运行代码,无需担心底层基础设施,如服务器或虚拟机。一旦部署,您的函数将自动进行管理和扩展。
云函数通过将传入请求分配给您函数的实例来处理传入请求。根据请求的数量以及现有函数实例的数量,云函数可以将请求分配给现有实例或创建新实例。
在传入请求量超过现有实例数量的情况下,云函数可能会启动多个新实例来处理请求。这种自动扩展行为允许云函数并行处理许多请求,每个请求使用您函数的不同实例。

您不会得到绝对的性能保证,但无需额外费用即可获得这种扩展行为。

英文:

> Does cloud function manage scalability and load balancing techniques facing lot of traffics(100k user read requests) at a same time via http async request or app onCall request?

Yes, it will scale up as needed to handle the requests as needed.

You should review the documentation to understand its behavior:

> Cloud Functions implements the serverless paradigm, in which you run your code without worrying about the underlying infrastructure, such as servers or virtual machines. Once deployed, your functions are automatically managed and scaled.
>
> Cloud Functions handles incoming requests by assigning them to instances of your function. Depending on the volume of requests, as well as the number of existing function instances, Cloud Functions may assign a request to an existing instance or create a new one.
>
> In cases where inbound request volume exceeds the number of existing instances, Cloud Functions may start multiple new instances to handle requests. This automatic scaling behavior allows Cloud Functions to handle many requests in parallel, each using a different instance of your function.

You do not get an absolute performance guarantee, but you do get this scaling behavior at no extra cost.

huangapple
  • 本文由 发表于 2023年7月11日 12:05:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76658645.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定