Skip to content

Commit 8efc971

Browse files
changes as per comments, add tests
make base delay constant 1 second, decrement retryAttempt in notificationListenerLoop, hardcode sleep duration in shutdown, slight change in logic - cap delays to max of 120 seconds, but allow for jitter in that too, add tests according to calculations Signed-off-by: pranshu-raj-211 <[email protected]>
1 parent 9324475 commit 8efc971

File tree

2 files changed

+61
-9
lines changed

2 files changed

+61
-9
lines changed

dbos/system_database.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,10 @@ const (
136136
_DBOS_WORKFLOW_EVENTS_CHANNEL = "dbos_workflow_events_channel"
137137

138138
// Database retry timeouts
139-
_DB_CONNECTION_RETRY_BASE_DELAY = 500 * time.Millisecond
139+
_DB_CONNECTION_RETRY_BASE_DELAY = 1 * time.Second
140140
_DB_CONNECTION_RETRY_FACTOR = 2
141-
_DB_CONNECTION_RETRY_MAX_RETRIES = 3
142-
_DB_CONNECTION_MAX_DELAY = 10000 * time.Millisecond
141+
_DB_CONNECTION_RETRY_MAX_RETRIES = 10
142+
_DB_CONNECTION_MAX_DELAY = 120 * time.Second
143143
_DB_RETRY_INTERVAL = 1 * time.Second
144144
)
145145

@@ -293,7 +293,7 @@ func (s *sysDB) shutdown(ctx context.Context, timeout time.Duration) {
293293
// Allow pgx health checks to complete
294294
// https://github.com/jackc/pgx/blob/15bca4a4e14e0049777c1245dba4c16300fe4fd0/pgxpool/pool.go#L417
295295
// These trigger go-leak alerts
296-
time.Sleep(backoffWithJitter(0))
296+
time.Sleep(500 * time.Second)
297297

298298
s.launched = false
299299
}
@@ -1532,6 +1532,10 @@ func (s *sysDB) notificationListenerLoop(ctx context.Context) {
15321532
time.Sleep(backoffWithJitter(retryAttempt))
15331533
retryAttempt += 1
15341534
continue
1535+
} else {
1536+
if retryAttempt > 0 {
1537+
retryAttempt -= 1
1538+
}
15351539
}
15361540
}
15371541
}
@@ -2337,14 +2341,11 @@ func (qb *queryBuilder) addWhereLessEqual(column string, value any) {
23372341
}
23382342

23392343
func backoffWithJitter(retryAttempt int) time.Duration {
2344+
exp := float64(_DB_CONNECTION_RETRY_BASE_DELAY) * math.Pow(_DB_CONNECTION_RETRY_FACTOR, float64(retryAttempt))
23402345
// cap backoff to max number of retries, then do a fixed time delay
23412346
// expected retryAttempt to initially be 0, so >= used
2342-
if retryAttempt >= _DB_CONNECTION_RETRY_MAX_RETRIES {
2343-
return _DB_CONNECTION_RETRY_BASE_DELAY
2344-
}
2345-
exp := float64(_DB_CONNECTION_RETRY_BASE_DELAY) * math.Pow(_DB_CONNECTION_RETRY_FACTOR, float64(retryAttempt))
23462347
// cap delay to maximum of _DB_CONNECTION_MAX_DELAY milliseconds
2347-
if exp > float64(_DB_CONNECTION_MAX_DELAY) {
2348+
if retryAttempt >= _DB_CONNECTION_RETRY_MAX_RETRIES || exp > float64(_DB_CONNECTION_MAX_DELAY) {
23482349
exp = float64(_DB_CONNECTION_MAX_DELAY)
23492350
}
23502351

dbos/system_database_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package dbos
2+
3+
import (
4+
"testing"
5+
"time"
6+
)
7+
8+
var backoffWithJitterTestcases = []struct {
9+
name string
10+
retryAttempt int
11+
wantMin time.Duration
12+
wantMax time.Duration
13+
}{
14+
{
15+
name: "first retry attempt (0)",
16+
retryAttempt: 0,
17+
wantMin: 750 * time.Millisecond,
18+
wantMax: 1250 * time.Millisecond,
19+
},
20+
{
21+
name: "second retry attempt (1)",
22+
retryAttempt: 1,
23+
wantMin: 1500 * time.Millisecond,
24+
wantMax: 2500 * time.Millisecond,
25+
},
26+
{
27+
name: "ninth retry attempt (8)",
28+
retryAttempt: 8,
29+
wantMin: 90 * time.Second,
30+
wantMax: 150 * time.Second,
31+
},
32+
{
33+
name: "exceeds max retries",
34+
retryAttempt: 10,
35+
wantMin: 90 * time.Second,
36+
wantMax: 150 * time.Second,
37+
},
38+
}
39+
40+
func TestBackoffWithJitter(t *testing.T) {
41+
for _, testcase := range backoffWithJitterTestcases {
42+
t.Run(testcase.name, func(t *testing.T) {
43+
got := backoffWithJitter(testcase.retryAttempt)
44+
45+
if got < testcase.wantMin || got > testcase.wantMax {
46+
t.Errorf("Should be between %v and %v, got=%v, attempt=%v",
47+
testcase.wantMin, testcase.wantMax, got, testcase.retryAttempt)
48+
}
49+
})
50+
}
51+
}

0 commit comments

Comments
 (0)