-
Notifications
You must be signed in to change notification settings - Fork 526
[SPIRE Agent] Add size based backoff strategy for fetchSVIDs #4279
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8cc13c7
[SPIRE Agent] Add size based backoff strategy for fetchSVIDs
prasadborole1 a59a722
Add comments and constant for max bakcoff
prasadborole1 5e819b4
Move backoff to fetchSVIDs method
prasadborole1 6bf1a99
Update README
prasadborole1 26b7782
Merge branch 'main' into agent-backoff
7e2b565
Update unit test
prasadborole1 1d1a23a
Merge branch 'main' into agent-backoff
rturner3 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,62 @@ | ||
package backoff | ||
|
||
// SizeLimitedBackOff defines interface for implementing a size based backoff for requests which | ||
// contain number of records to be processed by server. | ||
type SizeLimitedBackOff interface { | ||
// NextBackOff returns the duration to wait before retrying the operation, | ||
// or backoff. | ||
NextBackOff() int | ||
|
||
// Success indicates the backoff implementation that previous request succeeded | ||
// so that it can adjust backoff accordingly for next request. | ||
Success() | ||
|
||
// Failure indicates the backoff implementation that previous request failed | ||
// so that it can adjust backoff accordingly for next request. | ||
Failure() | ||
|
||
// Reset to initial state. | ||
Reset() | ||
} | ||
|
||
type sizeLimitedBackOff struct { | ||
currentSize int | ||
maxSize int | ||
} | ||
|
||
var _ SizeLimitedBackOff = (*sizeLimitedBackOff)(nil) | ||
|
||
func (r *sizeLimitedBackOff) NextBackOff() int { | ||
return r.currentSize | ||
} | ||
|
||
func (r *sizeLimitedBackOff) Success() { | ||
newSize := r.currentSize * 2 | ||
if newSize > r.maxSize { | ||
newSize = r.maxSize | ||
} | ||
r.currentSize = newSize | ||
} | ||
|
||
func (r *sizeLimitedBackOff) Failure() { | ||
newSize := r.currentSize / 2 | ||
if newSize < 1 { | ||
newSize = 1 | ||
} | ||
r.currentSize = newSize | ||
} | ||
|
||
func (r *sizeLimitedBackOff) Reset() { | ||
r.currentSize = r.maxSize | ||
} | ||
|
||
// NewSizeLimitedBackOff returns a new SizeLimitedBackOff with provided maxRequestSize and lowest request size of 1. | ||
// On Failure the size gets reduced by half and on Success size gets doubled | ||
func NewSizeLimitedBackOff(maxRequestSize int) SizeLimitedBackOff { | ||
prasadborole1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
b := &sizeLimitedBackOff{ | ||
maxSize: maxRequestSize, | ||
} | ||
b.Reset() | ||
|
||
return b | ||
} |
This file contains hidden or 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,54 @@ | ||
package backoff | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestRequestSizeBackOff(t *testing.T) { | ||
prasadborole1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
maxRequestSize := 1000 | ||
b := NewSizeLimitedBackOff(maxRequestSize) | ||
|
||
// Initial backoff value should be equal to the maxRequestSize | ||
assert.Equal(t, maxRequestSize, b.NextBackOff()) | ||
|
||
// After multiple successes, the backoff value should cap at maxRequestSize | ||
b.Success() | ||
prasadborole1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert.Equal(t, maxRequestSize, b.NextBackOff()) | ||
b.Success() | ||
assert.Equal(t, maxRequestSize, b.NextBackOff()) | ||
|
||
// After a failure, the backoff value should be halved | ||
b.Failure() | ||
assert.Equal(t, maxRequestSize/2, b.NextBackOff()) | ||
|
||
// After multiple failures, the backoff value should keep halving | ||
b.Failure() | ||
b.Failure() | ||
assert.Equal(t, maxRequestSize/8, b.NextBackOff()) | ||
|
||
// After success backoff value should keep doubling | ||
b.Success() | ||
assert.Equal(t, maxRequestSize/4, b.NextBackOff()) | ||
b.Success() | ||
assert.Equal(t, maxRequestSize/2, b.NextBackOff()) | ||
|
||
// Reset should set the backoff value back to the initial maxRequestSize | ||
b.Reset() | ||
assert.Equal(t, maxRequestSize, b.NextBackOff()) | ||
|
||
// validate lower limit | ||
maxRequestSize = 5 | ||
b = NewSizeLimitedBackOff(maxRequestSize) | ||
assert.Equal(t, maxRequestSize, b.NextBackOff()) | ||
|
||
b.Failure() | ||
assert.Equal(t, 2, b.NextBackOff()) | ||
b.Failure() | ||
assert.Equal(t, 1, b.NextBackOff()) | ||
|
||
// backoff value should not go below 1 | ||
b.Failure() | ||
assert.Equal(t, 1, b.NextBackOff()) | ||
} |
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.