-
Notifications
You must be signed in to change notification settings - Fork 555
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NET-1833: add retries to license key validation. (#3222)
* feat(go): add retries to license key validation. * feat(go): increase the number of retries.
- Loading branch information
1 parent
5f21c8b
commit 496d541
Showing
2 changed files
with
114 additions
and
46 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,41 @@ | ||
package utils | ||
|
||
import "time" | ||
|
||
// RetryStrategy specifies a strategy to retry an operation after waiting a while, | ||
// with hooks for successful and unsuccessful (>=max) tries. | ||
type RetryStrategy struct { | ||
Wait func(time.Duration) | ||
WaitTime time.Duration | ||
WaitTimeIncrease time.Duration | ||
MaxTries int | ||
Try func() error | ||
OnMaxTries func() | ||
OnSuccess func() | ||
} | ||
|
||
// DoStrategy does the retry strategy specified in the struct, waiting before retrying an operator, | ||
// up to a max number of tries, and if executes a success "finalizer" operation if a retry is successful | ||
func (rs RetryStrategy) DoStrategy() { | ||
err := rs.Try() | ||
if err == nil { | ||
rs.OnSuccess() | ||
return | ||
} | ||
|
||
tries := 1 | ||
for { | ||
if tries >= rs.MaxTries { | ||
rs.OnMaxTries() | ||
return | ||
} | ||
rs.Wait(rs.WaitTime) | ||
if err := rs.Try(); err != nil { | ||
tries++ // we tried, increase count | ||
rs.WaitTime += rs.WaitTimeIncrease // for the next time, sleep more | ||
continue // retry | ||
} | ||
rs.OnSuccess() | ||
return | ||
} | ||
} |