fix(chromium): resource intensive health check

This commit is contained in:
Julien Neuhart
2025-12-02 12:40:22 +00:00
parent 2f7218b09c
commit 8b9858aa66
3 changed files with 20 additions and 12 deletions

View File

@@ -10,6 +10,7 @@ import (
"sync/atomic"
"time"
cdprotobrowser "github.com/chromedp/cdproto/browser"
"github.com/chromedp/cdproto/fetch"
"github.com/chromedp/cdproto/network"
"github.com/chromedp/cdproto/page"
@@ -250,13 +251,20 @@ func (b *chromiumBrowser) Healthy(logger *zap.Logger) bool {
b.ctxMu.RLock()
defer b.ctxMu.RUnlock()
timeoutCtx, timeoutCancel := context.WithTimeout(b.ctx, time.Duration(10)*time.Second)
defer timeoutCancel()
// Create a timeout based on the existing browser context (b.ctx).
// IMPORTANT: We do NOT call chromedp.NewContext here.
// We want to execute this against the main browser connection,
// avoiding the creation of a new target (tab).
ctx, cancel := context.WithTimeout(b.ctx, 5*time.Second)
defer cancel()
taskCtx, taskCancel := chromedp.NewContext(timeoutCtx)
defer taskCancel()
err := chromedp.Run(taskCtx, chromedp.Navigate("about:blank"))
// Check if the browser is responsive by asking for its version.
// This involves a simple JSON payload roundtrip over the websocket.
// See https://github.com/gotenberg/gotenberg/issues/1169.
err := chromedp.Run(ctx, chromedp.ActionFunc(func(ctx context.Context) error {
_, _, _, _, _, err := cdprotobrowser.GetVersion().Do(ctx)
return err
}))
if err != nil {
logger.Error(fmt.Sprintf("browser health check failed: %s", err))
return false