接口请求限流

This commit is contained in:
xxqfamous 2023-06-08 14:00:06 +08:00
parent 9f40886c45
commit 18a4833759
1 changed files with 8 additions and 12 deletions

View File

@ -1165,19 +1165,15 @@ class ApplicationController < ActionController::Base
# 接口限流,请求量大有性能问题
def request_limit
begin
record_count = Rails.cache.read("request/#{controller_name}/#{Time.now.strftime('%Y%m%d%H%M')}/#{request.remote_ip}")
if record_count.present?
record_count = record_count + 1
else
record_count = 1
end
normal_status("请求太快,请稍后再试。") if record_count > 100
Rails.cache.write("request/#{controller_name}/#{Time.now.strftime('%Y%m%d%H%M')}/#{request.remote_ip}", record_count, expires_in: 1.minute)
rescue
record_count = Rails.cache.read("request/#{controller_name}/#{Time.now.strftime('%Y%m%d%H%M')}/#{request.remote_ip}")
if record_count.present?
record_count = record_count + 1
else
record_count = 1
end
tip_exception("请求太快,请稍后再试。") if record_count > 100
Rails.cache.write("request/#{controller_name}/#{Time.now.strftime('%Y%m%d%H%M')}/#{request.remote_ip}", record_count, expires_in: 1.minute)
end
end