接口请求限流
This commit is contained in:
parent
6f33016a78
commit
f1ab52da5c
|
@ -1163,4 +1163,21 @@ class ApplicationController < ActionController::Base
|
||||||
@atme_receivers = User.where(login: params[:receivers_login])
|
@atme_receivers = User.where(login: params[:receivers_login])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# 接口限流,请求量大有性能问题
|
||||||
|
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
|
||||||
|
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)
|
||||||
|
rescue
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -9,6 +9,7 @@ class ProjectsController < ApplicationController
|
||||||
before_action :load_repository, except: %i[index group_type_list migrate create recommend banner_recommend]
|
before_action :load_repository, except: %i[index group_type_list migrate create recommend banner_recommend]
|
||||||
before_action :authorizate_user_can_edit_project!, only: %i[update]
|
before_action :authorizate_user_can_edit_project!, only: %i[update]
|
||||||
before_action :project_public?, only: %i[fork_users praise_users watch_users]
|
before_action :project_public?, only: %i[fork_users praise_users watch_users]
|
||||||
|
before_action :request_limit, only: %i[index]
|
||||||
|
|
||||||
def menu_list
|
def menu_list
|
||||||
menu = []
|
menu = []
|
||||||
|
@ -286,6 +287,7 @@ class ProjectsController < ApplicationController
|
||||||
|
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def project_params
|
def project_params
|
||||||
params.permit(:user_id, :name, :description, :repository_name, :website, :lesson_url, :default_branch, :identifier,
|
params.permit(:user_id, :name, :description, :repository_name, :website, :lesson_url, :default_branch, :identifier,
|
||||||
:project_category_id, :project_language_id, :license_id, :ignore_id, :private,
|
:project_category_id, :project_language_id, :license_id, :ignore_id, :private,
|
||||||
|
|
Loading…
Reference in New Issue