This repository has been archived by the owner on Dec 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
scheduler: adds backoff to RecoveryDescriptor
The RecoveryDescriptor calls delegate.Run continuously and forever with no backoff or max. This commit adds netutil.ExponentialBackoff and a timer that resets the retry count of delegate.Run runs for longer than the configured reset retries duration. This is useful for delegates that might crash once in a while due to transient network errors or temporary 3rd party api issues.
- Loading branch information
Showing
2 changed files
with
172 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package scheduler | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type mockErrorDescriptor struct { | ||
errCount int | ||
successAfterCount int | ||
} | ||
|
||
func (d *mockErrorDescriptor) Run(ctx context.Context, pc chan<- Process) error { | ||
if d.errCount >= 0 && d.successAfterCount != 0 { | ||
err := fmt.Errorf("err count %d", d.errCount) | ||
d.errCount-- | ||
d.successAfterCount-- | ||
|
||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func TestRecoverySchedulerStops(t *testing.T) { | ||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) | ||
|
||
descriptor := NewRecoveryDescriptor(&mockErrorDescriptor{ | ||
errCount: 10, | ||
// make sure we never succeed | ||
successAfterCount: 15, | ||
}) | ||
|
||
pc := make(chan Process) | ||
|
||
defer cancel() | ||
|
||
require.Error(t, descriptor.Run(ctx, pc)) | ||
require.Equal(t, 10, descriptor.currentRetries) | ||
} | ||
|
||
type mockRetryResetDescriptor struct { | ||
count int | ||
successDuration time.Duration | ||
cancel context.CancelFunc | ||
} | ||
|
||
func (d *mockRetryResetDescriptor) Run(ctx context.Context, pc chan<- Process) error { | ||
if d.count == 0 { | ||
<-time.After(d.successDuration) | ||
d.cancel() | ||
return nil | ||
} | ||
|
||
err := fmt.Errorf("err count %d", d.count) | ||
d.count-- | ||
|
||
return err | ||
} | ||
|
||
func TestRecoverySchedulerRetryCountReset(t *testing.T) { | ||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) | ||
|
||
successDuration := time.Second * 1 | ||
|
||
mock := &mockRetryResetDescriptor{ | ||
count: 5, | ||
cancel: cancel, | ||
successDuration: successDuration, | ||
} | ||
|
||
descriptor := NewRecoveryDescriptorWithOptions(mock, RecoveryDescriptorOptions{ | ||
ResetRetriesTimerDuration: successDuration - (time.Millisecond * 500), | ||
}) | ||
|
||
pc := make(chan Process) | ||
|
||
defer cancel() | ||
|
||
require.NoError(t, descriptor.Run(ctx, pc)) | ||
|
||
require.Equal(t, 0, descriptor.currentRetries) | ||
require.Equal(t, 0, mock.count) | ||
} |