您现在的位置是:首页 > 电脑 > 

云中的Firestore函数需要几分钟来初始化并发出第一个请求

2025-07-21 09:46:03
云中的Firestore函数需要几分钟来初始化并发出第一个请求 对于在Cloud Function上运行的这个简单的Firestore查询,如果我们有一段时间没有调用它并需要创建一个新的客户端连接,则总是要花很长时间(最长的时间是100+秒)下面是最小可复制index.ts和详细日志。我尝试同时使用admin.firestore()和new Firestore({ grpc })并得到了相似的结

云中的Firestore函数需要几分钟来初始化并发出第一个请求

对于在Cloud Function上运行的这个简单的Firestore查询,如果我们有一段时间没有调用它并需要创建一个新的客户端连接,则总是要花很长时间(最长的时间是100+秒)

下面是最小可复制和详细日志。我尝试同时使用admin.firestore()new Firestore({ grpc })并得到了相似的结果。

import {https, config} from 'firebase-functi';
import * as admin from "firebase-admin";
ct { Firestore } = require('@google-cloud/firestore');
ct grpc = require('@grpc/grpc-js');

let initialized = false;

 = (async (req:any, res) => {
        res.json({received: true});

    if (!initialized) {
      admin.initializeApp(config().firebase);
      admin.firestore.setLogFunction(cole.log);
      initialized = true;
    }
    // using gRPC
    cole.log('using gRPC via @google-cloud/firestore')
    ct db = new Firestore({ grpc });
    // using admin node SDK
    // ct db = admin.firestore();

    await ("debugCollection")
        .doc("debug")
        .get();
    }
  );

我在Firestore中启用了调试日志记录,发现初始化Firestore GAPIC客户端花了47秒。

Firestore (.7.5) 2020-05-04T20:7:2.541Z ##### [Firestore]: Initialized Firestore GAPIC Client
  1:6:6.054 PM
  timeQuery
  Firestore (.7.5) 2020-05-04T20:6:6.054Z peRnr [ClientPool.acquire]: Creating a new client

并且在发送请求和接收查询响应之间,又花了5秒钟。

Firestore (.7.5) 2020-05-04T20:8:00.19Z peRnr [Firestore.requestStream]: Received respe: {"transaction":{"type":"Buffer","data":[]},"readTime":{"seconds":"1588624676","nanos":86619000},"missing":"projects/xxxxxxxx-my-project-id/databases/(default)/documents/debugCollection/debug","result":"missing"}
  1:7:25.141 PM
  timeQuery
  Firestore (.7.5) 2020-05-04T20:7:25.141Z peRnr [Firestore.requestStream]: Sending request: {"database":"projects/xxxxxxxx-my-project-id/databases/(default)","documents":["projects/xxxxxxxx-my-project-id/databases/(default)/documents/debugCollection/debug"]}

这里是详细信息日志。


  1:8:05.440 PM
  timeQuery
  Firestore (.7.5) 2020-05-04T20:8:05.440Z peRnr [Firestore.getAll_]: Received 1 results
  1:8:05.240 PM
  timeQuery
  Firestore (.7.5) 2020-05-04T20:8:05.240Z peRnr [Firestore._initializeStream]: Received stream end
  1:8:00.40 PM
  timeQuery
  Firestore (.7.5) 2020-05-04T20:8:00.40Z peRnr [Firestore.getAll_]: Document missing: projects/xxxxxxxx-my-project-id/databases/(default)/documents/debugCollection/debug
  1:8:00.141 PM
  timeQuery
  Firestore (.7.5) 2020-05-04T20:8:00.140Z peRnr [Firestore._initializeStream]: Releasing stream
  1:8:00.141 PM
  timeQuery
  Firestore (.7.5) 2020-05-04T20:8:00.19Z peRnr [Firestore.requestStream]: Received respe: {"transaction":{"type":"Buffer","data":[]},"readTime":{"seconds":"1588624676","nanos":86619000},"missing":"projects/lovecaster-staging/databases/(default)/documents/debugCollection/debug","result":"missing"}
  1:7:25.141 PM
  timeQuery
  Firestore (.7.5) 2020-05-04T20:7:25.141Z peRnr [Firestore.requestStream]: Sending request: {"database":"projects/xxxxxxxx-my-project-id/databases/(default)","documents":["projects/xxxxxxxx-my-project-id/databases/(default)/documents/debugCollection/debug"]}
  1:7:25.140 PM
  timeQuery
  Firestore (.7.5) 2020-05-04T20:7:25.140Z peRnr [ClientPool.acquire]: Re-using existing client with 100 remaining operati
  1:7:24.440 PM
  timeQuery
  Firestore (.7.5) 2020-05-04T20:7:24.440Z ##### [Firestore.initializeIfeeded]: Detected project ID: xxxxxxxxxxx-my-projectID
  1:7:2.541 PM
  timeQuery
  Firestore (.7.5) 2020-05-04T20:7:2.541Z ##### [Firestore]: Initialized Firestore GAPIC Client
  1:6:6.054 PM
  timeQuery
  Firestore (.7.5) 2020-05-04T20:6:6.054Z peRnr [ClientPool.acquire]: Creating a new client
  1:6:6.052 PM
  timeQuery
  Firestore (.7.5) 2020-05-04T20:6:6.051Z ##### [Firestore]: Initialized Firestore
  1:6:6.050 PM
  timeQuery
  using gRPC via @google-cloud/firestore
  1:6:6.04 PM
  timeQuery
  Function execution took 54 ms, finished with status code: 200
  1:6:5.989 PM
  timeQuery
  Function execution started

和节点包版本:"firebase-admin": "^8.11.0", "@grpc/grpc-js": "0.8.1", "@google-cloud/firestore": ".7.5",

回答如下:

您的功能具有这种方式,因为您在所有工作完成之前就发送了响应。根据documentation,在发送响应后立即终止功能。之后的任何其他工作可能无法正确完成。因此,在函数中,对res.json({received: true});的调用应该是非常[的东西。

[如果您希望您的函数立即将响应发送给客户端,并继续在客户端之外进行工作,则您将无法使用单个HTTP函数来执行此操作。相反,您将不得不将该工作卸载到另一个服务或功能。通常会向pubsub function发送一条消息,以使其在第一个功能中断的后台进行接收。

#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格

本文地址:http://www.dnpztj.cn/diannao/884730.html

相关标签:无
上传时间: 2024-05-07 11:53:52
留言与评论(共有 11 条评论)
本站网友 安家盛
12分钟前 发表
05.440Z peRnr [Firestore.getAll_]
本站网友 马丽萍
5分钟前 发表
"documents"
本站网友 鸡尾酒会醉吗
22分钟前 发表
finished with status code
本站网友 蹦极的感觉
11分钟前 发表
7
本站网友 吉米祛斑
20分钟前 发表
7
本站网友 黄曲霉毒素检测方法
22分钟前 发表
6
本站网友 华北水利水电
14分钟前 发表
发现初始化Firestore GAPIC客户端花了47秒
本站网友 老生常谈
16分钟前 发表
{"seconds"
本站网友 飞拓无线
2分钟前 发表
6
本站网友 电脑性能检测
7分钟前 发表
云中的Firestore函数需要几分钟来初始化并发出第一个请求 对于在Cloud Function上运行的这个简单的Firestore查询