Skip to content

New feature proposal: New Policy: "Polling" #397

Description

@avavakin

Polling

TL;DR: create a Polling which will encapsulate the polling strategy of some java code (e.g. API calls)

Case example

A service polls /get-some-task to monitor background jobs
A static 1s interval is inefficient: it over-polls long tasks with many requests and under-polls short tasks (too long delay)
The goal is to reach the target state of optimal polling range (configurable, but let's say 1-3 polls)

API proposal

// Declaration
final Polling<T> polling = Polling.adaptiveBuilder()
    .targetPolls(1.5)
    .minDelay(Duration.ofMillis(500))
    .maxDelay(Duration.ofSeconds(10))
    .timout(Duration.ofMinutes(1))
    .basedOnLast(10) // memory of adaptive poller on which it's calculate the avg
    .build();

// Usage
final var pollResult = polling.poll(() -> someCustomJavaCode());

Behaviour proposal (first strategy implementation - Adaptive with memory)

Estimation: Maintain an Exponentially Weighted Moving Average (EWMA) of total task durations.
Calculation: T_avg = α * T_actual_duration + ( 1 - α ) * T_avg

Where α - is configurable volatility coefficient in range (0, 1), let say by default 0.2
If task completion takes longer than the average, the algorithm treats it as a slow-outlier and adjusts the moving average upward, increasing the delay for subsequent polls.

Potential

  • Allow users to configure other strategies of defining delay, based on some input data
  • Allow users to configure strategy of defining taken time of pollable task (for example parse last response from poll, extract start and finish timestamps and provide duration as result) - by default: just calculate duration between first poll and last poll

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions