rails/rails — pull request #6917 by thedarkone (2012-06-30 22:12:52+00:00)
Title: Thread safety

Finally bit the bullet and wrote a thread safe hash-like `Cache` implementation. It is going to be available through [thread_safe](https://github.com/headius/thread_safe) gem.

I used a custom middleware to hunt down concurrent collection usage: [ThreadSafetyTester](https://github.com/thedarkone/rack-contrib/blob/thread-safety-tester/lib/rack/contrib/thread_safety_tester.rb).

```ruby
thread_safety_test_options = {
:skip_paths => [/\A\/assets\//],
:whitelist => {
Hash => [
'active_support/descendants_tracker.rb', # ok, when eager loaded in production mode
'active_support/dependencies.rb', # dev. mode related
'active_record/connection_adapters/sqlite3_adapter.rb',
'active_record/connection_adapters/abstract/query_cache.rb', # query cache is connection confined
'active_record/connection_adapters/abstract_adapter.rb', # query cache usage
'active_record/connection_adapters/schema_cache.rb', # SchemaCache is connection confined
'webrick/utils.rb',
'webrick/httpresponse.rb'
],
Set => 'active_support/dependencies.rb', # dev. mode related
Array => [
'active_support/dependencies.rb', # dev. mode related
'active_support/log_subscriber.rb', # Thread local array
'active_support/tagged_logging.rb', # Thread local array
'active_record/connection_adapters/abstract/connection_pool.rb', # Synchronized array queue
'active_record/connection_adapters/abstract/database_statements.rb' # connection confined
]
}
}

config.middleware.insert_before 'ActionDispatch::Static', 'Rack::ThreadSafetyTester', thread_safety_test_options
```

I'm not looking to get this merged right away. A feedback on the `ThreadSafe::Cache` API is also welcomed.