class Auth
def initialize(callback)
@callback = callback
end
def call(info)
@user = user_from_info(info)
res = @callback.call(info)
update_user(res) # access @user
res
end
end
class Auth
def initialize(callback)
@callback = callback
end
def call(info)
@user = user_from_info(info)
res = @callback.call(info)
update_user(res) # access @user
res
end
end
Slide 60
Slide 60 text
class AuthMiddleware
def initialize(callback)
@callback = callback
end
def call(info)
@user = user_from_info(info)
res = @callback.call(info)
update_user(res) # access @user
res
end
end
Slide 61
Slide 61 text
config.middleware.use(
AuthMiddleware
)
Slide 62
Slide 62 text
Eager loading
Slide 63
Slide 63 text
class MyApp < Sinatra::Base
autoload :Broker, "my_app/broker"
post "/content" do
# ...
MyApp::Broker.push(info)
end
end
Threadsafe?
Slide 64
Slide 64 text
module MyApp::Broker
# ...
def push do
# ...
end
# ...
end
Slide 65
Slide 65 text
module MyApp::Broker
# ...
def push do
# ...
end
# ...
end
Slide 66
Slide 66 text
module MyApp::Broker
# ...
def push do
# ...
end
# ...
end
Slide 67
Slide 67 text
Let’s replace all autoloads
by requires!
Slide 68
Slide 68 text
module MyApp
extend ActiveSupport::Autoload
eager_autoload do
autoload :Broker
end
end
MyApp.eager_load!
Slide 69
Slide 69 text
config.eager_load_namespaces << MyApp
Slide 70
Slide 70 text
Mutex
Slide 71
Slide 71 text
hash = {}
10.times do |i|
Thread.new {
hash[i] = "Hello #{rand}"
}
end
Slide 72
Slide 72 text
No content
Slide 73
Slide 73 text
“There are heaps of non
thread-safe usage of
Hashes in Rails and the
surrounding gems”
Slide 74
Slide 74 text
Using hashes as a cache
is an extremely common
pattern in Ruby