Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
runtime/jitter: allow overwrite of defaults
Browse files Browse the repository at this point in the history
Signed-off-by: Hidde Beydals <hidde@hhh.computer>
hiddeco committed Aug 1, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 524858d commit 683036a
Showing 2 changed files with 50 additions and 6 deletions.
19 changes: 16 additions & 3 deletions runtime/jitter/interval.go
Original file line number Diff line number Diff line change
@@ -27,7 +27,8 @@ import (
)

const (
flagIntervalJitter = "interval-jitter-percent"
flagIntervalJitter = "interval-jitter-percentage"
defaultIntervalJitterPercentage = 10
)

var (
@@ -101,9 +102,21 @@ type Interval struct {
Percentage uint8
}

// BindFlags will parse the given pflag.FlagSet and load the interval jitter.
// BindFlags will parse the given pflag.FlagSet and load the interval jitter
// with the default value of 10%.
func (o *Interval) BindFlags(fs *pflag.FlagSet) {
fs.Uint8Var(&o.Percentage, flagIntervalJitter, 0,
o.BindFlagsWithDefault(fs, -1)
}

// BindFlagsWithDefault will parse the given pflag.FlagSet and load the interval
// jitter. The defaultPercentage is used to set the default value for the
// interval jitter percentage. If the defaultPercentage is negative, then the
// default value (of 10%) will be used.
func (o *Interval) BindFlagsWithDefault(fs *pflag.FlagSet, defaultPercentage int) {
if defaultPercentage < 0 {
defaultPercentage = defaultIntervalJitterPercentage
}
fs.Uint8Var(&o.Percentage, flagIntervalJitter, uint8(defaultPercentage),
"Percentage of jitter to apply to interval durations. A value of 10 "+
"will apply a jitter of +/-10% to the interval duration. It cannot be "+
"negative, and must be less than 100.")
37 changes: 34 additions & 3 deletions runtime/jitter/interval_test.go
Original file line number Diff line number Diff line change
@@ -92,9 +92,40 @@ func TestInterval_BindFlags(t *testing.T) {
fs := pflag.NewFlagSet("test", pflag.ContinueOnError)
interval.BindFlags(fs)

err := fs.Set(flagIntervalJitter, "20")
g.Expect(err).ToNot(HaveOccurred())
g.Expect(interval.Percentage).To(Equal(uint8(20)))
g.Expect(interval.Percentage).To(Equal(uint8(defaultIntervalJitterPercentage)))
}

func TestInterval_BindFlagsWithDefault(t *testing.T) {
g := NewWithT(t)

t.Run("with default fallback", func(t *testing.T) {
interval := &Interval{}

fs := pflag.NewFlagSet("test", pflag.ContinueOnError)
interval.BindFlagsWithDefault(fs, -1)

g.Expect(interval.Percentage).To(Equal(uint8(defaultIntervalJitterPercentage)))
})

t.Run("with custom default", func(t *testing.T) {
interval := &Interval{}

fs := pflag.NewFlagSet("test", pflag.ContinueOnError)
interval.BindFlagsWithDefault(fs, 50)

g.Expect(interval.Percentage).To(Equal(uint8(50)))
})

t.Run("with flag override", func(t *testing.T) {
interval := &Interval{}

fs := pflag.NewFlagSet("test", pflag.ContinueOnError)
interval.BindFlagsWithDefault(fs, 0)

err := fs.Set("interval-jitter-percentage", "25")
g.Expect(err).ToNot(HaveOccurred())
g.Expect(interval.Percentage).To(Equal(uint8(25)))
})
}

func TestInterval_SetGlobalJitter(t *testing.T) {

0 comments on commit 683036a

Please sign in to comment.