Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

1.7 KB
Raw Blame
use std::sync::Arc;

use async_trait::async_trait;

use crate::budget::BatchBudget;
use crate::context::{OkResultWithContext, ResultWithContext};
use crate::metrics::{record_budget_checkpoint, BudgetCheckpoint};
use crate::pipeline::{DynPipeline, Pipeline};
use crate::results::{NonOk, Outcome};

const TIMEOUT_BARRIER: &str = "timeoutBarrier";

/// An element with time left continues with an unlimited budget, so it always finishes.
pub struct TimeoutBarrierPipeline<I, O, C> {
    previous: DynPipeline<I, O, C>,
}

impl<I, O, C> TimeoutBarrierPipeline<I, O, C> {
    pub fn new(previous: DynPipeline<I, O, C>) -> Self {
        Self { previous }
    }
}

#[async_trait]
impl<I, O, C> Pipeline<I, O, C> for TimeoutBarrierPipeline<I, O, C>
where
    I: Send + 'static,
    O: Send + 'static,
    C: Send + 'static,
{
    async fn process(
        &self,
        input: OkResultWithContext<I, C>,
    ) -> anyhow::Result<ResultWithContext<O, C>> {
        let mut result = self.previous.process(input).await?;
        let Some(budget) = result.context.budget.as_ref() else {
            return Ok(result);
        };
        if !result.is_ok() {
            return Ok(result);
        }
        if budget.exhausted() {
            record_budget_checkpoint(BudgetCheckpoint::Step, Arc::from(TIMEOUT_BARRIER));
            return Ok(ResultWithContext {
                result: Outcome::NonOk(NonOk::Timeout {
                    reason: format!("budget exceeded before {TIMEOUT_BARRIER}"),
                }),
                context: result.context,
            });
        }
        if !budget.is_unlimited() {
            result.context.budget = Some(BatchBudget::unlimited());
        }
        Ok(result)
    }
}