Class DedupingHook


public final class DedupingHook extends HookDecorator
Wraps a hook so that repeated evaluations resolving to the same result do not reach it again within a time window.

The wrapped hook is told about a flag when its result changes, and at most once per window while the result stays the same. This is useful for reducing the telemetry volume produced by frequent re-evaluations, for example a flag that is read on every redraw of a view. Deduplication is opt-in: a hook that is registered unwrapped observes every evaluation.

This class is not stable, and not subject to any backwards compatibility guarantees or semantic versioning. It is experimental.


     Components.hooks()
         .addHook(new MetricsHook())                                  // observes every evaluation
         .addHook(new DedupingHook(new ObservabilityHook()))           // default window
         .addHook(new DedupingHook(new TelemetryHook(), 60_000))
         .addHook(new DedupingHook(new ExperimentHook(), sharedDeduper))
 

Two evaluations resolve to the same result when they agree on everything EvaluationExposureKey describes.

An evaluation the SDK has no flag data for resolves to the default value, and is the same result as another that does. Evaluations made before the client has flags are of that kind, so the wrapped hook is told about one of them and then told about the flag again as soon as its data arrives.

A suppressed evaluation reaches neither Hook.beforeEvaluation(EvaluationSeriesContext, Map) nor Hook.afterEvaluation(EvaluationSeriesContext, Map, EvaluationDetail), because hooks pair their stages. The identify and track stages are always forwarded. Analytics events are unaffected: feature, debug, and summary events are still recorded for every evaluation, so the evaluation counts LaunchDarkly reports for your flags do not change.

What the wrapped hook has been told about is cleared by LDClient.identify(com.launchdarkly.sdk.LDContext), so the first evaluation of each flag after an identify always reaches it.

Give each hook its own instance unless you intend hooks to share a window: the first hook to be told about an evaluation starts the window that suppresses the rest.

Wrap outermost when you stack decorators. Suppressing an evaluation means returning series data that says so in place of what the stage was given, so a decorator outside this one does not get back what it stored in its own before stage. A decorator inside this one is unaffected, since a suppressed evaluation never reaches it.

  • Constructor Details

    • DedupingHook

      public DedupingHook(Hook delegate)
      Parameters:
      delegate - the hook to wrap
    • DedupingHook

      public DedupingHook(Hook delegate, int windowMillis)
      Parameters:
      delegate - the hook to wrap
      windowMillis - the dedupe window in milliseconds; zero or negative forwards every evaluation
    • DedupingHook

      public DedupingHook(Hook delegate, EvaluationExposureDeduper deduper)
      Parameters:
      delegate - the hook to wrap
      deduper - decides which evaluations reach the wrapped hook
  • Method Details

    • beforeEvaluation

      public Map<String,Object> beforeEvaluation(EvaluationSeriesContext seriesContext, Map<String,Object> seriesData)
      Forwards the evaluation unless the wrapped hook has just been told about the same result.

      The decision is made here, before the evaluation runs, so that a suppressed evaluation reaches neither stage of the wrapped hook. An evaluation whose result the SDK did not describe, which is to say a series context built by something other than the SDK, is always forwarded.

      Overrides:
      beforeEvaluation in class HookDecorator
      Parameters:
      seriesContext - container of parameters associated with this evaluation
      seriesData - immutable data from the previous stage in the evaluation series
      Returns:
      the wrapped hook's series data, or data marking the series as suppressed
    • afterEvaluation

      public Map<String,Object> afterEvaluation(EvaluationSeriesContext seriesContext, Map<String,Object> seriesData, EvaluationDetail<LDValue> evaluationDetail)
      Forwards the result unless this instance suppressed the series in its before stage.
      Overrides:
      afterEvaluation in class HookDecorator
      Parameters:
      seriesContext - container of parameters associated with this evaluation
      seriesData - the data returned by this hook's before stage
      evaluationDetail - the result of the evaluation
      Returns:
      the wrapped hook's series data, unchanged if the series was suppressed
    • beforeIdentify

      public Map<String,Object> beforeIdentify(IdentifySeriesContext seriesContext, Map<String,Object> seriesData)
      Forgets which results the wrapped hook has been told about, then forwards the stage.

      Evaluations observed before an identify describe an earlier point in the application's lifecycle, so they are reported again afterwards. This happens even when the context is unchanged, so that identify is a reliable way for an application to mark a new phase of a session.

      Overrides:
      beforeIdentify in class HookDecorator
      Parameters:
      seriesContext - container of parameters associated with this identify
      seriesData - immutable data from the previous stage in the identify series
      Returns:
      the wrapped hook's series data