: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
use sqlx::Row;
/// Per-team error-tracking settings loaded from `posthog_errortrackingsettings`.
/// The adaptive preference shares the cached read with two manual limits.
/// Those limits match the Node.js per-issue and project-wide caps; a missing
/// value disables that limit for the team.
#[derive(Debug, Clone, Default)]
pub struct RateLimitSettings {
pub adaptive_ingestion_enabled: bool,
pub project_value: Option<i32>,
pub project_bucket_minutes: Option<i32>,
pub per_issue_value: Option<i32>,
pub per_issue_bucket_minutes: Option<i32>,
}
/// Token-bucket parameters for one limit: `max` is the burst size (bucket
/// capacity), `rate` is the steady refill in tokens/second.
#[derive(Debug, Clone, Copy)]
pub struct BucketParams {
pub max: f64,
pub rate: f64,
}
impl RateLimitSettings {
fn to_bucket_params(value: Option<i32>, minutes: Option<i32>) -> Option<BucketParams> {
let value = value?;
if value <= 0 {
return None;
}
// "value events per `minutes` minutes" → bucket size `value`,
// refill `value / (minutes * 60)` tokens per second.
let minutes = minutes.unwrap_or(60).max(1);
Some(BucketParams {
max: value as f64,
rate: value as f64 / (minutes as f64 * 60.0),
})
}
pub fn per_issue(&self) -> Option<BucketParams> {
Self::to_bucket_params(self.per_issue_value, self.per_issue_bucket_minutes)
}
pub fn project(&self) -> Option<BucketParams> {
Self::to_bucket_params(self.project_value, self.project_bucket_minutes)
}
pub async fn load_for_team<'c, E>(conn: E, team_id: i32) -> Result<Option<Self>, sqlx::Error>
where
E: sqlx::Executor<'c, Database = sqlx::Postgres>,
{
let row = sqlx::query(
"SELECT adaptive_ingestion_enabled, project_rate_limit_value, project_rate_limit_bucket_size_minutes, \
per_issue_rate_limit_value, per_issue_rate_limit_bucket_size_minutes \
FROM posthog_errortrackingsettings WHERE team_id = $1",
)
.bind(team_id)
.fetch_optional(conn)
.await?;
row.map(|r| {
Ok(Self {
adaptive_ingestion_enabled: r.try_get("adaptive_ingestion_enabled")?,
project_value: r.try_get("project_rate_limit_value")?,
project_bucket_minutes: r.try_get("project_rate_limit_bucket_size_minutes")?,
per_issue_value: r.try_get("per_issue_rate_limit_value")?,
per_issue_bucket_minutes: r.try_get("per_issue_rate_limit_bucket_size_minutes")?,
})
})
.transpose()
}
}
#[cfg(test)]
mod tests {
use sqlx::PgPool;
use super::*;
#[test]
fn disabled_when_value_missing_or_nonpositive() {
let s = RateLimitSettings::default();
assert!(s.per_issue().is_none());
assert!(s.project().is_none());
let s = RateLimitSettings {
project_value: Some(0),
..Default::default()
};
assert!(s.project().is_none());
}
#[test]
fn bucket_params_from_value_and_minutes() {
let s = RateLimitSettings {
project_value: Some(120),
project_bucket_minutes: Some(2),
per_issue_value: Some(60),
per_issue_bucket_minutes: None, // defaults to 60 minutes
adaptive_ingestion_enabled: false,
};
let project = s.project().unwrap();
assert_eq!(project.max, 120.0);
assert!((project.rate - 1.0).abs() < 1e-9); // 120 / (2*60) = 1/s
let per_issue = s.per_issue().unwrap();
assert_eq!(per_issue.max, 60.0);
assert!((per_issue.rate - (60.0 / 3600.0)).abs() < 1e-9);
}
#[sqlx::test]
async fn loads_adaptive_preference_with_manual_limits(db: PgPool) {
sqlx::query(
"CREATE TABLE posthog_errortrackingsettings (\
team_id INT PRIMARY KEY, adaptive_ingestion_enabled BOOLEAN NOT NULL, \
project_rate_limit_value INT, project_rate_limit_bucket_size_minutes INT, \
per_issue_rate_limit_value INT, per_issue_rate_limit_bucket_size_minutes INT)",
)
.execute(&db)
.await
.unwrap();
sqlx::query(
"INSERT INTO posthog_errortrackingsettings \
(team_id, adaptive_ingestion_enabled, project_rate_limit_value) \
VALUES (7, TRUE, 120), (8, FALSE, 120)",
)
.execute(&db)
.await
.unwrap();
let enabled = RateLimitSettings::load_for_team(&db, 7)
.await
.unwrap()
.unwrap();
assert!(enabled.adaptive_ingestion_enabled);
assert_eq!(enabled.project().unwrap().max, 120.0);
let disabled = RateLimitSettings::load_for_team(&db, 8)
.await
.unwrap()
.unwrap();
assert!(!disabled.adaptive_ingestion_enabled);
assert_eq!(disabled.project().unwrap().max, 120.0);
assert!(RateLimitSettings::load_for_team(&db, 9)
.await
.unwrap()
.is_none());
}
}