I was wondering if anyone knew of a way that you could manage bandwidth within a rails application in some way that isn't dependent on the web server. For example each account has a bandwidth limit. In and out bound traffic subtracts from the monthly allowance?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
One option would be to add an after_filter in application.rb (so that it applies to all actions) and do the following:
def store_bandwidth_usage
response_size = response.body.size
# Assuming the User model has a bandwidth_usage attribute
@current_user.increment!(:bandwidth_usage, response_size)
end
Of course then you would need a before_filter which checked that a user had not gone over their allocated bandwidth otherwise they should be denied access.
Keep in mind that this will only be counted for requests that hit the rails server, any requests that are filled by an front-end server (e.g. images) will not be included.