Skip to content

Commit 2489714

Browse files
committed
feat(aws-connection): Simplifies retry logic
1 parent 278f7c1 commit 2489714

File tree

2 files changed

+12
-22
lines changed

2 files changed

+12
-22
lines changed

dynatrace/api/builtin/hyperscalerauthentication/connections/aws/role_arn/service.go

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,11 @@ func (me *service) Create(ctx context.Context, v *role_arn.Settings) (*api.Stub,
8585
connValue.AWSWebIdentity.RoleARN = v.RoleARN
8686
}
8787

88-
ctxRetry, cancel, retryTimeout, err := computeRetryContext(ctx, timeoutDeadlineBuffer, role_arn.DefaultCreateTimeout)
88+
retryTimeout, err := computeRetryTimeout(ctx, timeoutDeadlineBuffer, role_arn.DefaultCreateTimeout)
8989
if err != nil {
9090
return nil, err
9191
}
92+
ctxRetry, cancel := context.WithTimeout(ctx, retryTimeout)
9293
defer cancel()
9394

9495
if err = retry.RetryContext(ctxRetry, retryTimeout, func() *retry.RetryError {
@@ -100,29 +101,20 @@ func (me *service) Create(ctx context.Context, v *role_arn.Settings) (*api.Stub,
100101
return &api.Stub{ID: v.AWSConnectionID, Name: v.AWSConnectionID}, nil
101102
}
102103

103-
// computeRetryContext computes a safe retry timeout based on the incoming ctx deadline.
104+
// computeRetryTimeout computes a safe retry timeout based on the incoming ctx deadline.
104105
// - timeoutDeadlineBuffer: amount of time to reserve for finalization (e.g. 1 minute).
105106
// - defaultTimeout: fallback when caller didn't provide a deadline.
106-
// Returns the derived ctx (with timeout), its cancel func, the retryTimeout, or an error
107-
// if the caller's deadline already expired.
108-
func computeRetryContext(ctx context.Context, timeoutDeadlineBuffer time.Duration, defaultTimeout time.Duration) (context.Context, context.CancelFunc, time.Duration, error) {
107+
// Returns the derived timeout, or an error if the caller's deadline already expired (taking the buffer into account as well).
108+
func computeRetryTimeout(ctx context.Context, timeoutDeadlineBuffer time.Duration, defaultTimeout time.Duration) (time.Duration, error) {
109109
if dl, ok := ctx.Deadline(); ok {
110-
remaining := time.Until(dl)
110+
remaining := time.Until(dl) - timeoutDeadlineBuffer
111111
if remaining <= 0 {
112-
return nil, nil, 0, context.DeadlineExceeded
112+
return 0, context.DeadlineExceeded
113113
}
114-
var retryTimeout time.Duration
115-
if remaining > timeoutDeadlineBuffer {
116-
retryTimeout = remaining - timeoutDeadlineBuffer
117-
} else {
118-
retryTimeout = remaining
119-
}
120-
ctxRetry, cancel := context.WithTimeout(ctx, retryTimeout)
121-
return ctxRetry, cancel, retryTimeout, nil
114+
return remaining, nil
122115
}
123116
// no deadline: use conservative default
124-
ctxRetry, cancel := context.WithTimeout(ctx, defaultTimeout)
125-
return ctxRetry, cancel, defaultTimeout, nil
117+
return defaultTimeout, nil
126118
}
127119

128120
// classifyRetryError encapsulates which errors should be retried.

dynatrace/api/builtin/hyperscalerauthentication/connections/aws/role_arn/service_test.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ func approx(d1, d2 time.Duration) bool {
2020

2121
func TestComputeRetryContext_NoDeadline(t *testing.T) {
2222
ctx := context.Background()
23-
_, cancel, retryTimeout, _ := computeRetryContext(ctx, time.Minute, 2*time.Minute)
24-
defer cancel()
23+
retryTimeout, _ := computeRetryTimeout(ctx, time.Minute, 2*time.Minute)
2524

2625
assert.Equal(t, 2*time.Minute, retryTimeout)
2726
}
@@ -33,8 +32,7 @@ func TestComputeRetryContext_WithDeadline_Plenty(t *testing.T) {
3332
ctxWithDL, cancelParent := context.WithDeadline(parent, deadline)
3433
defer cancelParent()
3534

36-
_, cancel, retryTimeout, _ := computeRetryContext(ctxWithDL, time.Minute, 2*time.Minute)
37-
defer cancel()
35+
retryTimeout, _ := computeRetryTimeout(ctxWithDL, time.Minute, 2*time.Minute)
3836

3937
// expect ~4 minutes
4038
expected := 4 * time.Minute
@@ -45,7 +43,7 @@ func TestComputeRetryContext_ExpiredDeadline(t *testing.T) {
4543
ctxWithExpiredDL, cancel := context.WithDeadline(context.Background(), time.Now().Add(-time.Second))
4644
defer cancel()
4745

48-
_, _, _, err := computeRetryContext(ctxWithExpiredDL, time.Minute, 2*time.Minute)
46+
_, err := computeRetryTimeout(ctxWithExpiredDL, time.Minute, 2*time.Minute)
4947

5048
assert.ErrorIs(t, err, context.DeadlineExceeded)
5149
}

0 commit comments

Comments
 (0)