限制 Rails 应用程序生成的出站 API 调用 [英] Throttling outbound API calls generated by a Rails app

查看:22
本文介绍了限制 Rails 应用程序生成的出站 API 调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是专业的网络开发人员,但我喜欢将网站作为一种爱好.最近,我一直在开发一个 Rails 应用程序作为一个项目来帮助我学习框架.我的玩具应用程序的目标是通过他们的 API 从另一个服务收集数据,并让我可以使用搜索功能进行查询.

I am not a professional web developer, but I like to wrench on websites as a hobby. Recently, I have been playing with developing a Rails app as a project to help me learn the framework. The goal of my toy app is to harvest data from another service through their API and make it available for me to query using a search function.

但是,我想从中提取数据的服务对每分钟可能执行的 API 调用数量施加了速率限制.我计划让我的应用程序每天运行一次更新,这可能会产生远远超过外部服务提供的限制的大量 API 调用.我希望尊重外部站点的性能,因此希望限制我的应用执行调用的速度.

However, the service I want to pull data from imposes a rate limit on the number of API calls that may be executed per minute. I plan on having my app run a daily update which may generate a burst of API calls that far exceeds the limit provided by the external service. I wish to respect the performance of the external site and so would like to throttle the rate at which my app executes the calls.

我进行了一些搜索,我发现大量的教程材料和预建库涵盖了限制对 Web 应用程序的入站 API 调用,我几乎找不到关于控制的讨论出站呼叫的流程.

I have done a little bit of searching and the overwhelming amount of tutorial material and pre-built libraries I have found cover throttling inbound API calls to a web app and I can find little discussion of controlling the flow of outbound calls.

作为一名业余 Web 开发人员和 Rails 新手,我完全有可能在错误的地方执行了错误的搜索.因此,我的问题是:

Being both an amateur web developer and a rails newbie, it is entirely possible that I have been executing the wrong searches in the wrong places. Therefore my questions are:

  • 是否有一个很好的网站汇总了 Rails 教程,其中包含与限制出站 API 请求相关的材料?

  • Is there a nice website out there aggregating Rails tutorials that has material related to throttling outbound API requests?

是否有任何 ruby​​ gem 或其他库可以帮助我限制请求?

Are there any ruby gems or other libraries that would help me throttle the requests?

我对如何使用基于队列的工作程序(如 DelayedJob 或 Resque)编写节流系统来管理 API 调用有一些想法,但如果有需要,我宁愿花周末时间构建网站的其余部分已经有很好的预构建解决方案.

I have some ideas of how I might go about writing a throttling system using a queue-based worker like DelayedJob or Resque to manage the API calls, but I would rather spend my weekends building the rest of the site if there is a good pre-built solution out there already.

推荐答案

没有人谈论出站节流的原因是它通常是微不足道的,因为控制它.控制带宽可能有点困难,但控制请求数量?

The reason nobody talk about outbound throttling is that it's usually pretty trivial, since you control it. Controlling bandwidth can be a bit harder, but controlling number of request ?

ri Kernel#sleep

因此,如果允许每分钟 10 次 api 调用,则每次调用后只需 sleep(6)

So, if you're allowed 10 api calls per min you just sleep(6) after each call

这篇关于限制 Rails 应用程序生成的出站 API 调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆