forked from longtao.wu/droboticflow
60 lines
1.7 KiB
Go
60 lines
1.7 KiB
Go
package main
|
|
|
|
import (
|
|
"droboticflow/internal/store"
|
|
"time"
|
|
)
|
|
|
|
// The default preserves immediate periodic backlog draining. A production
|
|
// backfill can explicitly pace every pass without changing query timeouts,
|
|
// transaction durability, or the projector's renewable lease.
|
|
func validPacing(batch int, minimum time.Duration) bool {
|
|
return batch >= 1 && batch <= 200 && minimum >= 0 && minimum <= time.Minute
|
|
}
|
|
|
|
func passDelay(mode string, ready bool, elapsed, interval, minimum time.Duration) time.Duration {
|
|
var delay time.Duration
|
|
if ready || mode != "periodic" {
|
|
delay = interval
|
|
}
|
|
if remaining := minimum - elapsed; remaining > delay {
|
|
delay = remaining
|
|
}
|
|
// WaitNextPass has a 100ms lower bound and preserves lease heartbeats.
|
|
if delay > 0 && delay < 100*time.Millisecond {
|
|
return 100 * time.Millisecond
|
|
}
|
|
return delay
|
|
}
|
|
|
|
func daemonRetryDelay(attempt int) time.Duration {
|
|
if attempt <= 0 {
|
|
return 2 * time.Second
|
|
}
|
|
if attempt > 10 {
|
|
attempt = 10
|
|
}
|
|
delay := 2 * time.Second << attempt
|
|
if delay > 15*time.Second || delay <= 0 {
|
|
return 15 * time.Second
|
|
}
|
|
return delay
|
|
}
|
|
|
|
func adaptiveBackfillCooldown(ready bool, elapsed time.Duration) time.Duration {
|
|
if !ready && elapsed >= 2*time.Second {
|
|
return 500 * time.Millisecond
|
|
}
|
|
return 0
|
|
}
|
|
|
|
// Retrying transport reads is safe; permission, lease, unsupported data and
|
|
// persistence failures must remain visible to the operator/health observer.
|
|
func daemonRetryable(err error) bool {
|
|
d := store.QueryReplicaDiagnostic(err)
|
|
if d.Phase == "target_apply" || d.Phase == "checkpoint" || d.Class == "mongo_write" || d.Class == "mongo_bulk_write" {
|
|
return false
|
|
}
|
|
return d.Class == "network" || d.Class == "timeout" || d.Class == "deadline"
|
|
}
|