问题描述
我使用自定义域名在Heroku上托管了一个Rails应用程序。该应用程序显示每个页面顶部的单个用户的最新推文。为了避免碰到Twitter速率限制(150个请求p / h),我让应用程序将搜索结果缓存在memcache(Dalli)中,并且过期5分钟。这很好地工作,确保应用程序每小时只有12个请求。如果它被高度贩卖,这个解决方案可能会有问题,但因为它是我认为它很好。
尽管事实上我远低于速率限制,但我的网站定期停机并查看我的服务器日志,这是因为我已经超过了Twitter的速率限制。
这与Heroku有关吗?还有什么可能导致它?这与共享IP地址有关吗?
日志:
2012-02-20T22:09:52 +00:00 app [web.1]:布局/应用程序中的渲染页面/ home.html.erb(0.8ms)
2012-02-20T22:09:52 + 00:00 app [web.1] :呈现布局/_header.html.erb(336.2ms)
2012-02-20T22:09:52 + 00:00 app [web.1]:在339ms内完成500次内部服务器错误
2012- 02-20T22:09:52 + 00:00 app [web.1]:ActionView :: Template :: Error(超出速率限制,客户端每小时不能超过150个请求):
2012-02 -20T22:09:52 + 00:00 app [web.1]:5:< p class =latest-tweet>
2012-02-20T22:09:52 + 00:00 app [web.1]:
2012-02-20T22:09:52 + 00:00 app [web.1]:7: <%if!latest_tweet%>
2012-02-20T22:09:52 + 00:00 app [web.1]:8:<%latest_tweet = Twitter.user_timeline(sometwitterusername)。first.text%>
2012-02-20T22:09:52 + 00:00 app [web.1]:6:<%latest_tweet = Rails.cache.readlatest_tweet%>
2012-02-20T22:09:52 + 00:00 app [web.1]:9:<%Rails.cache.write(latest_tweet,latest_tweet,:expires_in => 5.minutes) %GT;
2012-02-20T22:09:52 + 00:00 app [web.1]:10:<%end%>
2012-02-20T22:09:52 + 00:00 app [web.1]:11:<%= latest_tweet%>
2012-02-20T22:09:52 + 00:00 app [web.1]:app / views / layouts / _header.html.erb:8:in _app_views_layouts__header_html_erb___4472938277532005844_37037700'
2012-02- 20T22:09:52 + 00:00 app [web.1]:app / views / layouts / application.html.erb:14:in _app_views_layouts_application_html_erb__4101970984987800094_41561940'
非常简单。如果您与多个其他开发人员共享IP,您也将分享费率限制。
我建议使用身份验证将其提高到每小时350个请求,基于IP的限制。无论您与IP分享的开发者如何,这只会给您个请求。
$ b
I have a rails application hosted on Heroku using a custom Domain. The application displays the latest Tweet by a single user at the top of each page. To avoid hitting Twitter rate limits (150 requests p/h) I have the application cache the search result in memcache (Dalli) with a 5 minute expiration. This works nicely, ensuring the application only ever makes 12 requests per hour. If it was highly trafficked this solution might be problematic, but as it is I think its fine.
Despite the fact I am well below the rate limits, my site periodically goes down and looking at my server logs it is because I have exceeded Twitter's rate limits.
Is this something to do with Heroku? What else could be causing it? Is it something to do with shared IP addresses?
Logs:
2012-02-20T22:09:52+00:00 app[web.1]: Rendered pages/home.html.erb within layouts/application (0.8ms)
2012-02-20T22:09:52+00:00 app[web.1]: Rendered layouts/_header.html.erb (336.2ms)
2012-02-20T22:09:52+00:00 app[web.1]: Completed 500 Internal Server Error in 339ms
2012-02-20T22:09:52+00:00 app[web.1]: ActionView::Template::Error (Rate limit exceeded. Clients may not make more than 150 requests per hour.):
2012-02-20T22:09:52+00:00 app[web.1]: 5: <p class="latest-tweet">
2012-02-20T22:09:52+00:00 app[web.1]:
2012-02-20T22:09:52+00:00 app[web.1]: 7: <% if !latest_tweet%>
2012-02-20T22:09:52+00:00 app[web.1]: 8: <% latest_tweet = Twitter.user_timeline("sometwitterusername").first.text %>
2012-02-20T22:09:52+00:00 app[web.1]: 6: <% latest_tweet = Rails.cache.read "latest_tweet" %>
2012-02-20T22:09:52+00:00 app[web.1]: 9: <% Rails.cache.write("latest_tweet", latest_tweet, :expires_in => 5.minutes) %>
2012-02-20T22:09:52+00:00 app[web.1]: 10: <% end %>
2012-02-20T22:09:52+00:00 app[web.1]: 11: <%= latest_tweet %>
2012-02-20T22:09:52+00:00 app[web.1]: app/views/layouts/_header.html.erb:8:in `_app_views_layouts__header_html_erb___4472938277532005844_37037700'
2012-02-20T22:09:52+00:00 app[web.1]: app/views/layouts/application.html.erb:14:in `_app_views_layouts_application_html_erb__4101970984987800094_41561940'
Pretty simple. If you share an IP with multiple other developers, you'll share the rate limits as well.
I recommend using authentication to raise this to 350 requests per hour, removing the IP-based limits. This'll simply give you 350 requests, regardless of the developers you share the IP with.
https://dev.twitter.com/docs/rate-limiting
这篇关于Heroku上托管的网站的Twitter费率限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!