Trait Datasource

Source
pub trait Datasource: Send + Sync {
    // Required methods
    fn consume<'life0, 'async_trait>(
        &'life0 self,
        id: DatasourceId,
        sender: Sender<(Update, DatasourceId)>,
        cancellation_token: CancellationToken,
        metrics: Arc<MetricsCollection>,
    ) -> Pin<Box<dyn Future<Output = CarbonResult<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;
    fn update_types(&self) -> Vec<UpdateType>;
}
Expand description

Defines the interface for data sources that produce updates for accounts, transactions, and account deletions.

The Datasource trait represents a data source that can be consumed asynchronously within a pipeline. Implementations of this trait are responsible for fetching updates and sending them through a channel to be processed further. Each datasource specifies the types of updates it supports by implementing the update_types method.

§Required Methods

  • consume: Initiates the asynchronous consumption of updates. This method should send updates through the provided sender channel.
  • update_types: Returns a list of UpdateType variants indicating the types of updates the datasource can provide.

§Example

use std::sync::Arc;
use carbon_core::datasource::UpdateType;
use carbon_core::datasource::Update;
use carbon_core::error::CarbonResult;
use carbon_core::metrics::MetricsCollection;
use carbon_core::datasource::Datasource;
use tokio_util::sync::CancellationToken;
use async_trait::async_trait;

#[async_trait]
impl Datasource for MyDatasource {
    async fn consume(
        &self,
        sender: &tokio::sync::mpsc::UnboundedSender<Update>,
        cancellation_token: CancellationToken,
        metrics: Arc<MetricsCollection>,
    ) -> CarbonResult<tokio::task::AbortHandle> {
        // Implement update fetching logic
    }

    fn update_types(&self) -> Vec<UpdateType> {
        vec![UpdateType::AccountUpdate, UpdateType::Transaction]
    }
}

§Notes

  • This trait is marked with async_trait, so implementations must be asynchronous.
  • The consume method should handle errors and retries to ensure robust update delivery.

Required Methods§

Source

fn consume<'life0, 'async_trait>( &'life0 self, id: DatasourceId, sender: Sender<(Update, DatasourceId)>, cancellation_token: CancellationToken, metrics: Arc<MetricsCollection>, ) -> Pin<Box<dyn Future<Output = CarbonResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source

fn update_types(&self) -> Vec<UpdateType>

Implementors§