From f39bb01df710a0bcdc4631e16a192bdfbbd020de Mon Sep 17 00:00:00 2001 From: Alex Skrenchuk Date: Tue, 15 Aug 2023 12:02:06 -0700 Subject: [PATCH 1/2] reduce request limit for resource intensive api calls (#121) --- config/rack_attack.rb | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/config/rack_attack.rb b/config/rack_attack.rb index 60d2e3de..3eae6949 100644 --- a/config/rack_attack.rb +++ b/config/rack_attack.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true -puts "(API) >> Throttling enabled at #{LinkedData::OntologiesAPI.settings.req_per_second_per_ip} req/sec" +limit_req_ip = LinkedData::OntologiesAPI.settings.req_per_second_per_ip +limit_req_ip_heavy = limit_req_ip / 5 +puts "(API) >> Throttling enabled at #{limit_req_ip} req/sec" require 'rack/attack' require 'redis-activesupport' @@ -28,11 +30,12 @@ request.env['REMOTE_USER']&.admin? end -Rack::Attack.throttle('requests by ip', - limit: LinkedData::OntologiesAPI.settings.req_per_second_per_ip, - period: 1.second -) do |request| - request.ip +Rack::Attack.throttle('req/ip/heavy', limit: limit_req_ip_heavy, period: 1.second) do |req| + req.ip if req.path.include?('/recommender') || req.path.include?('/annotator') +end + +Rack::Attack.throttle('req/ip', limit: limit_req_ip, period: 1.second) do |req| + req.ip end Rack::Attack.throttled_responder = lambda do |request| From ac4223893f2a9a3cd50acb8ae49a19dd7b7134dc Mon Sep 17 00:00:00 2001 From: Alex Skrenchuk Date: Wed, 30 Aug 2023 16:41:23 -0700 Subject: [PATCH 2/2] Announce deployments in NewRelic (#124) * Record deployments to NewRelic https://docs.newrelic.com/docs/apm/agents/ruby-agent/features/record-deployments-ruby-agent/ * add newrelic to deployment group github actions deployment doesn't install default group so capistrano fails to find newrelic recepies unless we add it to the deployment group * add rubocop --- Capfile | 2 +- Gemfile | 10 +++++++--- Gemfile.lock | 28 +++++++++++++++++++++++++++- config/deploy.rb | 1 + 4 files changed, 36 insertions(+), 5 deletions(-) diff --git a/Capfile b/Capfile index f86314f3..7ecc995c 100644 --- a/Capfile +++ b/Capfile @@ -22,6 +22,6 @@ require 'capistrano/bundler' # require 'capistrano/rails/assets' # require 'capistrano/rails/migrations' require 'capistrano/locally' - +require 'new_relic/recipes' # announce deployments in NewRelic # Loads custom tasks from `lib/capistrano/tasks' if you have any defined. Dir.glob('lib/capistrano/tasks/*.cap').each { |r| import r } diff --git a/Gemfile b/Gemfile index 7b76c8b3..2236aee2 100644 --- a/Gemfile +++ b/Gemfile @@ -31,7 +31,7 @@ gem 'redis-store', '1.9.1' # remove after https://github.com/redis-store/redis-s # Monitoring gem 'cube-ruby', require: 'cube' -gem 'newrelic_rpm' +gem 'newrelic_rpm', group: [:default, :deployment] # HTTP server gem 'unicorn' @@ -50,6 +50,12 @@ gem 'ontologies_linked_data', github: 'ncbo/ontologies_linked_data', branch: 'de gem 'sparql-client', github: 'ncbo/sparql-client', branch: 'develop' group :development do + # bcrypt_pbkdf and ed35519 is required for capistrano deployments when using ed25519 keys; see https://github.com/miloserdow/capistrano-deploy/issues/42 + gem 'shotgun', github: 'palexander/shotgun', branch: 'ncbo' + gem 'rubocop' +end + +group :deployment do # bcrypt_pbkdf and ed35519 is required for capistrano deployments when using ed25519 keys; see https://github.com/miloserdow/capistrano-deploy/issues/42 gem 'bcrypt_pbkdf', '>= 1.0', '< 2.0', require: false gem 'capistrano', '~> 3', require: false @@ -57,8 +63,6 @@ group :development do gem 'capistrano-locally', require: false gem 'capistrano-rbenv', require: false gem 'ed25519', '>= 1.2', '< 2.0', require: false - gem 'pry' - gem 'shotgun', github: 'palexander/shotgun', branch: 'ncbo' end group :profiling do diff --git a/Gemfile.lock b/Gemfile.lock index 822d5816..5affb200 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -107,7 +107,9 @@ GEM public_suffix (>= 2.0.2, < 6.0) airbrussh (1.4.2) sshkit (>= 1.6.1, != 1.7.0) + ast (2.4.2) backports (3.24.1) + base64 (0.1.1) bcrypt (3.1.19) bcrypt_pbkdf (1.1.0) bigdecimal (1.4.2) @@ -202,6 +204,7 @@ GEM json_pure (2.6.3) jwt (2.7.1) kgio (2.11.4) + language_server-protocol (3.17.0.3) libxml-ruby (4.1.1) logger (1.5.3) macaddr (1.7.2) @@ -241,13 +244,18 @@ GEM omni_logger (0.1.4) logger os (1.1.4) + parallel (1.23.0) parseconfig (1.1.2) + parser (3.2.2.3) + ast (~> 2.4.1) + racc pony (1.13.1) mail (>= 2.0) pry (0.14.2) coderay (~> 1.1) method_source (~> 1.0) public_suffix (5.0.3) + racc (1.7.1) rack (1.6.13) rack-accept (0.4.5) rack (>= 0.4) @@ -264,6 +272,7 @@ GEM rack-test (2.1.0) rack (>= 1.3) rack-timeout (0.6.3) + rainbow (3.1.1) raindrops (0.20.1) rake (10.5.0) rdf (1.0.8) @@ -278,6 +287,7 @@ GEM redis-store (>= 1.6, < 2) redis-store (1.9.1) redis (>= 4, < 5) + regexp_parser (2.8.1) rest-client (2.1.0) http-accept (>= 1.7.0, < 2.0) http-cookie (>= 1.0.2, < 2.0) @@ -287,6 +297,21 @@ GEM rsolr (2.5.0) builder (>= 2.1.2) faraday (>= 0.9, < 3, != 2.0.0) + rubocop (1.56.2) + base64 (~> 0.1.1) + json (~> 2.3) + language_server-protocol (>= 3.17.0) + parallel (~> 1.10) + parser (>= 3.2.2.3) + rainbow (>= 2.2.2, < 4.0) + regexp_parser (>= 1.8, < 3.0) + rexml (>= 3.2.5, < 4.0) + rubocop-ast (>= 1.28.1, < 2.0) + ruby-progressbar (~> 1.7) + unicode-display_width (>= 2.4.0, < 3.0) + rubocop-ast (1.29.0) + parser (>= 3.2.1.0) + ruby-progressbar (1.13.0) ruby-xxHash (0.4.0.2) ruby2_keywords (0.0.5) rubyzip (2.3.2) @@ -331,6 +356,7 @@ GEM unf (0.1.4) unf_ext unf_ext (0.0.8.2) + unicode-display_width (2.4.2) unicorn (6.1.0) kgio (~> 2.6) raindrops (~> 0.7) @@ -371,7 +397,6 @@ DEPENDENCIES oj (~> 3.0) ontologies_linked_data! parseconfig - pry rack rack-accept (~> 0.4) rack-attack (~> 6.6.1) @@ -387,6 +412,7 @@ DEPENDENCIES redis-activesupport redis-rack-cache (~> 2.0) redis-store (= 1.9.1) + rubocop shotgun! simplecov simplecov-cobertura diff --git a/config/deploy.rb b/config/deploy.rb index 23a982cd..6b051216 100644 --- a/config/deploy.rb +++ b/config/deploy.rb @@ -108,6 +108,7 @@ after :publishing, :get_config after :get_config, :restart # after :deploy, :smoke_test + after :restart, "newrelic:notice_deployment" after :restart, :clear_cache do on roles(:web), in: :groups, limit: 3, wait: 10 do