问题
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.
来源:https://stackoverflow.com/questions/1226856/bandwidth-management-with-rails