gpt4 book ai didi

meteor - 您如何限制服务器端的调用?

转载 作者:行者123 更新时间:2023-12-01 09:00:31 24 4
gpt4 key购买 nike

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

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

最佳答案

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

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

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

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

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

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

此系统使用新的 Collection 'throttle' 和一些辅助方法来:checksetpurge 记录。还有一个helper 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) 检查一个 key ,如果通过则设置该 key 以供将来检查
  • check(key, allowedCount) 检查一个key,如果存在的(未过期)记录少于allowedCount,则通过
  • set(key, expireInSec) 设置key的记录,expireInSec秒后过期,例如:60 = 1 min future
  • purge() 过期所有不再在时间范围内的记录(每次检查时自动调用)

方法(可调用)

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

关于meteor - 您如何限制服务器端的调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17959184/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com