-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1184 from ydb-platform/retry-quoter
added experimental `retry/budget` package
- Loading branch information
Showing
33 changed files
with
772 additions
and
164 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
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,43 @@ | ||
package backoff | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
type ( | ||
delayOptions struct { | ||
fast Backoff | ||
slow Backoff | ||
} | ||
delayOption func(o *delayOptions) | ||
) | ||
|
||
func WithFastBackoff(fast Backoff) delayOption { | ||
return func(o *delayOptions) { | ||
o.fast = fast | ||
} | ||
} | ||
|
||
func WithSlowBackoff(slow Backoff) delayOption { | ||
return func(o *delayOptions) { | ||
o.slow = slow | ||
} | ||
} | ||
|
||
func Delay(t Type, i int, opts ...delayOption) time.Duration { | ||
optionsHolder := delayOptions{ | ||
fast: Fast, | ||
slow: Slow, | ||
} | ||
for _, opt := range opts { | ||
opt(&optionsHolder) | ||
} | ||
switch t { | ||
case TypeFast: | ||
return optionsHolder.fast.Delay(i) | ||
case TypeSlow: | ||
return optionsHolder.slow.Delay(i) | ||
default: | ||
return 0 | ||
} | ||
} |
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,92 @@ | ||
package backoff | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xtest" | ||
) | ||
|
||
func TestDelay(t *testing.T) { | ||
for _, tt := range []struct { | ||
name string | ||
act time.Duration | ||
exp time.Duration | ||
}{ | ||
{ | ||
name: xtest.CurrentFileLine(), | ||
act: Delay(TypeNoBackoff, 0), | ||
exp: 0, | ||
}, | ||
{ | ||
name: xtest.CurrentFileLine(), | ||
act: Delay(TypeNoBackoff, 1), | ||
exp: 0, | ||
}, | ||
{ | ||
name: xtest.CurrentFileLine(), | ||
act: Delay(TypeNoBackoff, 2), | ||
exp: 0, | ||
}, | ||
{ | ||
name: xtest.CurrentFileLine(), | ||
act: Delay(TypeFast, 0, WithFastBackoff(New( | ||
WithSlotDuration(fastSlot), | ||
WithCeiling(6), | ||
WithJitterLimit(1), | ||
))), | ||
exp: 5 * time.Millisecond, | ||
}, | ||
{ | ||
name: xtest.CurrentFileLine(), | ||
act: Delay(TypeFast, 1, WithFastBackoff(New( | ||
WithSlotDuration(fastSlot), | ||
WithCeiling(6), | ||
WithJitterLimit(1), | ||
))), | ||
exp: 10 * time.Millisecond, | ||
}, | ||
{ | ||
name: xtest.CurrentFileLine(), | ||
act: Delay(TypeFast, 3, WithFastBackoff(New( | ||
WithSlotDuration(fastSlot), | ||
WithCeiling(6), | ||
WithJitterLimit(1), | ||
))), | ||
exp: 40 * time.Millisecond, | ||
}, | ||
{ | ||
name: xtest.CurrentFileLine(), | ||
act: Delay(TypeSlow, 0, WithSlowBackoff(New( | ||
WithSlotDuration(slowSlot), | ||
WithCeiling(6), | ||
WithJitterLimit(1), | ||
))), | ||
exp: time.Second, | ||
}, | ||
{ | ||
name: xtest.CurrentFileLine(), | ||
act: Delay(TypeSlow, 1, WithSlowBackoff(New( | ||
WithSlotDuration(slowSlot), | ||
WithCeiling(6), | ||
WithJitterLimit(1), | ||
))), | ||
exp: 2 * time.Second, | ||
}, | ||
{ | ||
name: xtest.CurrentFileLine(), | ||
act: Delay(TypeSlow, 3, WithSlowBackoff(New( | ||
WithSlotDuration(slowSlot), | ||
WithCeiling(6), | ||
WithJitterLimit(1), | ||
))), | ||
exp: 8 * time.Second, | ||
}, | ||
} { | ||
t.Run(tt.name, func(t *testing.T) { | ||
require.Equal(t, tt.exp, tt.act) | ||
}) | ||
} | ||
} |
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
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
Oops, something went wrong.