Skip to content

Commit

Permalink
fix if only one target
Browse files Browse the repository at this point in the history
  • Loading branch information
latitov committed Aug 22, 2024
1 parent 036cfa3 commit 27491bf
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
8 changes: 7 additions & 1 deletion replicount/01-sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ func Test_001(t *testing.T) {
}

func Test_002(t *testing.T) {
replSet0 := []string{
"0004a",
}

replSet1 := []string{
"0001",
"0002",
Expand All @@ -55,7 +59,9 @@ func Test_002(t *testing.T) {

r.PollFunc = func() (idPtr *string) {
var id string
if time.Now().Before(t0.Add(time.Second * 40)) {
if time.Now().Before(t0.Add(time.Second * 5)) {
id = replSet1[rand.Intn(len(replSet0))]
} else if time.Now().Before(t0.Add(time.Second * 40)) {
id = replSet1[rand.Intn(len(replSet1))]
} else {
id = replSet2[rand.Intn(len(replSet2))]
Expand Down
16 changes: 12 additions & 4 deletions replicount/replicount.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func New(of ...func(r *Replicount)) (r *Replicount) {
state: mode_Slow,
mainTimer: timmer.New(),
fastModeLengthTimer: timmer.New(),
currentResult: &ChangeableObject{NumberOfReplicas: 1},
currentResult: &ChangeableObject{NumberOfReplicas: 0},
}
for _, f := range of {
f(r)
Expand Down Expand Up @@ -155,15 +155,15 @@ func (r *Replicount) scheduler() {
}
r.mainTimer.Restart( // immediate race-restart of the main timer
r.SlowPollPeriodPerReplica /
time.Duration(r.currentResult.NumberOfReplicas) /
time.Duration(atLeastOne(r.currentResult.NumberOfReplicas)) /
time.Duration(r.FastModeSpeedMultiple),
)
r.fastModeLengthTimer.Restart(r.fastModeLength)
}

} // dqf func
d := r.SlowPollPeriodPerReplica /
time.Duration(r.currentResult.NumberOfReplicas)
time.Duration(atLeastOne(r.currentResult.NumberOfReplicas))
r.mainTimer.Restart(d)
}() // go func

Expand Down Expand Up @@ -196,7 +196,7 @@ func (r *Replicount) scheduler() {
}
} // dqf func
d := r.SlowPollPeriodPerReplica /
time.Duration(r.currentResult.NumberOfReplicas) /
time.Duration(atLeastOne(r.currentResult.NumberOfReplicas)) /
time.Duration(r.FastModeSpeedMultiple)
r.mainTimer.Restart(d)
}() // go func
Expand Down Expand Up @@ -240,3 +240,11 @@ func (r *Replicount) log(msg string) {
r.LogFunc(msg)
}
}

func atLeastOne(i int) int {
if i < 1 {
return 1
} else {
return i
}
}

0 comments on commit 27491bf

Please sign in to comment.