Skip to content

Commit

Permalink
Refactors createRetryIntervalFunc and updateJWTSVID.
Browse files Browse the repository at this point in the history
Signed-off-by: JU4N98 <[email protected]>
  • Loading branch information
JU4N98 committed Nov 13, 2023
1 parent 4160fe9 commit f0b07c2
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions pkg/sidecar/sidecar.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,11 +286,21 @@ func (s *Sidecar) fetchJWTSVID(options ...workloadapi.ClientOption) (*jwtsvid.SV
return jwtSVID, nil
}

func getRetryInterval() func() time.Duration {
backoffInterval := 0
func createRetryIntervalFunc() func() time.Duration {
const (
initialBackoff = 1 * time.Second
maxBackoff = 60 * time.Second
multiplier = 2
)
backoffInterval := initialBackoff
return func() time.Duration {
backoffInterval += 5
return time.Duration(backoffInterval) * time.Second
currentBackoff := backoffInterval
// Update backoffInterval for next call, capped at maxBackoff
backoffInterval *= multiplier
if backoffInterval > maxBackoff {
backoffInterval = maxBackoff
}
return currentBackoff
}
}

Expand Down Expand Up @@ -319,15 +329,17 @@ func (s *Sidecar) performJWTSVIDUpdate(options ...workloadapi.ClientOption) (*jw
}

func (s *Sidecar) updateJWTSVID(ctx context.Context, options ...workloadapi.ClientOption) {
retryInterval := createRetryIntervalFunc()
var initialInterval time.Duration
jwtSVID, err := s.performJWTSVIDUpdate(options...)

retryInterval := getRetryInterval()
var ticker *time.Ticker
if err == nil {
ticker = time.NewTicker(getRefreshInterval(jwtSVID))
if err != nil {
// If the first update fails, use the retry interval
initialInterval = retryInterval()
} else {
ticker = time.NewTicker(retryInterval())
// If the update succeeds, use the refresh interval
initialInterval = getRefreshInterval(jwtSVID)
}
ticker := time.NewTicker(initialInterval)
defer ticker.Stop()

for {
Expand All @@ -337,7 +349,7 @@ func (s *Sidecar) updateJWTSVID(ctx context.Context, options ...workloadapi.Clie
case <-ticker.C:
jwtSVID, err = s.performJWTSVIDUpdate(options...)
if err == nil {
retryInterval = getRetryInterval()
retryInterval = createRetryIntervalFunc()
ticker.Reset(getRefreshInterval(jwtSVID))
} else {
ticker.Reset(retryInterval())
Expand Down

0 comments on commit f0b07c2

Please sign in to comment.