m3dev/octoparts — pull request #68 by lloydmeta (2014-11-20 09:09:36+00:00)
Title: Fix/enhance KeyedResourcePool

Should address https://github.com/m3dev/octoparts/issues/66

If we want our `KeyedResourcePool` to work well with shared mutable state in general (not cause race conditions in things that it may not know about), we will need to synchronise our accesses to the underlying holder map as well as the conditional side-effecting logic that follows. For`KeyedResourcePool` this is either in the `getOrCreate` and the `cleanObsolete` methods.

This PR implements a naive (?) version of `KeyedResourcePool` called `SyncedKeyedResourcePool` (for now, after iteration we might just replace the old) that uses `synchronized` to do syncing as well as a failing test (passes under `SyncedKeyedResourcePool`).

Another way is to use an Akka actor, which is easier to reason about with (thus safer to maintain) in the long run. It will likely perform worse though (more method calls, construction Futures objects, thread-weaving, possibly others), though I don't know for sure without benching.

Note that all bets are off if there are multiple instances of `KeyedResourcePool` that may affect the same global state.