bazaarvoice/ostrich — pull request #138 by ohhatiya (2013-02-21 20:35:41+00:00)
Title: ServiceCache bugfixes

Fixes a couple bugs with ServiceCache when caching multiple of the same service instance. These would only be encountered if a ServiceFactory could provide equivalent service instances on multiple create calls (most likely if it had its own cache or pool it was drawing from).

1) If multiple copies of an instance were checked out when an end point becomes invalidated, they should now each be invalidated respectively when they are turned in. Since we can't know which instance is checked back in, this does introduce a bit of an edge case of its own. Namely, if an instance is checked out, its end point is invalidated, the end point becomes active again, and the ServiceFactory provides a new copy of the same instance, then each will be destroyed upon respective check in. This could continue indefinitely if there is always at least one copy of the instance checked out.

2) If multiple end points have copies of the same service (why this would happen I am not sure), they should now be able to be invalidated separately. Previously, if an instance was checked out for an end point, that end point was invalidated, and the ServiceFactory provided the same instance for a separate end point, the instance for the invalid end point would be allowed to return to the cache. Now it, and only it, will be destroyed.

3) The possibility of encountering a race condition with creation and eviction is reduced, but not eliminated. If a check out call for an end point is in progress while that end point is evicted, the instance checked out may not be properly invalidated on check in. The cache keeps a revision number that gets incremented each time an end point is evicted, and tracks which revision a check out happens in. The eviction could theoretically increment the revision before the check out notes the current revision. Moving the retrieval of the current revision to the beginning of the checkOut method lowers the likelihood of this happening.