Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
[#164] Make to_s in paginator threadsafe #374
Per #164 and #214 modifies the Paginator to_s method to be threadsafe without creating the overhead of a new thread and without changing the functionality of the code. Tested to work on ruby 1.8 and 1.9 for cruby, jruby, and rubinius.
Essential references I read on threading, concurrency, :
For JRuby, I had to enable c-extensions ( for sqlite3. I couldn't get the jdbc driver working )
I also had to have mongod running
Here are the gems I installed
@jc00ke I believe this solution (which we've tested on our app via loader.io) is the smallest possible change without significantly changing the functionality.
I was unable to make it threadsafe by using threading and mutexes, etc.
The only other solution would be to change
@brixen right now I only wanted to check that it's not cruby, which having RUBY_ENGINE defined is sufficient for, and appears to be at the core of https://github.com/brixen/redcard/blob/master/lib/redcard.rb as well, though I see the advantage of encapsulating the type-check.. If this request gets accepted, perhaps we'll include that. Any reason you didn't use RbConfig::CONFIG['RUBY_INSTALL_VERSION']?
@brixen my bad re: where RUBY_ENGINE is defined. Fortunately, that detail does not affect this pull request.
As and aside, in this pull request I tried not to change anything significant about the rendering hack in the original code or the need for it. It would be better to find a cleaner way to achieve the same goal. e.g. In a twitter discussion Charles Nutter wrote "Would be great to eliminate the singleton…bad for perf."
added a commit
this pull request
May 2, 2013
@matpowel I didn't try the specific code in that commit, but I did try variations of it (using a Mutex) that did not prevent errors. In any case, regarding simplicity, I'd argue that resorting to synchronizing code in order to dynamically add and remove methods to a logging subscriber is more complex than just adding a toggle to the subscriber to temporarily turn off logging. There's still room for code/performance improvements :)
@bf4 the problem to me is that I don't think the new fix is actually threadsafe, we are running our mutex version in production and have tested with very high concurrency, the symptom we had before our mutex fix was exceptions because of race conditions where the method was being defined and un-defined.. the fix in this PR makes the race condition less likely but still a problem I think?
Take this code..
unless defined?(render_partial_with_logging) alias_method :render_partial_with_logging, :render_partial
Thread 1: render_partial_with_logging is not defined
That's off the top of my head, I'm assuming trying to re-define the same alias will throw an exception but if not there are still many other problems with two threads trying to add the same method at the same time. The basic point is that here the class definition is a process singleton (ie shared amongst threads) and is being updated in multiple threads simultaneously which is bad, I don't think you can get around needing to use a mutex except to find a way to switch logging off using instance variables or thread-safe class variables only.
I could be missing something though :/
You make a good point. However, since implementing this code, we have not
On Tue, May 7, 2013 at 6:31 PM, Matt Powell firstname.lastname@example.org: