首页 > 解决方案 > 为什么 Rails 在开发模式下响应需要一分钟?

问题描述

无法理解为什么我的 Rails 应用程序变得响应超慢 - 1 分钟内!

我看到 Rails 的总时间应该是:Views 0.2ms + ActiveRecord 219.5ms + Solr 379.7ms = 599.4ms

但是它需要62615ms,剩下的62015.6ms在哪里花费?

Started POST "/applications/135" for ::1 at 2019-08-05 17:59:04 +0300
Processing by ApplicationController#create as JS

...

Completed 200 OK in 62615ms (Views: 0.2ms | ActiveRecord: 219.5ms | Solr: 379.7ms)

config/environments/development.rb

# frozen_string_literal: true

require 'sidekiq/testing/inline'

Rails.application.configure do
  # Settings specified here will take precedence over those in config/application.rb.
  # Verifies that versions and hashed value of the package contents in the project's package.json
  config.webpacker.check_yarn_integrity = false

  # In the development environment your application's code is reloaded on
  # every request. This slows down response time but is perfect for development
  # since you don't have to restart the web server when you make code changes.
  config.cache_classes = false

  # Do not eager load code on boot.
  config.eager_load = false

  # Show full error reports and disable caching.
  config.consider_all_requests_local       = true
  config.action_controller.perform_caching = false

  # Print deprecation notices to the Rails logger.
  config.active_support.deprecation = :log

  # Raise an error on page load if there are pending migrations.
  config.active_record.migration_error = :page_load

  # Debug mode disables concatenation and preprocessing of assets.
  # This option may cause significant delays in view rendering with a large
  # number of complex assets.
  config.assets.debug = false

  # Asset digests allow you to set far-future HTTP expiration dates on all assets,
  # yet still be able to expire them through the digest params.
  config.assets.digest = true

  # Adds additional error checking when serving assets at runtime.
  # Checks for improperly declared sprockets dependencies.
  # Raises helpful error messages.
  config.assets.raise_runtime_errors = true

  # Raises error for missing translations
  # config.action_view.raise_on_missing_translations = true

  # SMTP configuration
  config.action_mailer.default_url_options = {
    host: ENV['HOST']
  }
  config.action_mailer.delivery_method = :letter_opener
  config.action_mailer.perform_deliveries = true
  # Care if the mailer can't send.
  config.action_mailer.raise_delivery_errors = true
end

标签: ruby-on-railsrubyperformancerest-client

解决方案


找到了减速的原因。我们团队的某个人在控制器中添加了延迟服务。

在注释并单独运行后#require 'sidekiq/testing/inline',它解决了问题,现在服务正在异步运行。config/environments/development.rbsidekiq -C config/sidekiq.yml

但是问题出在服务内部,该调用RestClient.post(endpoint, payload.to_json, 'api-key': api_key)被包装到begin rescue end捕获错误的块中(由 RestClient 超时在一分钟内引发)。

Completed 401 Unauthorized in 62562ms (ActiveRecord: 191.5ms)

RestClient::Exceptions::ReadTimeout (Timed out reading data from server):

推荐阅读