Replace expirable map with guava cache #160
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR (unfortunately) removes the very good ExpirableMap class @nite23 created as part of PR #118. The reason for this is that I began encountering intermittent unit test failures in ExpirableMapTest when running on Java 8. The Java 8 implementation of ConcurrentHashMap has changed, apparently in a way that was occasionally breaking ExpirableMapTest.
I began to look into the issue to fix it, when I discovered that we were already including Guava as part of sitebricks, and on top of that, littleproxy includes Guava -- so we may as well leverage the expiring cache capabilities of Guava and save some additional maintenance.
In additional to replacing ExpirableMap, this PR also adds some unit tests to verify actual expiration (removal) of the expired proxy from the ProxyManager.
Important note: the guava cache does not proactively expire objects. Expiration checks happen on writes and "occasional" reads. This means there may be some overhead to other threads requesting new/deleting old proxies if there are expired proxies that need to be stopped.
It also means if you spin up a large number of proxies and then don't access them via REST calls, they "may" stay in memory past their TTL, perhaps indefinitely. I believe this is a sufficiently rare use case (typically clients would access the proxies they create, otherwise why create them?) so it should not be a noticeable problem. However, if others would prefer proactive expiration, this can be done easily by using the guava cache's cleanUp() method from a scheduled thread.