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.

fix(cli): speed up symbol set uploads (#75297)

Hugues Pouillotccd7de59e039
diff --git a/cli/.sampo/changesets/speed-up-symbol-set-uploads.md b/cli/.sampo/changesets/speed-up-symbol-set-uploads.md
new file mode 100644
index 000000000000..8a14a642d60e
--- /dev/null
+++ b/cli/.sampo/changesets/speed-up-symbol-set-uploads.md
@@ -0,0 +1,5 @@
+---
+cargo/posthog-cli: patch
+---
+
+Symbol set uploads (`sourcemap upload`, `sourcemap upload-hermes`, `symbol-sets upload`, dSYM and Proguard uploads) are now significantly faster: chunk uploads reuse a single HTTP connection pool instead of opening a fresh TLS connection per chunk, payload content hashes are computed once (in parallel) instead of twice per file, and sourcemap payload preparation (serialization + compression) runs across all cores.
diff --git a/cli/src/api/symbol_sets.rs b/cli/src/api/symbol_sets.rs
index a80652d7be16..66f82fe4be68 100644
--- a/cli/src/api/symbol_sets.rs
+++ b/cli/src/api/symbol_sets.rs
@@ -1,9 +1,10 @@
use anyhow::{anyhow, Context, Result};
use rayon::{
- iter::{IntoParallelIterator, ParallelIterator},
+ iter::{IntoParallelIterator, IntoParallelRefIterator, ParallelIterator},
ThreadPool, ThreadPoolBuilder,
};
use reqwest::blocking::multipart::{Form, Part};
+use reqwest::blocking::Client;
use serde::{Deserialize, Serialize};
use std::{
collections::HashMap, fmt::Debug, iter, num::NonZeroUsize, thread::sleep, time::Duration,
@@ -103,12 +104,18 @@ pub fn upload_with_retry_and_concurrency(
concurrency: NonZeroUsize,
) -> Result<()> {
let thread_pool = build_upload_thread_pool(concurrency)?;
+ // One client for the whole run: reusing its connection pool avoids paying a
+ // TCP + TLS handshake per uploaded chunk.
+ let s3_client = context()
+ .build_http_client()
+ .context("Failed to initialize upload HTTP client")?;
let res = upload_inner(
&input_sets,
batch_size,
force,
skip_on_conflict,
&thread_pool,
+ &s3_client,
);
match res {
Ok(()) => Ok(()),
@@ -128,6 +135,7 @@ pub fn upload_with_retry_and_concurrency(
force,
skip_on_conflict,
&thread_pool,
+ &s3_client,
)
.map_err(|e| e.into())
}
@@ -148,6 +156,7 @@ fn upload_inner(
force: bool,
skip_on_conflict: bool,
thread_pool: &ThreadPool,
+ s3_client: &Client,
) -> Result<(), UploadError> {
let upload_requests: Vec<_> = input_sets
.iter()
@@ -164,9 +173,17 @@ fn upload_inner(
for (i, batch) in upload_requests.chunks(batch_size).enumerate() {
info!("Starting upload of batch {i}, {} symbol sets", batch.len());
- let start_response = start_upload(batch, force, skip_on_conflict)?;
+ // Hash each payload once, across the pool — the same hash is sent in the
+ // start request and used to confirm the upload when finishing.
+ let content_hashes: Vec<String> =
+ thread_pool.install(|| batch.par_iter().map(|u| content_hash([&u.data])).collect());
+ let start_response = start_upload(batch, &content_hashes, force, skip_on_conflict)?;
- let id_map: HashMap<_, _> = batch.iter().map(|u| (u.chunk_id.as_str(), u)).collect();
+ let id_map: HashMap<_, _> = batch
+ .iter()
+ .zip(content_hashes.iter())
+ .map(|(u, hash)| (u.chunk_id.as_str(), (u, hash)))
+ .collect();
info!(
"Server returned {} upload keys ({} skipped as already present)",
@@ -180,13 +197,12 @@ fn upload_inner(
.into_par_iter()
.map(|(chunk_id, data)| {
debug!("uploading chunk {}", chunk_id);
- let upload = id_map.get(chunk_id.as_str()).ok_or(anyhow!(
+ let (upload, content_hash) = id_map.get(chunk_id.as_str()).ok_or(anyhow!(
"Got a chunk ID back from posthog that we didn't expect!"
))?;
- let content_hash = content_hash([&upload.data]);
- upload_to_s3(data.presigned_url.clone(), &upload.data)?;
- Ok((data.symbol_set_id, content_hash))
+ upload_to_s3(s3_client, data.presigned_url.clone(), &upload.data)?;
+ Ok((data.symbol_set_id, (*content_hash).clone()))
})
.collect()
});
@@ -201,6 +217,7 @@ fn upload_inner(
fn start_upload(
symbol_sets: &[&SymbolSetUpload],
+ content_hashes: &[String],
force: bool,
skip_on_conflict: bool,
) -> Result<BulkUploadStartResponse, UploadError> {
@@ -209,7 +226,12 @@ fn start_upload(
let request = BulkUploadStartRequest {
symbol_sets: symbol_sets
.iter()
- .map(|s| CreateSymbolSetRequest::new(s))
+ .zip(content_hashes.iter())
+ .map(|(s, hash)| CreateSymbolSetRequest {
+ chunk_id: s.chunk_id.clone(),
+ release_id: s.release_id.clone(),
+ content_hash: hash.clone(),
+ })
.collect(),
force,
skip_on_conflict,
@@ -238,8 +260,7 @@ fn start_upload(
}
}
-fn upload_to_s3(presigned_url: PresignedUrl, data: &[u8]) -> Result<()> {
- let client = &context().build_http_client()?;
+fn upload_to_s3(client: &Client, presigned_url: PresignedUrl, data: &[u8]) -> Result<()> {
retry(retry_policy(500, 2, 3), |_| -> Result<()> {
let mut form = Form::new();
for (key, value) in &presigned_url.fields {
@@ -375,16 +396,6 @@ struct CreateSymbolSetRequest {
content_hash: String,
}
-impl CreateSymbolSetRequest {
- pub fn new(inner: &SymbolSetUpload) -> Self {
- Self {
- chunk_id: inner.chunk_id.clone(),
- release_id: inner.release_id.clone(),
- content_hash: content_hash([&inner.data]),
- }
- }
-}
-
fn retry_policy(duration: u64, factor: u64, max_attempts: usize) -> impl Iterator<Item = Duration> {
iter::once((duration, factor))
.cycle()
diff --git a/cli/src/sourcemaps/plain/upload.rs b/cli/src/sourcemaps/plain/upload.rs
index 5a45bf8586f4..a7ff0698e63b 100644
--- a/cli/src/sourcemaps/plain/upload.rs
+++ b/cli/src/sourcemaps/plain/upload.rs
@@ -1,6 +1,7 @@
use std::{path::PathBuf, time::Instant};
use anyhow::{Context, Result};
+use rayon::iter::{IntoParallelIterator, ParallelIterator};
use serde_json::json;
use tracing::{debug, info, warn};
@@ -129,8 +130,10 @@ pub fn upload(args: &Args, existing_release: Option<&Release>) -> Result<()> {
}
let empty_skipped = empty_pairs.len();
+ // Payload preparation (serialization + zstd compression) is CPU-bound,
+ // so spread it across cores.
let uploads = valid_pairs
- .into_iter()
+ .into_par_iter()
.map(TryInto::try_into)
.collect::<Result<Vec<SymbolSetUpload>>>()
.context("While preparing files for upload")?;