Share
Embed
Debugging Adventures in Rack-land (Fog City Ruby - November 2018)
I ran into a weird bug the other day. Editing an attribute in a web form would kick off an XHR request to the server and update a row in the database. But when the page was refreshed, the attribute on the page had not changed from its original value. Even weirder: refreshing the page a second time caused the attribute to show up correctly—and reflect what was in the database.
I’m not going to spoil the story here, but the issue led me into the internals of Rack middleware. Join me as we explore the details of this strange bug. Together, we’ll learn something about the wonderful world of Rack.
Pan Thomakos
November 13, 2018
Transcript
197 slides
-
@panthomakos From: $HOME/web/app/controllers/settings_controller.rb @ line 313 SettingsController#athlete: 312: def athlete
=> 313: binding.pry 314: Permissions.decorate(current_athlete, self) 315: end 2.3.3 (...):0 > -
@panthomakos From: $HOME/web/app/controllers/settings_controller.rb @ line 313 SettingsController#athlete: 312: def athlete
=> 313: binding.pry 314: Permissions.decorate(current_athlete, self) 315: end 2.3.3 (...):0 > current_athlete.measurement_preference => :meters 2.3.3 (...):0 > Athlete.find(current_athlete.id).measurement_preference => :feet -
@panthomakos From: $HOME/web/app/controllers/settings_controller.rb @ line 313 SettingsController#athlete: 312: def athlete
=> 313: binding.pry 314: Permissions.decorate(current_athlete, self) 315: end 2.3.3 (...):0 > current_athlete.measurement_preference => :meters 2.3.3 (...):0 > Athlete.find(current_athlete.id).measurement_preference => :feet 2.3.3 (...):0 > Athlete.fetch(current_athlete.id).measurement_preference => :meters -
@panthomakos From: $HOME/web/app/controllers/settings_controller.rb @ line 313 SettingsController#athlete: 312: def athlete
=> 313: binding.pry 314: Permissions.decorate(current_athlete, self) 315: end 2.3.3 (...):0 > Athlete.fetch(current_athlete.id).measurement_preference => :meters 2.3.3 (...):0 > Strava::EntityMap.clear! => nil 2.3.3 (...):0 > Athlete.fetch(current_athlete.id).measurement_preference => :feet -
@panthomakos [ 200, { "Content-Type" => "application/json" }, [ {
"status": "success" }.to_json ] ] -
@panthomakos class Body def each yield “<html>” yield “<head> ...
</head>” yield “<body> ... </body>” yield “</html>” end end -
@panthomakos class MyTimer def initialize(app) @app = app end def
call(env) start = ::Timer.cputime response = @app.call(env) finish = ::Timer.cputime puts “Response Time: #{finish - start}ms” response end end -
@panthomakos class MyTimer def initialize(app) @app = app end def
call(env) start = ::Timer.cputime response = @app.call(env) finish = ::Timer.cputime puts “Response Time: #{finish - start}ms” response end end -
@panthomakos class SlowBody def each yield “<head> ... </head>” sleep(2)
yield “<body> ... </body>” end end -
@panthomakos # lib/rack/handler/webrick.rb def service(req, res) ... status, headers, body
= @app.call(env) begin ... body.each { |part| res.body << part } ensure body.close if body.respond_to?(:close) end end -
@panthomakos class MyTimer def initialize(app) @app = app end def
call(env) start = ::Timer.cputime response = @app.call(env) finish = ::Timer.cputime puts "Response Time: #{finish - start}ms" response end end -
@panthomakos class BoxyProxy def initialize(body, &close) @body = body @close
= close end def each @body.each end def close @body.close if @body.respond_to?(:close) @close.call end end -
@panthomakos class MyTimer def call(env) start = ::Timer.cputime status, headers,
body = @app.call(env) proxy = BodyProxy.new(body) do finish = ::Timer.cputime puts "Response Time: #{finish - start}ms" end [status, headers, proxy] end end -
@panthomakos Rack Handler Ruby Application Rack Middleware Rack Middleware Rack
Middleware Body Body Body Body Req Res -
@panthomakos Rack Handler Ruby Application Rack Middleware Rack Middleware Rack
Middleware Req Res Body Body Body Body #close #close #close #close -
@panthomakos From: $HOME/web/app/controllers/settings_controller.rb @ line 313 SettingsController#athlete: 312: def athlete
=> 313: binding.pry 314: Permissions.decorate(current_athlete, self) 315: end 2.3.3 (...):0 > Athlete.fetch(current_athlete.id).measurement_preference => :meters 2.3.3 (...):0 > Strava::EntityMap.clear! => nil 2.3.3 (...):0 > Athlete.fetch(current_athlete.id).measurement_preference => :feet -
@panthomakos # activerecord/lib/active_record/identity_map.rb class Middleware def initialize(app) @app = app
end def call(env) status, headers, body = @app.call(env) [status, headers, BodyProxy.new(body)] end end -
@panthomakos # activerecord/lib/active_record/identity_map.rb class Middleware class BodyProxy def initialize(target) @target
= target end def each(&block) @target.each(&block) end def close @target.close if @target.respond_to?(:close) ensure IdentityMap.clear end end end -
@panthomakos # lib/strava/entity_map_middleware.rb module Strava class EntityMapMiddleware def initialize(app) @app
= app end def call(env) status, headers, body = @app.call(env) [status, headers, BodyProxy.new(body)] end end end -
@panthomakos # lib/strava/entity_map_middleware.rb module Strava class EntityMapMiddleware class BodyProxy def
initialize(target) @target = target end def each(&block) @target.each(&block) end def close @target.close if @target.respond_to?(:close) ensure ::Strava::EntityMap.clear! end end end end -
@panthomakos def close raise 'This is supposed to happen!' @target.close
if @target.respond_to?(:close) ensure ::Strava::EntityMap.clear! end -
@panthomakos # lib/rack/handler/webrick.rb def service(req, res) ... status, headers, body
= @app.call(env) begin ... body.each { |part| res.body << part } ensure body.close if body.respond_to?(:close) end end -
@panthomakos # lib/rack/handler/webrick.rb def service(req, res) ... status, headers, body
= @app.call(env) begin ... body.each { |part| res.body << part } ensure binding.pry body.close if body.respond_to?(:close) end end -
@panthomakos From: .../lib/rack/handler/webrick.rb @ line 75 Rack::Handler::WEBrick#service: 70: } 71:
body.each { |part| 72: res.body << part 73: } 74: ensure => 75: binding.pry 76: body.close if body.respond_to? :close 77: end 78: end 79: end 80: end 2.3.3 (...):0 > -
@panthomakos 2.3.3 (...):0 > body => #<Rack::BodyProxy:0x0055b6af04bad0 @block=#<Proc:0x0055b6af04baa8@.../rack/lock.rb:16>, @body= #<ActionDispatch::BodyProxy:0x0055b6af04c318
@block= #<Proc:0x0055b6af04c2c8@.../action_dispatch/middleware/reloader.rb:66>, @body= #<ActiveRecord::ConnectionAdapters::ConnectionManagement::Proxy:0x00... @body= #<ActiveRecord::QueryCache::BodyProxy:0x00... @connection_id=47121566960060, @original_cache_value=false, @target=["{\"success\":true}"]>, @testing=false>, @closed=false>, @closed=false> -
@panthomakos 2.3.3 (...):0 > body => #<Rack::BodyProxy:0x0055b6af04bad0 @block=#<Proc:0x0055b6af04baa8@.../rack/lock.rb:16>, @body= #<ActionDispatch::BodyProxy:0x0055b6af04c318
@block= #<Proc:0x0055b6af04c2c8@.../action_dispatch/middleware/reloader.rb:66>, @body= #<ActiveRecord::ConnectionAdapters::ConnectionManagement::Proxy:0x00... @body= #<ActiveRecord::QueryCache::BodyProxy:0x00... @connection_id=47121566960060, @original_cache_value=false, @target=["{\"success\":true}"]>, @testing=false>, @closed=false>, @closed=false> -
@panthomakos 2.3.3 (...):0 > body => #<Rack::BodyProxy:0x0055b6af04bad0 @block=#<Proc:0x0055b6af04baa8@.../rack/lock.rb:16>, @body= #<ActionDispatch::BodyProxy:0x0055b6af04c318
@block= #<Proc:0x0055b6af04c2c8@.../action_dispatch/middleware/reloader.rb:66>, @body= #<ActiveRecord::ConnectionAdapters::ConnectionManagement::Proxy:0x00... @body= #<ActiveRecord::QueryCache::BodyProxy:0x00... @connection_id=47121566960060, @original_cache_value=false, @target=["{\"success\":true}"]>, @testing=false>, @closed=false>, @closed=false> -
@panthomakos From: .../lib/rack/handler/webrick.rb @ line 75 Rack::Handler::WEBrick#service: 70: } 71:
body.each { |part| 72: res.body << part 73: } 74: ensure => 75: binding.pry 76: body.close if body.respond_to? :close 77: end 78: end 79: end 80: end 2.3.3 (...):0 > -
@panthomakos 2.3.3 (#<Rack::Handler::WEBrick:0x0055b6ae171168>):0 > body => #<Rack::BodyProxy:0x0055b6af109b70 @block=#<Proc:[email protected]/rack/lock.rb:16>, @body= #<ActionDispatch::BodyProxy:0x0055b6af10a368
@block= #<Proc:[email protected]/action_dispatch/middleware/reloader.rb:66>, @body= #<ActiveRecord::ConnectionAdapters::ConnectionManagement::Proxy:0x00... @body= #<ActiveRecord::QueryCache::BodyProxy:0x00... @connection_id=47121546748840, @original_cache_value=false, @target= ["<!DOCTYPE html>\n<html class='logged-in clean s-minifeed old-login' dir='ltr' lang='en-US'..."] -
@panthomakos 2.3.3 (#<Rack::Handler::WEBrick:0x0055b6ae171168>):0 > body => #<Rack::BodyProxy:0x0055b6af109b70 @block=#<Proc:[email protected]/rack/lock.rb:16>, @body= #<ActionDispatch::BodyProxy:0x0055b6af10a368
@block= #<Proc:[email protected]/action_dispatch/middleware/reloader.rb:66>, @body= #<ActiveRecord::ConnectionAdapters::ConnectionManagement::Proxy:0x00... @body= #<ActiveRecord::QueryCache::BodyProxy:0x00... @connection_id=47121546748840, @original_cache_value=false, @target= ["<!DOCTYPE html>\n<html class='logged-in clean s-minifeed old-login' dir='ltr' lang='en-US'..."] -
@panthomakos 2.3.3 (#<Rack::Handler::WEBrick:0x0055b6ae171168>):0 > body => #<Rack::BodyProxy:0x0055b6af109b70 @block=#<Proc:[email protected]/rack/lock.rb:16>, @body= #<ActionDispatch::BodyProxy:0x0055b6af10a368
@block= #<Proc:[email protected]/action_dispatch/middleware/reloader.rb:66>, @body= #<ActiveRecord::ConnectionAdapters::ConnectionManagement::Proxy:0x00... @body= #<ActiveRecord::QueryCache::BodyProxy:0x00... @connection_id=47121546748840, @original_cache_value=false, @target= ["<!DOCTYPE html>\n<html class='logged-in clean s-minifeed old-login' dir='ltr' lang='en-US'..."] -
@panthomakos From: .../lib/rack/handler/webrick.rb @ line 75 Rack::Handler::WEBrick#service: 70: } 71:
body.each { |part| 72: res.body << part 73: } 74: ensure => 75: binding.pry 76: body.close if body.respond_to? :close 77: end 78: end 79: end 80: end 2.3.3 (...):0 > -
@panthomakos 2.3.3 (#<Rack::Handler::WEBrick:0x0055b6ae6ca100>):0 > body => #<Rack::BodyProxy:0x0055b6ae31a1e0 @block=#<Proc:[email protected]/rack/lock.rb:16>, @body= #<ActionDispatch::BodyProxy:0x0055b6ae31a898
@block= #<Proc:[email protected]/action_dispatch/middleware/reloader.rb:66>, @body= #<ActiveRecord::ConnectionAdapters::ConnectionManagement::Proxy:0x00... @body= #<ActiveRecord::QueryCache::BodyProxy:0x0055b6ae31a960 @connection_id=47121546748840, @original_cache_value=false, @target= #<Strava::EntityMapMiddleware::BodyProxy:0x00... @target=[]>>, @testing=false>, @closed=false>, @closed=false> -
@panthomakos 2.3.3 (#<Rack::Handler::WEBrick:0x0055b6ae6ca100>):0 > body => #<Rack::BodyProxy:0x0055b6ae31a1e0 @block=#<Proc:[email protected]/rack/lock.rb:16>, @body= #<ActionDispatch::BodyProxy:0x0055b6ae31a898
@block= #<Proc:[email protected]/action_dispatch/middleware/reloader.rb:66>, @body= #<ActiveRecord::ConnectionAdapters::ConnectionManagement::Proxy:0x00... @body= #<ActiveRecord::QueryCache::BodyProxy:0x0055b6ae31a960 @connection_id=47121546748840, @original_cache_value=false, @target= #<Strava::EntityMapMiddleware::BodyProxy:0x00... @target=[]>>, @testing=false>, @closed=false>, @closed=false> -
@panthomakos 2.3.3 (...):0 > env['REQUEST_PATH'] => "/assets/application.css" 2.3.3 (...):0 >
env['REQUEST_PATH'] => "/assets/strava/i18n/locales/en-US.js" -
@panthomakos $ bundle exec rake middleware use Rack::Cors use Airbrake::UserInformer
use ActionDispatch::Static use Rack::Lock use Rack::Runtime use Rack::MethodOverride use ActionDispatch::RequestId use Rails::Rack::Logger use ActionDispatch::ShowExceptions use ActionDispatch::DebugExceptions use BetterErrors::Middleware use Airbrake::Rails::Middleware use ActionDispatch::RemoteIp use ActionDispatch::Reloader use ActionDispatch::Callbacks use ActiveRecord::ConnectionAdapters::ConnectionManagement use ActiveRecord::QueryCache use ActionDispatch::Cookies use ActionDispatch::Session::CookieStore use ActionDispatch::Flash use ActionDispatch::ParamsParser use ActionDispatch::Head use Rack::ConditionalGet use Rack::ETag use ActionDispatch::BestStandardsSupport use Strava::EntityMapMiddleware run Strava::Application.routes -
@panthomakos $ bundle exec rake middleware use Rack::Cors use Airbrake::UserInformer
use ActionDispatch::Static use Rack::Lock use Rack::Runtime use Rack::MethodOverride use ActionDispatch::RequestId use Rails::Rack::Logger use ActionDispatch::ShowExceptions use ActionDispatch::DebugExceptions use BetterErrors::Middleware use Airbrake::Rails::Middleware use ActionDispatch::RemoteIp use ActionDispatch::Reloader use ActionDispatch::Callbacks use ActiveRecord::ConnectionAdapters::ConnectionManagement use ActiveRecord::QueryCache use ActionDispatch::Cookies use ActionDispatch::Session::CookieStore use ActionDispatch::Flash use ActionDispatch::ParamsParser use ActionDispatch::Head use Rack::ConditionalGet use Rack::ETag use ActionDispatch::BestStandardsSupport use Strava::EntityMapMiddleware run Strava::Application.routes -
@panthomakos $ bundle exec rake middleware use Rack::Cors use Airbrake::UserInformer
use ActionDispatch::Static use Rack::Lock use Rack::Runtime use Rack::MethodOverride use ActionDispatch::RequestId use Rails::Rack::Logger use ActionDispatch::ShowExceptions use ActionDispatch::DebugExceptions use BetterErrors::Middleware use Airbrake::Rails::Middleware use ActionDispatch::RemoteIp use ActionDispatch::Reloader use ActionDispatch::Callbacks use ActiveRecord::ConnectionAdapters::ConnectionManagement use ActiveRecord::QueryCache use ActionDispatch::Cookies use ActionDispatch::Session::CookieStore use ActionDispatch::Flash use ActionDispatch::ParamsParser use ActionDispatch::Head use Rack::ConditionalGet use Rack::ETag use ActionDispatch::BestStandardsSupport use Strava::EntityMapMiddleware run Strava::Application.routes -
@panthomakos $ bundle exec rake middleware use Rack::Cors use Airbrake::UserInformer
use ActionDispatch::Static use Rack::Lock use Rack::Runtime use Rack::MethodOverride use ActionDispatch::RequestId use Rails::Rack::Logger use ActionDispatch::ShowExceptions use ActionDispatch::DebugExceptions use BetterErrors::Middleware use Airbrake::Rails::Middleware use ActionDispatch::RemoteIp use ActionDispatch::Reloader use ActionDispatch::Callbacks use ActiveRecord::ConnectionAdapters::ConnectionManagement use ActiveRecord::QueryCache use ActionDispatch::Cookies use ActionDispatch::Session::CookieStore use ActionDispatch::Flash use ActionDispatch::ParamsParser use ActionDispatch::Head use Rack::ConditionalGet use Rack::ETag use ActionDispatch::BestStandardsSupport use Strava::EntityMapMiddleware run Strava::Application.routes -
@panthomakos $ bundle exec rake middleware use Rack::Cors use Airbrake::UserInformer
use ActionDispatch::Static use Rack::Lock use Rack::Runtime use Rack::MethodOverride use ActionDispatch::RequestId use Rails::Rack::Logger use ActionDispatch::ShowExceptions use ActionDispatch::DebugExceptions use BetterErrors::Middleware use Airbrake::Rails::Middleware use ActionDispatch::RemoteIp use ActionDispatch::Reloader use ActionDispatch::Callbacks use ActiveRecord::ConnectionAdapters::ConnectionManagement use ActiveRecord::QueryCache use ActionDispatch::Cookies use ActionDispatch::Session::CookieStore use ActionDispatch::Flash use ActionDispatch::ParamsParser use ActionDispatch::Head use Rack::ConditionalGet use Rack::ETag use ActionDispatch::BestStandardsSupport use Strava::EntityMapMiddleware run Strava::Application.routes -
@panthomakos $ bundle exec rake middleware use Rack::Cors use Airbrake::UserInformer
use ActionDispatch::Static use Rack::Lock use Rack::Runtime use Rack::MethodOverride use ActionDispatch::RequestId use Rails::Rack::Logger use ActionDispatch::ShowExceptions use ActionDispatch::DebugExceptions use BetterErrors::Middleware use Airbrake::Rails::Middleware use ActionDispatch::RemoteIp use ActionDispatch::Reloader use ActionDispatch::Callbacks use ActiveRecord::ConnectionAdapters::ConnectionManagement use ActiveRecord::QueryCache use ActionDispatch::Cookies use ActionDispatch::Session::CookieStore use ActionDispatch::Flash use ActionDispatch::ParamsParser use ActionDispatch::Head use Rack::ConditionalGet use Rack::ETag use ActionDispatch::BestStandardsSupport use Strava::EntityMapMiddleware run Strava::Application.routes -
@panthomakos $ bundle exec rake middleware use Rack::Cors use Airbrake::UserInformer
use ActionDispatch::Static use Rack::Lock use Rack::Runtime use Rack::MethodOverride use ActionDispatch::RequestId use Rails::Rack::Logger use ActionDispatch::ShowExceptions use ActionDispatch::DebugExceptions use BetterErrors::Middleware use Airbrake::Rails::Middleware use ActionDispatch::RemoteIp use ActionDispatch::Reloader use ActionDispatch::Callbacks use ActiveRecord::ConnectionAdapters::ConnectionManagement use ActiveRecord::QueryCache use ActionDispatch::Cookies use ActionDispatch::Session::CookieStore use ActionDispatch::Flash use ActionDispatch::ParamsParser use ActionDispatch::Head use Rack::ConditionalGet use Rack::ETag use Strava::EntityMapMiddleware use ActionDispatch::BestStandardsSupport run Strava::Application.routes -
@panthomakos $ bundle exec rake middleware use Rack::Cors use Airbrake::UserInformer
use ActionDispatch::Static use Rack::Lock use Rack::Runtime use Rack::MethodOverride use ActionDispatch::RequestId use Rails::Rack::Logger use ActionDispatch::ShowExceptions use ActionDispatch::DebugExceptions use BetterErrors::Middleware use Airbrake::Rails::Middleware use ActionDispatch::RemoteIp use ActionDispatch::Reloader use ActionDispatch::Callbacks use ActiveRecord::ConnectionAdapters::ConnectionManagement use ActiveRecord::QueryCache use ActionDispatch::Cookies use ActionDispatch::Session::CookieStore use ActionDispatch::Flash use ActionDispatch::ParamsParser use ActionDispatch::Head use Rack::ConditionalGet use Rack::ETag use Strava::EntityMapMiddleware use ActionDispatch::BestStandardsSupport run Strava::Application.routes -
@panthomakos $ bundle exec rake middleware use Rack::Cors use Airbrake::UserInformer
use ActionDispatch::Static use Rack::Lock use Rack::Runtime use Rack::MethodOverride use ActionDispatch::RequestId use Rails::Rack::Logger use ActionDispatch::ShowExceptions use ActionDispatch::DebugExceptions use BetterErrors::Middleware use Airbrake::Rails::Middleware use ActionDispatch::RemoteIp use ActionDispatch::Reloader use ActionDispatch::Callbacks use ActiveRecord::ConnectionAdapters::ConnectionManagement use ActiveRecord::QueryCache use ActionDispatch::Cookies use ActionDispatch::Session::CookieStore use ActionDispatch::Flash use ActionDispatch::ParamsParser use ActionDispatch::Head use Rack::ConditionalGet use Rack::ETag use Strava::EntityMapMiddleware use ActionDispatch::BestStandardsSupport run Strava::Application.routes -
@panthomakos $ bundle exec rake middleware use Rack::Cors use Airbrake::UserInformer
use ActionDispatch::Static use Rack::Lock use Rack::Runtime use Rack::MethodOverride use ActionDispatch::RequestId use Rails::Rack::Logger use ActionDispatch::ShowExceptions use ActionDispatch::DebugExceptions use BetterErrors::Middleware use Airbrake::Rails::Middleware use ActionDispatch::RemoteIp use ActionDispatch::Reloader use ActionDispatch::Callbacks use ActiveRecord::ConnectionAdapters::ConnectionManagement use ActiveRecord::QueryCache use ActionDispatch::Cookies use ActionDispatch::Session::CookieStore use ActionDispatch::Flash use ActionDispatch::ParamsParser use ActionDispatch::Head use Rack::ConditionalGet use Strava::EntityMapMiddleware use Rack::ETag use ActionDispatch::BestStandardsSupport run Strava::Application.routes -
@panthomakos def call(env) status, headers, body = @app.call(env) if etag_status?(status)
&& etag_body?(body) && !skip_caching?(headers) digest, body = digest_body(body) headers['ETag'] = %("#{digest}") if digest end unless headers['Cache-Control'] if digest headers['Cache-Control'] = @cache_control if @cache_control else headers['Cache-Control'] = @no_cache_control if @no_cache_control end end [status, headers, body] end -
@panthomakos def call(env) status, headers, body = @app.call(env) if etag_status?(status)
&& etag_body?(body) && !skip_caching?(headers) digest, body = digest_body(body) headers['ETag'] = %("#{digest}") if digest end unless headers['Cache-Control'] if digest headers['Cache-Control'] = @cache_control if @cache_control else headers['Cache-Control'] = @no_cache_control if @no_cache_control end end [status, headers, body] end -
@panthomakos private def digest_body(body) parts = [] body.each { |part|
parts << part } string_body = parts.join digest = Digest::MD5.hexdigest(string_body) unless string_body.empty? [digest, parts] end -
@panthomakos private def digest_body(body) parts = [] body.each { |part|
parts << part } string_body = parts.join digest = Digest::MD5.hexdigest(string_body) unless string_body.empty? [digest, parts] end -
@panthomakos private def digest_body(body) parts = [] body.each { |part|
parts << part } string_body = parts.join digest = Digest::MD5.hexdigest(string_body) unless string_body.empty? [digest, parts] end -
@panthomakos def call(env) status, headers, body = @app.call(env) if etag_status?(status)
&& etag_body?(body) && !skip_caching?(headers) digest, body = digest_body(body) headers['ETag'] = %("#{digest}") if digest end unless headers['Cache-Control'] if digest headers['Cache-Control'] = @cache_control if @cache_control else headers['Cache-Control'] = @no_cache_control if @no_cache_control end end [status, headers, body] end -
@panthomakos def call(env) status, headers, body = @app.call(env) # if
etag_status?(status) && etag_body?(body) && !skip_caching?(headers) # digest, body = digest_body(body) # headers['ETag'] = %("#{digest}") if digest # end unless headers['Cache-Control'] if digest headers['Cache-Control'] = @cache_control if @cache_control else headers['Cache-Control'] = @no_cache_control if @no_cache_control end end [status, headers, body] end -
@panthomakos def call(env) status, headers, body = @app.call(env) if etag_status?(status)
&& etag_body?(body) && !skip_caching?(headers) original_body = body digest, new_body = digest_body(body) body = Rack::BodyProxy.new(new_body) do original_body.close if original_body.respond_to?(:close) end headers[ETAG_STRING] = %(W/"#{digest}") if digest end unless headers[CACHE_CONTROL] if digest headers[CACHE_CONTROL] = @cache_control if @cache_control else headers[CACHE_CONTROL] = @no_cache_control if @no_cache_control end end [status, headers, body] end -
@panthomakos def call(env) status, headers, body = @app.call(env) if etag_status?(status)
&& etag_body?(body) && !skip_caching?(headers) original_body = body digest, new_body = digest_body(body) body = Rack::BodyProxy.new(new_body) do original_body.close if original_body.respond_to?(:close) end headers[ETAG_STRING] = %(W/"#{digest}") if digest end unless headers[CACHE_CONTROL] if digest headers[CACHE_CONTROL] = @cache_control if @cache_control else headers[CACHE_CONTROL] = @no_cache_control if @no_cache_control end end [status, headers, body] end -
@panthomakos module Strava class EntityMapMiddleware def initialize(app) @app = app
end def call(env) status, headers, body = @app.call(env) [status, headers, BodyProxy.new(body)] end end end -
@panthomakos module Strava class EntityMapMiddleware def initialize(app) @app = app
end def call(env) ::Strava::EntityMap.clear! @app.call(env) end end end -
@panthomakos class MyTimer def initialize(app) @app = app end def
call(env) start = ::Timer.cputime response = @app.call(env) finish = ::Timer.cputime puts "Response Time: #{finish - start}ms" response end end -
@panthomakos class MyTimer def initialize(app) @app = app end def
call(env) start = ::Timer.cputime response = @app.call(env) finish = ::Timer.cputime puts "Response Time: #{finish - start}ms" response end end -
@panthomakos class MyTimer def initialize(app) @app = app end def
call(env) start = ::Timer.cputime response = @app.call(env) finish = ::Timer.cputime puts "Response Time: #{finish - start}ms" response end end -
@panthomakos class MyTimer def before(_request) @start = Timer.cputime end def
after(_response) @finish = Timer.cputime puts "Response Time: #{@finish - @start}ms" end end -
@panthomakos Middleware Lite Miller Lite Beer Mat / Roger W
/ CC BY-SA 2.0