Skip to content

Commit fbc8a67

Browse files
committed
Improves retry mechanism for POST requests to Executor
1 parent 7299f30 commit fbc8a67

1 file changed

Lines changed: 18 additions & 7 deletions

File tree

internal/container/container.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,29 +73,40 @@ func Destroy(id ContainerID) error {
7373
}
7474

7575
func sendPostRequestWithRetries(url string, body *bytes.Buffer) (*http.Response, time.Duration, error) {
76-
const maxRetries = 15
76+
const TIMEOUT_MILLIS = 30000
77+
const MAX_BACKOFF_MILLIS = 500
7778
var backoffMillis = 25
7879
var totalWaitMillis = 0
80+
var attempts = 1
7981

8082
var err error
8183

82-
for retry := 1; retry <= maxRetries; retry++ {
84+
for totalWaitMillis < TIMEOUT_MILLIS {
8385
resp, err := http.Post(url, "application/json", body)
8486
if err == nil {
8587
return resp, time.Duration(totalWaitMillis * int(time.Millisecond)), err
86-
} else if retry > 1 {
88+
} else if attempts > 3 {
8789
// It is common to have a failure after a cold start, so
88-
// we avoid logging failures on the first attempt
89-
log.Printf("Invocation POST failed (attempt %d/%d): %v", retry, maxRetries, err)
90+
// we avoid logging failures on the first attempt(s)
91+
log.Printf("Invocation POST failed (attempt %d): %v", attempts, err)
9092
}
9193

9294
time.Sleep(time.Duration(backoffMillis * int(time.Millisecond)))
9395
totalWaitMillis += backoffMillis
96+
attempts += 1
9497

95-
if backoffMillis <= 200 {
96-
backoffMillis *= 2
98+
if backoffMillis < MAX_BACKOFF_MILLIS {
99+
backoffMillis = min(backoffMillis*2, MAX_BACKOFF_MILLIS)
97100
}
98101
}
99102

100103
return nil, time.Duration(totalWaitMillis * int(time.Millisecond)), err
101104
}
105+
106+
func min(a, b int) int {
107+
if a <= b {
108+
return a
109+
} else {
110+
return b
111+
}
112+
}

0 commit comments

Comments
 (0)