Skip to content

Commit dc70d62

Browse files
authored
Add config for waiting for multiple successful cycles until rendering (#880)
* don't proceed until we've been at 0 queries for several cycles in a row * rename var
1 parent c947ecb commit dc70d62

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

pkg/config/config.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,8 @@ type BrowserConfig struct {
322322
// ReadinessTimeout is the maximum time to wait for the web-page to become ready (i.e. no longer loading anything).
323323
ReadinessTimeout time.Duration
324324
ReadinessIterationInterval time.Duration
325+
// ReadinessWaitForNQueryCycles is the number of readiness checks that must pass consecutively before considering the page ready. This handles the case where queries drop to 0 briefly before incrementing again.
326+
ReadinessWaitForNQueryCycles int
325327
// ReadinessPriorWait is the time to wait before checking for how ready the page is.
326328
// This lets you force the webpage to take a beat and just do its thing before the service starts looking for whether it's time to render anything.
327329
ReadinessPriorWait time.Duration
@@ -447,6 +449,17 @@ func BrowserFlags() []cli.Flag {
447449
return nil
448450
},
449451
},
452+
&cli.IntFlag{
453+
Name: "browser.readiness.wait-for-n-query-cycles",
454+
Usage: "The number of readiness checks that must pass consecutively before considering the page ready.",
455+
Value: 1,
456+
Validator: func(i int) error {
457+
if i < 1 {
458+
return fmt.Errorf("browser readiness wait-for-n-query-cycles must be at least 1 (got %d)", i)
459+
}
460+
return nil
461+
},
462+
},
450463
&cli.DurationFlag{
451464
Name: "browser.readiness.prior-wait",
452465
Usage: "The time to wait before checking for how ready the page is. This lets you force the webpage to take a beat and just do its thing before the service starts looking for whether it's time to render anything. If <= 0, this is disabled.",
@@ -596,6 +609,7 @@ func BrowserConfigFromCommand(c *cli.Command) (BrowserConfig, error) {
596609
TimeBetweenScrolls: c.Duration("browser.time-between-scrolls"),
597610
ReadinessTimeout: c.Duration("browser.readiness.timeout"),
598611
ReadinessIterationInterval: c.Duration("browser.readiness.iteration-interval"),
612+
ReadinessWaitForNQueryCycles: c.Int("browser.readiness.wait-for-n-query-cycles"),
599613
ReadinessPriorWait: c.Duration("browser.readiness.prior-wait"),
600614
ReadinessDisableQueryWait: c.Bool("browser.readiness.disable-query-wait"),
601615
ReadinessFirstQueryTimeout: c.Duration("browser.readiness.give-up-on-first-query"),

pkg/service/browser.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -994,6 +994,7 @@ func waitForReady(browserCtx context.Context, cfg config.BrowserConfig) chromedp
994994
}
995995

996996
hasSeenAnyQuery := false
997+
numSuccessfulCycles := 0
997998

998999
var domHashCode int
9991000
initialDOMPass := true
@@ -1034,6 +1035,10 @@ func waitForReady(browserCtx context.Context, cfg config.BrowserConfig) chromedp
10341035
} else if !hasSeenAnyQuery && (cfg.ReadinessFirstQueryTimeout <= 0 || time.Since(start) < cfg.ReadinessFirstQueryTimeout) {
10351036
span.AddEvent("no first query detected yet; giving it more time")
10361037
continue
1038+
} else if numSuccessfulCycles+1 < cfg.ReadinessWaitForNQueryCycles {
1039+
numSuccessfulCycles++
1040+
span.AddEvent("waiting for more successful readiness cycles", trace.WithAttributes(attribute.Int("currentCycle", numSuccessfulCycles), attribute.Int("requiredCycles", cfg.ReadinessWaitForNQueryCycles)))
1041+
continue // need more successful cycles
10371042
}
10381043
}
10391044

0 commit comments

Comments
 (0)