Permalink
Browse files

wrap writes to @registered_middleware in a mutex

  • Loading branch information...
1 parent 1f6808e commit f3c1d06ad678bcba1da89f2785f278b893e742bf @technoweenie committed Oct 21, 2012
Showing with 34 additions and 13 deletions.
  1. +34 −13 lib/faraday.rb
View
@@ -1,3 +1,5 @@
+require 'thread'
+
# Public: This is the main namespace for Faraday. You can either use it to
# create Faraday::Connection objects, or access it directly.
#
@@ -148,8 +150,10 @@ def register_middleware(autoload_path = nil, mapping = nil)
mapping = autoload_path
autoload_path = nil
end
- @middleware_autoload_path = autoload_path if autoload_path
- (@registered_middleware ||= {}).update(mapping)
+ middleware_mutex do
+ @middleware_autoload_path = autoload_path if autoload_path
+ (@registered_middleware ||= {}).update(mapping)
+ end
end
# Public: Lookup middleware class with a registered Symbol shortcut.
@@ -169,23 +173,40 @@ def register_middleware(autoload_path = nil, mapping = nil)
#
# Returns a middleware Class.
def lookup_middleware(key)
- value = defined?(@registered_middleware) && @registered_middleware[key]
+ load_middleware(key) ||
+ raise(Faraday::Error.new("#{key.inspect} is not registered on #{self}"))
+ end
+
+ def middleware_mutex(&block)
+ (@middleware_mutex ||= Mutex.new).synchronize(&block)
+ end
+
+ def fetch_middleware(key)
+ defined?(@registered_middleware) && @registered_middleware[key]
+ end
+
+ def load_middleware(key)
+ value = fetch_middleware(key)
case value
when Module then value
when Symbol, String
- @registered_middleware[key] = const_get(value)
+ middleware_mutex do
+ @registered_middleware[key] = const_get(value)
+ end
when Proc
- @registered_middleware[key] = value.call
+ middleware_mutex do
+ @registered_middleware[key] = value.call
+ end
when Array
- const, path = value
- if root = @middleware_autoload_path
- path = "#{root}/#{path}"
+ middleware_mutex do
+ const, path = value
+ if root = @middleware_autoload_path
+ path = "#{root}/#{path}"
+ end
+ require(path)
+ @registered_middleware[key] = const
end
- require(path)
- @registered_middleware[key] = const
- lookup_middleware(key)
- else
- raise Faraday::Error.new("#{key.inspect} is not registered on #{self}")
+ load_middleware(key)
end
end
end

0 comments on commit f3c1d06

Please sign in to comment.