Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

perf(agent): exponential backoff in polling to prevent forking storm (#41222)

Yunlu Wen679514d40a3f
diff --git a/dify-agent-runtime/internal/server/config.go b/dify-agent-runtime/internal/server/config.go
index c30ed34df70..bae1c56d93f 100644
--- a/dify-agent-runtime/internal/server/config.go
+++ b/dify-agent-runtime/internal/server/config.go
@@ -24,6 +24,7 @@ const (
DefaultGCIntervalSeconds = 60.0
DefaultGCFinishedJobRetentionSeconds = 300.0
DefaultPollInterval = 5 * time.Millisecond
+ DefaultMaxPollInterval = 50 * time.Millisecond
DefaultPipeMonitorInterval = 1 * time.Second
DefaultPipeReadyTimeout = 10 * time.Second
DefaultSQLiteBusyTimeoutMs = 5000
@@ -53,6 +54,7 @@ type Config struct {
MaxOutputLimitBytes int
DefaultTerminateGraceSeconds float64
PollInterval time.Duration
+ MaxPollInterval time.Duration
PipeMonitorInterval time.Duration
PipeReadyTimeout time.Duration
SQLiteBusyTimeoutMs int
@@ -90,6 +92,7 @@ func DefaultConfig() (*Config, error) {
MaxOutputLimitBytes: MaxOutputLimitBytes,
DefaultTerminateGraceSeconds: DefaultTerminateGraceSeconds,
PollInterval: DefaultPollInterval,
+ MaxPollInterval: DefaultMaxPollInterval,
PipeMonitorInterval: DefaultPipeMonitorInterval,
PipeReadyTimeout: DefaultPipeReadyTimeout,
SQLiteBusyTimeoutMs: DefaultSQLiteBusyTimeoutMs,
diff --git a/dify-agent-runtime/internal/server/service.go b/dify-agent-runtime/internal/server/service.go
index 693d3c16fcc..24a21f5ee54 100644
--- a/dify-agent-runtime/internal/server/service.go
+++ b/dify-agent-runtime/internal/server/service.go
@@ -349,6 +349,11 @@ func (s *Service) WaitJob(jobID string, req *WaitJobRequest) (*JobResult, error)
lastGrowthAt = &now
}
+ // Poll with exponential backoff: start tight so short jobs return with low
+ // latency, then grow the interval up to MaxPollInterval so long/idle waits
+ // stop spawning a tmux liveness probe (fork) every few milliseconds.
+ pollInterval := s.config.PollInterval
+
for {
view, err := s.GetJobStatus(jobID)
if err != nil {
@@ -411,7 +416,8 @@ func (s *Service) WaitJob(jobID string, req *WaitJobRequest) (*JobResult, error)
return s.jobResultFromView(view, row, window), nil
}
- time.Sleep(s.config.PollInterval)
+ time.Sleep(pollInterval)
+ pollInterval = min(pollInterval*2, s.config.MaxPollInterval)
}
}