【问题标题】:How can you throttle calls server side?您如何限制服务器端的呼叫?
【发布时间】:2013-07-31 07:37:26
【问题描述】:

我知道客户端 _underscore.js 可以用来限制点击率,但是你如何限制服务器端的调用呢?我想过使用相同的模式,但不幸的是 _throttle 似乎不允许区分 Meteor.userId() 的。

Meteor.methods({
  doSomething: function(arg1, arg2){
    // how can you throttle this without affecting ALL users
  }
);

【问题讨论】:

    标签: meteor


    【解决方案1】:

    目前在流星中没有内置支持,但它在路线图上https://trello.com/c/SYcbkS3q/18-dos-hardening-rate-limiting

    理论上你可以在这里使用一些选项Throttling method calls to M requests in N seconds,但你必须推出自己的解决方案

    【讨论】:

      【解决方案2】:

      这是一个我已经粗略完成但尚未提交给 Atmosphere 的包(等到我熟悉 tinytest 并为其编写单元测试)。

      https://github.com/zeroasterisk/Meteor-Throttle

      随意使用、扩展、修复和贡献(鼓励请求请求)

      这个概念很简单,它只在服务器上运行(应该只运行)。

      你首先需要为你想要限制的东西想出一个唯一的键......

      例如:Meteor.userId() + 'my-function-name' + 'whatever'

      此系统使用新的 Collection 'throttle' 和一些辅助方法来: checksetpurge 记录。还有一个帮手checkThenSet 方法实际上是最常见的模式,检查我们是否可以做某事, 并创造了我们所做的记录。

      用法

      (用例) 如果您的应用正在发送电子邮件,您不会希望通过以下方式发送相同的电子邮件 一遍又一遍,即使用户触发了它。

      // on server
      if (!Throttle.checkThenSet(key, allowedCount, expireInSec)) {
        throw new Meteor.Error(500, 'You may only send ' + allowedCount + ' emails at a time, wait a while and try again');
      }
      ....
      

      关于节流方法

      • checkThenSet(key, allowedCount, expireInSec) 检查密钥,如果通过,则设置密钥以供将来检查
      • check(key, allowedCount) 检查一个键,如果存在的(未过期)记录少于 allowedCount,则通过
      • set(key, expireInSec)为key设置了一条记录,在expireInSec秒后过期,例如:60=1分钟后
      • purge() 过期所有不再在时间范围内的记录(每次检查时自动调用)

      方法(可调用)

      • throttle(key, allowedCount, expireInSec) --> Throttle.checkThenSet()
      • throttle-check(key, allowedCount) --> Throttle.check()
      • throttle-set(key, expireInSec) --> Throttle.set()

      【讨论】:

      • 请注意 link-only answers 是不鼓励的,所以答案应该是寻找解决方案的终点(而不是另一个参考中途停留,随着时间的推移往往会变得陈旧)。请考虑在此处添加独立的概要,并保留链接作为参考。
      猜你喜欢
      • 2013-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多