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

View file

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