我知道客户端_underscore.js可以用来限制点击率,但是如何限制服务器端的呼叫呢?我想过使用相同的模式但不幸的是_throttle似乎不允许区分Meteor.userId().
Meteor.methods({
doSomething: function(arg1, arg2){
// how can you throttle this without affecting ALL users
}
);
Run Code Online (Sandbox Code Playgroud)
这是一个我已经粗暴对待的程序包 - 但尚未提交给Atmosphere(等到我熟悉最小的并为其编写单元测试).
https://github.com/zeroasterisk/Meteor-Throttle
随意玩它,扩展,修复和贡献(鼓励拉取请求)
这个概念很简单,它只在服务器上运行(应该只运行).
你首先需要想出一个你想要节流的独特钥匙......
例如: Meteor.userId() + 'my-function-name' + 'whatever'
该系统采用了全新的集"油门"和一些辅助方法:
check,set,和purge记录.还有一个辅助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');
}
....
Run Code Online (Sandbox Code Playgroud)
checkThenSet(key, allowedCount, expireInSec) 检查一个密钥,如果通过它,则设置密钥以供将来检查check(key, allowedCount)检查一个密钥,如果存在少于allowedCount(未到期的)记录,它就会通过set(key, expireInSec)设置密钥记录,它将在expireInSec秒后过期,例如:60=未来1分钟purge() 到期不再在时间范围内的所有记录(每次检查时自动调用)throttle(key, allowedCount, expireInSec) - > Throttle.checkThenSet()throttle-check(key, allowedCount) - > Throttle.check()throttle-set(key, expireInSec) - > Throttle.set()| 归档时间: |
|
| 查看次数: |
1545 次 |
| 最近记录: |