Rails 7.1: guide with custom middleware affecting request timing and response
I'm maintaining legacy code that Could someone explain I've encountered a strange issue with I'm working on a personal project and I'm experiencing an scenario with a custom Rack middleware I've implemented in my Ruby on Rails 7.1 application..... The middleware is intended to log request timings, but it seems to be affecting the response times for certain endpoints significantly. I've set up the middleware like this: ```ruby class RequestTimingMiddleware def initialize(app) @app = app end def call(env) start_time = Time.now response = @app.call(env) total_time = Time.now - start_time Rails.logger.info "Request took #{total_time.round(2)} seconds" response end end ``` I added it to my application in the `application.rb` file: ```ruby config.middleware.use RequestTimingMiddleware ``` However, I noticed that for endpoints that involve complex queries or heavy processing, the response time is significantly longer than expected. For instance, a simple `GET /users` request that used to complete in under 100ms now sometimes takes over 300ms. I've tried disabling the middleware temporarily, and the response times return to normal. I've also checked if the middleware is acting synchronously or asynchronously and confirmed it runs synchronously as intended. My initial guess is that there's some interaction with ActiveRecord queries that I'm not accounting for. Is there a best practice for implementing middleware in Rails that could prevent this performance hit? Could this be caused by the way Rack handles the response? Any insights or suggestions would be greatly appreciated! For context: I'm using Ruby on Linux. What am I doing wrong? My development environment is macOS. I'd be grateful for any help. Cheers for any assistance! My team is using Ruby for this REST API. Thanks in advance!