-
Notifications
You must be signed in to change notification settings - Fork 590
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
69 additions
and
8 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
internal/dataplane/sendconfig/backoff_strategy_decorator.go
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,52 @@ | ||
package sendconfig | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/kong/deck/file" | ||
|
||
"github.com/kong/kubernetes-ingress-controller/v2/internal/metrics" | ||
) | ||
|
||
type LastRetryProvider interface { | ||
LastRetry() time.Time | ||
} | ||
|
||
type UpdateStrategyBackoff struct { | ||
decorated UpdateStrategy | ||
lastRetryProvider LastRetryProvider | ||
} | ||
|
||
func NewUpdateStrategyBackoff(decorated UpdateStrategy, lastRetryProvider LastRetryProvider) UpdateStrategyBackoff { | ||
return UpdateStrategyBackoff{ | ||
decorated: decorated, | ||
lastRetryProvider: lastRetryProvider, | ||
} | ||
} | ||
|
||
func (s UpdateStrategyBackoff) Update(ctx context.Context, targetContent *file.Content) ( | ||
err error, | ||
resourceErrors []ResourceError, | ||
resourceErrorsParseErr error, | ||
) { | ||
if !s.shouldUpdate() { | ||
return ErrUpdateSkippedDueToBackoffStrategy, nil, nil | ||
} | ||
|
||
return s.decorated.Update(ctx, targetContent) | ||
} | ||
|
||
// TODO: implement this method properly | ||
// We can take into account the last returned error from Update() and decide whether to retry or not, e.g. if | ||
// - the HTTP error code was 429, we should wait the time returned in HTTP headers | ||
// - the config was invalid, we should retry only after the config changes | ||
// - ... | ||
func (s UpdateStrategyBackoff) shouldUpdate() bool { | ||
const backoffDuration = time.Minute | ||
return time.Now().After(s.lastRetryProvider.LastRetry().Add(backoffDuration)) | ||
} | ||
|
||
func (s UpdateStrategyBackoff) MetricsProtocol() metrics.Protocol { | ||
return s.decorated.MetricsProtocol() | ||
} |
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