使用rails进行带宽管理?

bwi*_*zzy 5 ruby-on-rails bandwidth

我想知道是否有人知道你可以通过某种方式管理rails应用程序中的带宽而不依赖于Web服务器.例如,每个帐户都有带宽限制.进出的交通量从月度补贴中扣除?

小智 4

一种选择是在 application.rb 中添加 after_filter (以便它适用于所有操作)并执行以下操作:

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
Run Code Online (Sandbox Code Playgroud)

当然,那么您需要一个 before_filter 来检查用户是否没有超过分配的带宽,否则他们应该被拒绝访问。

请记住,这只会计算到达 Rails 服务器的请求,任何由前端服务器填充的请求(例如图像)将不包括在内。