Skip to content

Conversation

@Tuntii
Copy link
Owner

@Tuntii Tuntii commented Jan 31, 2026

This PR optimizes WebhookExporter to address severe performance degradation caused by blocking thread operations in async contexts (middleware).

Previously, WebhookExporter spawned a new thread and a new Tokio runtime for every batch of insights, blocking the caller until the HTTP request completed (or timed out).

This change:

  1. Initializes a single background thread and Runtime in WebhookExporter::new.
  2. Uses a bounded channel (size 100) to communicate between the exporter and the background worker.
  3. Updates send_insights to be fire-and-forget (non-blocking). If the channel is full, the batch is dropped and a warning is logged to prevent backpressure from blocking the application.
  4. Logs HTTP errors via tracing instead of returning them to the caller (as the caller has already moved on).

Performance Impact:

  • Baseline: export blocked for ~500ms (or timeout duration) when the webhook endpoint was slow.
  • Optimized: export takes ~50µs (microseconds), returning immediately.

This ensures that logging/telemetry does not impact the critical path of request handling.


PR created automatically by Jules for task 6390766288707008754 started by @Tuntii

- Replaced blocking `std::thread::spawn` + `recv_timeout` per request with a persistent background thread and Tokio runtime.
- Implemented `tokio::sync::mpsc::channel` (bounded: 100) to queue insights.
- `send_insights` now uses `try_send` to avoid blocking, dropping batches if the queue is full (load shedding).
- Preserves synchronous `InsightExporter` trait signature while offloading I/O.
- Added `tests/webhook_performance.rs` to verify non-blocking behavior.

Co-authored-by: Tuntii <121901995+Tuntii@users.noreply.github.com>
@google-labs-jules
Copy link
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

- Applied `cargo fmt` to `crates/rustapi-extras/src/insight/export.rs` and `crates/rustapi-extras/tests/webhook_performance.rs`.
- Added `#![cfg(feature = "webhook")]` to `crates/rustapi-extras/tests/webhook_performance.rs` to prevent compilation errors when the `webhook` feature (and thus the `insight` module) is disabled.

Co-authored-by: Tuntii <121901995+Tuntii@users.noreply.github.com>
@Tuntii Tuntii merged commit bede414 into main Jan 31, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants