Skip to content

Commit

Permalink
Merge pull request #252 from leocassarani/constants-mutex
Browse files Browse the repository at this point in the history
Make the @@Constants Hash thread-safe by wrapping it in a Mutex
  • Loading branch information
technoweenie committed Apr 17, 2013
2 parents 50f8530 + 76701fd commit c4d8354
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/faraday/rack_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,19 @@ class StackLocked < RuntimeError; end
# borrowed from ActiveSupport::Dependencies::Reference &
# ActionDispatch::MiddlewareStack::Middleware
class Handler
@@constants_mutex = Mutex.new
@@constants = Hash.new { |h, k|
h[k] = k.respond_to?(:constantize) ? k.constantize : Object.const_get(k)
value = k.respond_to?(:constantize) ? k.constantize : Object.const_get(k)
@@constants_mutex.synchronize { h[k] = value }
}

attr_reader :name

def initialize(klass, *args, &block)
@name = klass.to_s
@@constants[@name] = klass if klass.respond_to?(:name)
if klass.respond_to?(:name)
@@constants_mutex.synchronize { @@constants[@name] = klass }
end
@args, @block = args, block
end

Expand Down

0 comments on commit c4d8354

Please sign in to comment.