Skip to content

High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.

fix(scheduler): bound webhook goroutines and drain HTTP response body

Yash Jadhav8ed4c5716e7d
diff --git a/internal/scheduler/scheduler.go b/internal/scheduler/scheduler.go
index af2de3c7..82fbea7a 100644
--- a/internal/scheduler/scheduler.go
+++ b/internal/scheduler/scheduler.go
@@ -58,6 +58,9 @@ type Scheduler struct {
live map[string]*Task
liveMu sync.RWMutex
+ // webhookSem bounds the number of concurrent webhook delivery goroutines.
+ webhookSem chan struct{}
+
// cancellation
cancels map[string]context.CancelFunc
cancelsMu sync.Mutex
@@ -89,15 +92,16 @@ func New(cfg Config, resolver InstanceResolver) *Scheduler {
}
return &Scheduler{
- cfg: cfg,
- queue: NewTaskQueue(cfg.MaxQueueSize, cfg.MaxPerAgent),
- results: NewResultStore(cfg.ResultTTL),
- resolver: resolver,
- client: &http.Client{Timeout: 60 * time.Second},
- metrics: newMetrics(),
- live: make(map[string]*Task),
- cancels: make(map[string]context.CancelFunc),
- stopCh: make(chan struct{}),
+ cfg: cfg,
+ queue: NewTaskQueue(cfg.MaxQueueSize, cfg.MaxPerAgent),
+ results: NewResultStore(cfg.ResultTTL),
+ resolver: resolver,
+ client: &http.Client{Timeout: 60 * time.Second},
+ metrics: newMetrics(),
+ live: make(map[string]*Task),
+ cancels: make(map[string]context.CancelFunc),
+ stopCh: make(chan struct{}),
+ webhookSem: make(chan struct{}, 16),
}
}
@@ -420,7 +424,15 @@ func (s *Scheduler) finishTask(t *Task) {
// Fire webhook asynchronously if configured.
if t.CallbackURL != "" && t.GetState().IsTerminal() {
- go sendWebhook(t.CallbackURL, t)
+ select {
+ case s.webhookSem <- struct{}{}:
+ go func() {
+ defer func() { <-s.webhookSem }()
+ sendWebhook(t.CallbackURL, t)
+ }()
+ default:
+ slog.Warn("webhook: too many in-flight deliveries, dropping", "task", t.ID)
+ }
}
}
diff --git a/internal/scheduler/webhook.go b/internal/scheduler/webhook.go
index aa2b2fab..cee0148b 100644
--- a/internal/scheduler/webhook.go
+++ b/internal/scheduler/webhook.go
@@ -3,6 +3,7 @@ package scheduler
import (
"bytes"
"encoding/json"
+ "io"
"log/slog"
"net/http"
"net/url"
@@ -53,6 +54,8 @@ func sendWebhook(callbackURL string, t *Task) {
slog.Warn("webhook: delivery failed", "task", t.ID, "url", callbackURL, "err", err)
return
}
+ // Drain body so the underlying connection can be reused.
+ _, _ = io.Copy(io.Discard, resp.Body)
_ = resp.Body.Close()
if resp.StatusCode >= 400 {