Browse Source

raw api limit

pull/348/head
xxq250 1 year ago
parent
commit
6ede037c62
2 changed files with 16 additions and 1 deletions
  1. +15
    -1
      app/controllers/application_controller.rb
  2. +1
    -0
      app/controllers/repositories_controller.rb

+ 15
- 1
app/controllers/application_controller.rb View File

@@ -1184,7 +1184,21 @@ class ApplicationController < ActionController::Base
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)
Rails.cache.write("request/#{controller_name}/#{Time.now.strftime('%Y%m%d%H%M')}/#{request.remote_ip}", record_count, expires_in: 10.minute)
end

def check_batch_requests
check_key = "request.remote_ip:#{request.remote_ip}"
result = Rails.cache.read(check_key)
if result.present?
if result.to_i > 100
tip_exception(401, '暂时无法请求,请稍后再试')
else
Rails.cache.write(check_key, result.to_i + 1)
end
else
Rails.cache.write(check_key, 1, expires_in: 1.hour)
end
end

end

+ 1
- 0
app/controllers/repositories_controller.rb View File

@@ -14,6 +14,7 @@ class RepositoriesController < ApplicationController
before_action :get_latest_commit, only: %i[entries sub_entries top_counts]
before_action :get_statistics, only: %i[top_counts]
before_action :require_referer, only: [:raw]
before_action :request_limit, only: [:raw]
def files
result = @project.educoder? ? nil : Gitea::Repository::Files::GetService.call(@owner, @project.identifier, @ref, params[:search], @owner.gitea_token)


Loading…
Cancel
Save