Permalink
Browse files

Riak::Client is now threadsafe.

Removes the thread local storage for the riak client and folds it into
@@riak_client
  • Loading branch information...
Kyle Kingsbury
Kyle Kingsbury committed Nov 16, 2011
1 parent 954d339 commit eb85a1a25551818a79de0f2adf48f46bf7d7eb0d
Showing with 9 additions and 24 deletions.
  1. +8 −9 lib/risky.rb
  2. +1 −15 spec/threads.rb
View
@@ -244,29 +244,28 @@ def self.reduce(*args)
# The Riak::Client backing this model class.
def self.riak
- if c = Thread.current["#{self}.riak"]
- c
+ if @riak_client
+ @riak_client
elsif @riak and @riak.respond_to? :call
- Thread.current["#{self}.riak"] = @riak.call(self)
+ @riak_client = @riak.call(self)
elsif @riak
- Thread.current["#{self}.riak"] = @riak
+ @riak_client = @riak
else
superclass.riak
end
end
- # Forces this model and thread's Riak client to be reset.
+ # Forces this model's Riak client to be reset.
# If your @riak proc can choose between multiple hosts, calling this on
# failure will allow subsequent requests to proceed on another host.
def self.riak!
- Thread.current["#{self}.riak"] = nil
+ @riak_client = nil
riak
end
# Sets the Riak Client backing this model class. If client is a lambda (or
# anything responding to #call), it will be invoked to generate a new client
- # every time Risky feels it is appropriate. Clients are stored in
- # thread-local storage, under "Classname.riak".
+ # every time Risky feels it is appropriate.
def self.riak=(client)
@riak = client
end
@@ -497,7 +496,7 @@ def save(opts = {})
end
@riak_object.raw_data = MultiJson.encode @values
- @riak_object.content_type = "application/json"
+ @riak_object.content_type = "application/json"
store_opts = {}
store_opts[:w] = opts[:w] if opts[:w]
View
@@ -2,7 +2,7 @@
require File.expand_path("#{File.dirname(__FILE__)}/init.rb")
-Risky.riak = lambda { Riak::Client.new(:host => '127.0.0.1') }
+Risky.riak = proc { Riak::Client.new(:host => '127.0.0.1') }
class Crud < Risky
bucket 'crud'
@@ -25,20 +25,6 @@ def self.merge(versions)
end
describe 'Threads' do
- should 'use a different client in each thread with lambdas' do
- clients = []
- (0..3).map do |i|
- Thread.new do
- clients << Risky.riak
- end
- end.each do |thread|
- thread.join
- end
-
- # Verify clients are distinct
- clients.uniq.should == clients
- end
-
should 'support concurrent modification' do
Concurrent.bucket.props['allow_mult'].should.be.true

0 comments on commit eb85a1a

Please sign in to comment.