Skip to content

ConceptsPeriodic polling

Periodic polling

How a polling source schedules refreshes, reports changed or unchanged, handles failures and stops.

Some data offers nothing to subscribe to, such as a REST API or a database. A polling source asks on a timer. It serves its initial collection until a refresh reports a change. Where polling sits among the other sources is on data sources and ownership.

The source makes a PeriodicTimer in its constructor, on the TimeProvider it is given, TimeProvider.System by default, and it ticks every refreshInterval. The first refresh follows the first tick.

DefaultCollectionDataSourceFactory passes the TimeProvider it was constructed with, and AddLiveCollections() registers TimeProvider.System unless the container has a TimeProvider already, so a FakeTimeProvider registered first drives every polling source, as poll a remote source shows.

Refreshes run one after another on one thread-pool task and never overlap. A tick that falls during a refresh is kept, so a refresh slower than the interval is followed at once by the next.

The callback receives three things: the state passed to the constructor, current, the collection last stored (the initial one, or the last changed one), and a cancellation token that DisposeAsync cancels.

It returns one of two results:

  • RefreshCollectionResultFactory.Changed(...) stores the new collection as given and signals. The source does not compare it with current, so a Changed result with the same items still signals and rebuilds.
  • RefreshCollectionResultFactory.Unchanged<TItem>() keeps the collection and signals nothing. It is the default value.

Only your code knows what equality means, such as an ETag or a version column, so the comparison is yours.

Each exception the refresh throws goes to HandleExceptionCallback, as an ExceptionDispatchInfo, with the same token.

  • A handler that returns keeps the collection, and polling goes on.
  • A handler that throws ends polling. The exception is swallowed.
  • With no handler, the default rethrows, so the first failure ends polling.
  • An OperationCanceledException for the disposal token ends polling.

Once polling ends, consumers see no signal and no exception, and the collection stays as last stored. A caller that wants retries passes a handler that logs and returns.

DisposeAsync cancels the token, awaits the polling task and empties the collection.