Skip to content

Commit 82547b4

Browse files
committed
backend: calculate max total time for starting
1 parent ba086aa commit 82547b4

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

backend/pkg/api/api.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,10 @@ func setDefaultClientProviders(cfg *config.Config, logger *zap.Logger, opts *opt
173173

174174
// Start the API server and block
175175
func (api *API) Start() {
176-
startCtx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
176+
// Assume 6s for other initializations and up to max startup time before we cancel
177+
// the context and force to return early.
178+
maxStartTime := 6*time.Second + api.Cfg.Kafka.Startup.TotalMaxTime()
179+
startCtx, cancel := context.WithTimeout(context.Background(), maxStartTime)
177180
defer cancel()
178181

179182
err := api.ConsoleSvc.Start(startCtx)

backend/pkg/config/startup.go

+22
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,25 @@ func (k *ServiceStartupAttemptsOptions) Validate() error {
5454

5555
return nil
5656
}
57+
58+
// TotalMaxTime calculates the maximum duration for all retry attempts.
59+
// It includes a fixed 6-second time for each attempt and the subsequent
60+
// backoff intervals, which increase by BackoffMultiplier (capped at MaxRetryInterval).
61+
func (k *ServiceStartupAttemptsOptions) TotalMaxTime() time.Duration {
62+
// Assuming each attempt takes 6s before a failure triggers a backoff.
63+
totalTime := time.Duration(k.MaxRetries) * 6 * time.Second
64+
65+
// Calculate cumulative backoff delays for all but the final attempt.
66+
backoff := k.RetryInterval
67+
for i := 1; i < k.MaxRetries; i++ {
68+
totalTime += backoff
69+
next := time.Duration(float64(backoff) * k.BackoffMultiplier)
70+
if next > k.MaxRetryInterval {
71+
backoff = k.MaxRetryInterval
72+
} else {
73+
backoff = next
74+
}
75+
}
76+
77+
return totalTime
78+
}

0 commit comments

Comments
 (0)