Make some common shared state, and logging, threadsafe

This commit is contained in:
Ben Hollis 2013-04-09 21:52:25 -07:00
parent fb9a322b6e
commit 16ca3d5c17
2 changed files with 38 additions and 10 deletions

View file

@ -155,37 +155,56 @@ module Middleman
# Methods to be mixed-in to Middleman::Application # Methods to be mixed-in to Middleman::Application
module InstanceMethods module InstanceMethods
# Backwards-compatibility with old request.path signature # Backwards-compatibility with old request.path signature
attr_reader :request def request
Thread.current[:legacy_request]
end
# Accessor for current path # Accessor for current path
# @return [String] # @return [String]
attr_reader :current_path def current_path
Thread.current[:current_path]
end
# Set the current path # Set the current path
# #
# @param [String] path The new current path # @param [String] path The new current path
# @return [void] # @return [void]
def current_path=(path) def current_path=(path)
@current_path = path Thread.current[:current_path] = path
@request = ::Thor::CoreExt::HashWithIndifferentAccess.new({ Thread.current[:legacy_request] = ::Thor::CoreExt::HashWithIndifferentAccess.new({
:path => path, :path => path,
:params => req ? ::Thor::CoreExt::HashWithIndifferentAccess.new(req.params) : {} :params => req ? ::Thor::CoreExt::HashWithIndifferentAccess.new(req.params) : {}
}) })
end end
def use(*args, &block); self.class.use(*args, &block); end delegate :use, :to => :"self.class"
def map(*args, &block); self.class.map(*args, &block); end delegate :map, :to => :"self.class"
# Rack env # Rack env
attr_accessor :env def env
Thread.current[:env]
end
def env=(value)
Thread.current[:env] = value
end
# Rack request # Rack request
# @return [Rack::Request] # @return [Rack::Request]
attr_accessor :req def req
Thread.current[:req]
end
def req=(value)
Thread.current[:req] = value
end
# Rack response # Rack response
# @return [Rack::Response] # @return [Rack::Response]
attr_accessor :res def res
Thread.current[:res]
end
def res=(value)
Thread.current[:res] = value
end
def call(env) def call(env)
dup.call!(env) dup.call!(env)
@ -228,7 +247,7 @@ module Middleman
# @param [Rack::Response] res # @param [Rack::Response] res
def process_request(env, req, res) def process_request(env, req, res)
start_time = Time.now start_time = Time.now
@current_path = nil current_path = nil
request_path = URI.decode(env["PATH_INFO"].dup) request_path = URI.decode(env["PATH_INFO"].dup)
if request_path.respond_to? :force_encoding if request_path.respond_to? :force_encoding

View file

@ -1,5 +1,6 @@
# Use the Ruby/Rails logger # Use the Ruby/Rails logger
require 'active_support/core_ext/logger' require 'active_support/core_ext/logger'
require 'thread'
module Middleman module Middleman
@ -16,6 +17,14 @@ module Middleman
if @instrumenting != false if @instrumenting != false
::ActiveSupport::Notifications.subscribe(/\.middleman$/, self) ::ActiveSupport::Notifications.subscribe(/\.middleman$/, self)
end end
@mutex = Mutex.new
end
def add(*args)
@mutex.synchronize do
super
end
end end
def call(message, *args) def call(message, *args)