我正在寻找为 Web 应用程序实现移动时间窗口速率限制算法的有效方法。我正在寻找一种可扩展的算法。
到目前为止,我正在考虑使用分片计数器和内存缓存。
这里是伪语言的算法:
For each request:
1: get the number of requests in the last N minutes from memcache
2: if nothing found in the memcache (memcache flushed or first call?)
3: get the number of requests in the last N minutes from ndb (expensive!)
4: if the number is to high
5: block the request
6: increment the sharding counter
7: increment the memcache value (failsafe, if an error occurs here ignore it)
8: process the request
到目前为止,我发现的其他问题不适用于 App Engine 的上下文。