云代码功能运行两次 [英] Cloud Code function running twice

查看:96
本文介绍了云代码功能运行两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个运行良好的云功能.

I have written a cloud function that is working well.

有时,对于同一用户,此功能被执行了多次(我确保从客户端android应用仅请求了一个请求).

Sometimes this function is being executed more than one time for the same user (I made sure that only one request is being requested from the client android app).

经过一些调试后,我注意到如果连接不良,则会发生此问题.我可能是正确的,也可能是不正确的.

After some debugging I noticed that this problem happens if the connection is bad. I may be or may not be correct.

如何克服这样的问题?

推荐答案

就像在评论中一样,我也不相信客户端SDK会在连接错误的情况下复制Cloud Function调用.对于他们来说,预期的行为是在客户端抛出与网络相关的异常,而不是再次调用它.如果Cloud Function成功运行并且客户端仅无法获取结果,则会出现问题.

As in the comments I also don't believe the client SDKs would duplicate a Cloud Function call on a bad connection. The expected behaviour would be for them to throw a network-related exception on the client side and not call it again. A problem would arise if the Cloud Function runs successfully and the client is only unable to get the result back.

我可以想到以下解决方案,而没有关于Cloud Function本身的更多详细信息:

I can think of the following solutions, with no more details about the Cloud Function itself:

  1. 尝试使函数幂等-意味着即使运行两次,最终结果也是相同的(假设输入/参数相同).

  1. Try to make the function idempotent - Meaning that even if it runs twice the end result is the same, assuming the same input/parameters.

缓存结果并手动调节功能-这更复杂,只有在网络问题仍然存在并且您无法解决消除该功能的副作用的情况下才需要.您将必须创建另一个Class来缓存函数的结果(可能由参数键入),并返回相同的结果,而不会产生任何计算和副作用.

Cache the results and manually throttle the function - This is more complicated, and is only needed if the network problem persists and you can't work around eliminating the side effects of the function. You would have to create another Class to cache the results of the function (maybe keyed by the parameters) and returning the same result with no computation and side effects.

这篇关于云代码功能运行两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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