setGlobalOptions 的 Firebase 云函数错误

RmT*_*mal 2 firebase google-cloud-functions

我是 Firebase 云功能的新用户。当我想部署该功能时遇到错误。这是一个用于测试的简单函数。

但我每次都会遇到这个问题。

!  functions: Your current project quotas don't allow for the current max instances setting of 100. Either reduce this function's maximum instances, or request a quota increase on the underlying Cloud Run service at https://cloud.google.com/run/quotas.
!  functions: You can adjust the max instances value in your function's runtime options:
        setGlobalOptions({maxInstances: 10})
Failed to create function projects/orbaic-53e08/locations/us-central1/functions/helloWorld
Run Code Online (Sandbox Code Playgroud)

我只是尝试做一个 hello 函数而不是更多。

const {onRequest} = require("firebase-functions/v2/https");
const logger = require("firebase-functions/logger");
const { setGlobalOptions } = require("firebase-functions/v2");


exports.helloWorld = onRequest((request, response) => {
  logger.info("Hello logs!", {structuredData: true});
  response.send("Hello from Firebase!");
});
Run Code Online (Sandbox Code Playgroud)

我现在能做什么?

Roh*_*che 5

您收到的错误是因为根据最大实例限制,您必须超过 HTTP 和事件驱动函数的默认限制 100,或者您可以将 firebase.json 中的 maxInstances 设置设置为超过 100 或其他超过的值默认限制为 100 个实例。

正如错误所示,您可以使用 setGlobalOptions 为所有函数指定 maxInstances,如下所示:

const {setGlobalOptions} = require("firebase-functions/v2/options");
const {onRequest} = require("firebase-functions/v2/https");
const logger = require("firebase-functions/logger");

// Set the maximum instances to 10 for all functions
setGlobalOptions({maxInstances: 10});

exports.helloWorld = onRequest((request, response) => {
  logger.info("Hello logs!", {structuredData: true});
  response.send("Hello from Firebase!");
});
Run Code Online (Sandbox Code Playgroud)

更新 :

实例是运行函数代码的单个执行环境。每个实例都与其他实例隔离,因此我们的函数代码将无法访问其他实例的状态。

默认情况下,Cloud Functions 根据传入请求的数量缩放实例数量。这意味着当有更多请求时,Cloud Functions 将创建更多实例来处理负载。当请求较少时,Cloud Functions 会销毁实例以节省资源。

有关更多信息,您可以观看Cloud Functions for Firebase - 教程视频系列