订阅 PubSub 主题的速率限制/限制 google 云函数

Rate limit / throttle google cloud function that subscribes to a PubSub topic

所以我有一个订阅 Pub/Sub 主题的云函数,并且该函数与具有硬速率限制的第 3 方服务 API 交互。这就产生了一个问题,当 PubSub 主题突然收到大量传入流量时,云函数将被频繁调用,足以超过第 3 方 API 速率限制。

解决这个问题的最佳方法是什么?我是否能够限制 PubSub 主题,或者我必须在云函数中实现一些退避逻辑?谢谢!

在某种程度上你可以control the scaling behavior of Cloud Functions。来自链接文档:

You can set max instances for an individual function during deployment. Each function can have its own max instances limit. Functions scale independently of each other.

To set a max instances limit using the gcloud command-line tool, use the --max-instances flag at deploy time:

gcloud beta functions deploy FUNCTION_NAME --max-instances 10 FLAGS...

您必须计算出在另一个 API 的限制内有多少并发实例。如果一个实例仍然能够生成太多请求,您将不得不实施自己的退避,因为您无法设置执行率,只能设置分配实例的最大数量。