kickstarter/slave_pools — pull request #6 by cainlevy (2013-12-19 16:51:28+00:00)
Title: Updating and Refactoring

The SlavePools codebase was mostly inherited a couple years ago. This pull request modernizes, refactors, and streamlines it. Feedback is desired! This is a pretty important piece of infrastructure. If the diff is too noisy, it may be easier to just browse the [new tree](https://github.com/kickstarter/slave_pools/tree/owningit).

Aside from refactoring, I did accumulate a few notable changes along the way:

### Extractions

* Extracted stateful objects to thread vars (`SlavePools.proxy` and `SlavePools.pools`, which both maintain "current" values).
* Extracted a `SlavePools.config` object for a few settings that used to be on `SlavePools::ConnectionProxy`.

### Changes to `setup!`

* Automated `setup!` using `SlavePools::Engine.config.after_initialize`. This happens after `config/initializers` so devs have time to alter config values.
* Altered `setup!` so that it _always_ integrates into the system. If no replicas are configured in `database.yml`, it creates a default pool containing master. This reduces the difference between dev, test, and prod.

### Removed

* Removed support for `master_models`. If we want to reintroduce the feature later, I'd like to do it with an imperative syntax (`FooModel.use_master!`) instead of a configuration syntax (`SlavePools.config.master_models << 'FooModel'`).
* Removed a `reconnect_master` behavior that was implemented dangerously. It would trigger after _any_ exception, even a duplicate key violation, and then cause a reconnect on some _later_ query. A reconnect will wipe the query cache and implicitly rollback transactions.

### Fixes

* Fixed `SlavePools.configs.defaults_to_master` so that it allows switching to replicas via `with_pool`.