Close redis connection on google cloud function end

大城市里の小女人 提交于 2020-05-18 01:54:07

问题


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)

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();
});

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


回答1:


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

You have two choices:

  • Open and close connections at every function invocation
  • Use connection pooling and manage connection errors which goes against the intended usage of the 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.

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




回答2:


There are no lifecycle listeners defined in Google Cloud Functions. Each Cloud Function needs to run as shortly as possible, and close any resources it opened once it's done processing.



来源:https://stackoverflow.com/questions/57647889/close-redis-connection-on-google-cloud-function-end

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!