Skip to content

Commit

Permalink
Dialogue ChannelCache uses weak values to allow collection of old data (
Browse files Browse the repository at this point in the history
#2194)

Dialogue ChannelCache uses weak values to allow collection of old data
  • Loading branch information
carterkozak authored Mar 11, 2024
1 parent 9130704 commit 7f43e18
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-2194.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: improvement
improvement:
description: Dialogue ChannelCache uses weak values to allow collection of old data
links:
- https://github.com/palantir/dialogue/pull/2194
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,11 @@ final class ChannelCache {
*/
private final Map<String, ApacheCacheEntry> apacheCache = new ConcurrentHashMap<>();

private final LoadingCache<ChannelCacheKey, DialogueChannel> channelCache =
Caffeine.newBuilder().maximumSize(MAX_CACHED_CHANNELS).build(this::createNonLiveReloadingChannel);
private final LoadingCache<ChannelCacheKey, DialogueChannel> channelCache = Caffeine.newBuilder()
.maximumSize(MAX_CACHED_CHANNELS)
// Avoid holding onto old targets, which is now more common as we bind to resolved IP addresses
.weakValues()
.build(this::createNonLiveReloadingChannel);
private final int instanceNumber;

private ChannelCache() {
Expand Down Expand Up @@ -106,8 +109,15 @@ DialogueChannel getNonReloadingChannel(
List<TargetUri> uris,
@Safe String channelName,
OptionalInt overrideHostIndex) {
if (log.isWarnEnabled() && channelCache.estimatedSize() >= MAX_CACHED_CHANNELS * 0.75) {
log.warn("channelCache nearing capacity - possible bug? {}", SafeArg.of("cache", this));
if (log.isWarnEnabled()) {
long estimatedSize = channelCache.estimatedSize();
if (estimatedSize >= MAX_CACHED_CHANNELS * 0.75) {
log.warn(
"channelCache nearing capacity - possible bug? {} {} {}",
SafeArg.of("estimatedSize", estimatedSize),
SafeArg.of("maxSize", MAX_CACHED_CHANNELS),
SafeArg.of("cache", this));
}
}

return channelCache.get(ImmutableChannelCacheKey.builder()
Expand Down Expand Up @@ -199,6 +209,7 @@ private static ServiceConfiguration stripUris(ServiceConfiguration serviceConf)
.build();
}

@Safe
@Override
public String toString() {
return "ChannelCache{"
Expand Down

0 comments on commit 7f43e18

Please sign in to comment.