@Override public String getTargetSpec(Integer distributor, RoutingContext context) { GenerationCache cache = generationCache; int currentGeneration = getMirror(context).updates(); // The below code might race with other threads during a generation change. That is OK, as the cache // is thread safe and will quickly converge to a stable state for the new generation. if (cache == null || currentGeneration != cache.generation()) { cache = new GenerationCache(currentGeneration); generationCache = cache; } if (distributor != null) { return cachingGetTargetSpec(distributor, context, cache); } // Wildcard lookup case. Must not be cached. return super.getTargetSpec(null, context); }
private String cachingGetTargetSpec(Integer distributor, RoutingContext context, GenerationCache cache) { String cachedTarget = cache.get(distributor); if (cachedTarget != null) { return cachedTarget; } // Mirror _may_ be at a higher version if we race with generation read, but that is OK since // we'll either way get the most up-to-date mapping and the cache will be invalidated on the // next invocation. String resolvedTarget = super.getTargetSpec(distributor, context); cache.put(distributor, resolvedTarget); return resolvedTarget; }