简体   繁体   English

将 mono-repo 与 Google Cloud Functions 结合使用

[英]Use of mono-repo with Google Cloud Functions

I have multiple nodejs functions sharing the same repository and dependencies.我有多个 nodejs 函数共享相同的存储库和依赖项。 As advised in official GCF documentation, I use a index.js file which expose multiple functions entrypoints, like this:正如 GCF 官方文档中所建议的那样,我使用了一个公开多个函数入口点的index.js文件,如下所示:

/**
 * GCF Entrypoints
 */

const filenames = fs.readdirSync(__dirname);
const functions = filenames.reduce((acc, filename) => {
  const isJS = /\.js/.test(filename);
  const skipped = /index|sample/.test(filename);

  if (!isJS || skipped) return acc;
  const functionName = filename.replace('.js', '');

  return {
    ...acc,
    [functionName]: require(`./${filename}`).default,
  };
}, {});

Object.assign(exports, functions);

I then execute multiple times the gcloud functions deploy command to deploy theses functions and attach their respective PubSub topics.然后我多次执行gcloud functions deploy命令来部署这些函数并附加它们各自的 PubSub 主题。 So the whole package is uploaded and executed through different entrypoints with the same dependencies.因此整个 package 是通过具有相同依赖关系的不同入口点上传和执行的。

Is there any drawbacks or wrong-doing, if instead, I deploy the entire package as only one GC function, with one PubSub topic, and call it with the entrypoint specified in message payload?如果相反,我将整个 package 部署为一个 GC function,带有一个 PubSub 主题,并使用消息负载中指定的入口点调用它,是否有任何缺点或错误?

What about function performance, warming and scalability? function 的性能、预热和可扩展性如何?

It would ease the CD process a lot.这将大大简化 CD 过程。

There is not any bad practice with your approach, If all functions within your code use the same dependencies and if are fast/simple tasks.如果您的代码中的所有函数都使用相同的依赖项并且是快速/简单的任务,那么您的方法没有任何不好的做法。

Keep in mind that the memory & CPU management will be shared across all your code this means that if one function needs more memory or CPU it could hurt the performance of the other requests that are running concurrently on the same instance.请记住,memory 和 CPU 管理将在所有代码中共享,这意味着如果一个 function 需要更多 memory 或 CPU,它可能会损害在同一实例上同时运行的其他请求的性能。

The main idea of a separate each function is to isolate the necessary dependencies for every function and the processing resources.一个单独的每个 function 的主要思想是隔离每个 function 和处理资源的必要依赖关系。

If your code starts growing and you add more and more dependencies you will end separating the functions to have a better performance.如果您的代码开始增长并且您添加了越来越多的依赖项,您将结束分离功能以获得更好的性能。

In this video you can find information about how your code responsiveness could be impacted by Node.js dependecies.在此视频中,您可以找到有关 Node.js 依赖项如何影响代码响应能力的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM