Skip to main content

Overview

The Source trait defines how candidate content is fetched from various retrieval systems. Sources run in parallel and their results are combined before continuing through the pipeline.

Trait Definition

Type Parameters

generic
The query type that contains request context and parametersConstraints: Clone + Send + Sync + 'static
generic
The candidate type that represents a single content itemConstraints: Clone + Send + Sync + 'static

Methods

enable

fn
Determines whether this source should run for the given query
&Q
Reference to the query object
bool
Returns true if this source should fetch candidates, false to skip. Default implementation returns true.

get_candidates

async fn
Fetches candidates from the source’s retrieval system
&Q
Reference to the query object containing request parameters
Result<Vec<C>, String>
Returns a vector of candidates on success, or an error message string on failure

name

fn
Returns a stable name for logging and metrics
&'static str
A short type name derived from the implementing struct. Default implementation uses reflection to extract the struct name.

Example Implementation

Here’s a real example from the X For You feed that retrieves candidates from the Phoenix recommendation system:

Usage Notes

  • Multiple sources can be configured to run in parallel
  • Results from all enabled sources are combined before hydration
  • Sources should return candidates quickly; expensive operations should be deferred to hydrators
  • The enable method allows dynamic source selection based on query parameters
  • Error messages should be descriptive for debugging and monitoring

See Also