Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* * first commit * * 'continue' fix * * fixed ticker's interval adjustment * * rename method * * rename method * * PR review fixes * * PR review fixes * * PR review fixes * * PR review fixes * * PR review fixes * * PR review fixes * * PR review fixes * * use backoff v4.1.2 * * use backoff v4.1.3 with specific commit id to include Nir R changes * * use backoff v4.1.3 with specific commit id to include Nir R changes
- Loading branch information
1 parent
d915ca9
commit f7c4315
Showing
9 changed files
with
155 additions
and
25 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
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
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,63 @@ | ||
package intervalpolicy | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/cenkalti/backoff/v4" | ||
) | ||
|
||
const ( | ||
baseFactor = 2 | ||
maxInterval = 60 * time.Second | ||
maxNumOfConsecutiveEvaluationsBeforeNextBackoff = 3 | ||
) | ||
|
||
// exponentialBackoffIntervalPolicy is a default interval policy. | ||
type exponentialBackoffIntervalPolicy struct { | ||
exponentialBackoff *backoff.ExponentialBackOff | ||
interval time.Duration | ||
numOfConsecutiveEvaluations int | ||
} | ||
|
||
// NewExponentialBackoffIntervalPolicy creates new exponential backoff interval policy. | ||
func NewExponentialBackoffIntervalPolicy(interval time.Duration) IntervalPolicy { | ||
exponentialBackoff := &backoff.ExponentialBackOff{ | ||
InitialInterval: interval, | ||
RandomizationFactor: 0, | ||
Multiplier: baseFactor, | ||
MaxInterval: maxInterval, | ||
MaxElapsedTime: 0, | ||
Stop: 0, | ||
Clock: backoff.SystemClock, | ||
} | ||
|
||
exponentialBackoff.Reset() | ||
|
||
return &exponentialBackoffIntervalPolicy{ | ||
exponentialBackoff: exponentialBackoff, | ||
interval: interval, | ||
numOfConsecutiveEvaluations: 0, | ||
} | ||
} | ||
|
||
// Evaluate reevaluates interval. | ||
func (policy *exponentialBackoffIntervalPolicy) Evaluate() { | ||
policy.numOfConsecutiveEvaluations++ | ||
|
||
if policy.numOfConsecutiveEvaluations == maxNumOfConsecutiveEvaluationsBeforeNextBackoff { | ||
policy.interval = policy.exponentialBackoff.NextBackOff() | ||
policy.numOfConsecutiveEvaluations = 0 | ||
} | ||
} | ||
|
||
// Reset resets the entire state of the policy. | ||
func (policy *exponentialBackoffIntervalPolicy) Reset() { | ||
policy.exponentialBackoff.Reset() | ||
policy.interval = policy.exponentialBackoff.InitialInterval | ||
policy.numOfConsecutiveEvaluations = 0 | ||
} | ||
|
||
// GetInterval returns reevaluated interval. | ||
func (policy *exponentialBackoffIntervalPolicy) GetInterval() time.Duration { | ||
return policy.interval | ||
} |
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,13 @@ | ||
package intervalpolicy | ||
|
||
import "time" | ||
|
||
// IntervalPolicy defines a policy to return interval based on the received events. | ||
type IntervalPolicy interface { | ||
// Evaluate evaluates next interval. | ||
Evaluate() | ||
// Reset resets the interval of the interval policy. | ||
Reset() | ||
// GetInterval returns current interval. | ||
GetInterval() time.Duration | ||
} |