From ae6400a06d7baa6fdcbf164a56999d3d0c7b6a59 Mon Sep 17 00:00:00 2001 From: xxq250 Date: Fri, 28 Feb 2025 22:23:43 +0800 Subject: [PATCH] raw api limit request.path --- app/controllers/application_controller.rb | 13 +++++++++++++ app/controllers/repositories_controller.rb | 1 + 2 files changed, 14 insertions(+) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 4bffaeaf1..586644179 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1202,4 +1202,17 @@ class ApplicationController < ActionController::Base end end + def request_raw_limit + record_count = Rails.cache.read("request/#{request.path}/#{Time.now.strftime('%Y%m%d%H')}}") + if record_count.present? + record_count = record_count + 1 + else + record_count = 1 + end + Rails.logger.info("请求太快,请稍后再试。#{request.path}") if record_count > 1000 + tip_exception("请求太快,请稍后再试。") if record_count > 1000 + + Rails.cache.write("request/#{request.path}/#{Time.now.strftime('%Y%m%d%H')}}", record_count, expires_in: 1.hour) + end + end diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index 173d15752..368532ad5 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -15,6 +15,7 @@ class RepositoriesController < ApplicationController before_action :get_statistics, only: %i[top_counts] before_action :require_referer, only: [:raw] before_action :request_limit, only: [:raw] + before_action :request_raw_limit, only: [:raw] def files result = @project.educoder? ? nil : Gitea::Repository::Files::GetService.call(@owner, @project.identifier, @ref, params[:search], @owner.gitea_token)