Class EvaluationExposureDeduper

java.lang.Object
com.launchdarkly.sdk.android.integrations.EvaluationExposureDeduper

public class EvaluationExposureDeduper extends Object
Decides whether a hook should be told about an evaluation, so that repeated evaluations resolving to the same result do not invoke the hook again within a time window.

Deduplication is opt-in per hook: a hook is told about every evaluation until you wrap it in a DedupingHook, which is what consults a deduper.


     Components.hooks()
         .addHook(new MetricsHook())                        // told about every evaluation
         .addHook(new DedupingHook(new ObservabilityHook())) // default window
         .addHook(new DedupingHook(new TelemetryHook(), 30_000))
         .addHook(new DedupingHook(new ExperimentHook(), myCustomDeduper))
 

This class is the SDK's implementation: it remembers the result each flag last reported, and tells the hook about the flag again as soon as that result changes, or once the window elapses while it stays the same. Tracking one result per flag rather than every result seen keeps a flag that flips back and forth from hiding the flips, and holds one record per flag the application evaluates, so the window is the only thing there is to configure. Subclass this to implement a different policy; only shouldRecord(EvaluationExposureKey, long) and reset() are called by DedupingHook.

A deduper is consulted once per evaluation, before the series opens, so a suppressed evaluation invokes neither beforeEvaluation nor afterEvaluation. Implementations must be thread-safe, because evaluations may be made from any thread. Give each hook its own instance unless you intend hooks to share a window: the first hook to be told about an exposure starts the window that suppresses the rest.

  • Field Details

    • DEFAULT_WINDOW_MILLIS

      public static final int DEFAULT_WINDOW_MILLIS
      The dedupe window used by a deduper built without a window of its own: 10 minutes, in milliseconds.
      See Also:
  • Constructor Details

    • EvaluationExposureDeduper

      public EvaluationExposureDeduper()
      Creates a deduper with a window of DEFAULT_WINDOW_MILLIS.
    • EvaluationExposureDeduper

      public EvaluationExposureDeduper(int windowMillis)
      Parameters:
      windowMillis - the dedupe window in milliseconds; zero or negative disables deduplication, so every evaluation reaches the hook
  • Method Details

    • shouldRecord

      public boolean shouldRecord(EvaluationExposureKey key, long nowMillis)
      Returns whether the hook should be told about the evaluation identified by the given key, and if so starts a new dedupe window for the flag.

      DedupingHook calls this once per evaluation. This implementation answers true when the flag is reporting a different result than it last did, and when the window has elapsed on the result it is repeating. See EvaluationExposureKey for what makes two evaluations the same result.

      Parameters:
      key - the key identifying the evaluation result
      nowMillis - a reading of a clock that counts from an arbitrary point, in milliseconds. DedupingHook passes SystemClock.elapsedRealtime(), so that correcting the device clock cannot stretch a window. Only differences between readings are meaningful: this is not a time of day, and comparing it with System.currentTimeMillis() is a mistake.
      Returns:
      true if the hook should observe this evaluation, false if it should be suppressed
    • reset

      public void reset()
      Clears all recorded exposures, so the next evaluation of each is reported again. DedupingHook calls this when the evaluation context changes.