在Google Cloud Function端关闭Redis连接 [英] Close redis connection on google cloud function end

本文介绍了在Google Cloud Function端关闭Redis连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当云功能实例终止时,如何关闭从云功能到Redis内存存储的连接? (我相信要关闭我需要调用redis.quit(),但是我只是不知道什么时候,而且我不能在函数返回后立即关闭它们,因为函数实例可以是

How do you close the connection to a redis memory store from a cloud function when the cloud function instance terminates? (I believe to close I need to call redis.quit(), but I just don't know when, and I cannot close them immediately after a function returns because the function instance can be reused)

因为我只是保持连接打开状态,所以现在我收到"ECONNRESET"错误.

Because I'm just leaving the connections open, right now I am getting "ECONNRESET" errors.

或者,如果类似这样的是不可能的:

Alternatively if something like this is not possible:

process.on("exit", function(){//also process is not defined in cloud functions
    redisClient.quit();
});

在redis配置中指定超时是最好的选择吗? (如何在gcp memorystore中执行此操作?)

Is the best option to specify a timeout in the redis config? (How do you do this in gcp memorystore?)

推荐答案

当您的Cloud Function输入函数返回时,您的容器有资格被终止,恕不另行通知.

When your Cloud Function entry function returns, your container is eligible to be terminated without notice.

您有两种选择:

  • 在每次调用函数时打开和关闭连接
  • 使用连接池并管理与Cloud Functions的预期用法背道而驰的连接错误.

如果您的功能使容器保持温暖,则具有错误处理可能"的连接池将以不小的代价处理错误和测试所有可能的问题而受益. Cloud Functions应用程序应设计为无状态".在Cloud Functions中尝试在两次调用之间保持状态(连接,数据等)不是一个好策略.

If your functions are keeping the container warm, connection pooling with error handling "might" have benefit at a not-insignificant cost of error handling and testing all possible problems. Cloud Functions apps should be designed to be "stateless". Trying to persist state (connections, data, etc) between invocations in Cloud Functions is not a good strategy.

我将使用选项#1设计系统. Cloud Functions是轻量级"的,意味着启动,快速执行任务并关闭.

I would design my system using option #1. Cloud Functions are "lightweight" meaning startup, do the task quickly and shutdown.

这篇关于在Google Cloud Function端关闭Redis连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆