Skip to content

Commit

Permalink
fix(region): prevent infinite recursion
Browse files Browse the repository at this point in the history
When weekDays is [1,2,3,4,5,6,7] and timePoints is [0], It will recurse infinitely.
  • Loading branch information
rainzm committed Nov 12, 2020
1 parent b93d695 commit 0bbe2e7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
16 changes: 10 additions & 6 deletions pkg/compute/models/snapshotpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -732,15 +732,18 @@ func computeNextSyncTime(weekDays, timePoints []int, base time.Time) time.Time {
if base.IsZero() {
base = time.Now()
}

// Add 1 hour to base to prevent the calculation result from being equal to the input base
base = base.Add(time.Hour)
base = base.Truncate(time.Hour)

baseWeekday := int(base.Weekday())
if baseWeekday == 0 {
baseWeekday = 7
}
weekDays = append(weekDays, weekDays[0]+7)
index := sort.SearchInts(weekDays, baseWeekday)
addDay := weekDays[index] - baseWeekday
indexw := sort.SearchInts(weekDays, baseWeekday)
addDay := weekDays[indexw] - baseWeekday
nextTime := base.AddDate(0, 0, addDay)

// find timePoint closest to the base
Expand All @@ -750,6 +753,11 @@ func computeNextSyncTime(weekDays, timePoints []int, base time.Time) time.Time {
} else {
baseHour := base.Hour()
index := sort.SearchInts(timePoints, baseHour)
if index == len(timePoints) {
// indexw + 1 must less than len(weekDays)
addDay = weekDays[indexw+1] - baseWeekday
nextTime = base.AddDate(0, 0, addDay)
}
index = index % len(timePoints)
if timePoints[index] == baseHour {
index = (index + 1) % len(timePoints)
Expand All @@ -760,10 +768,6 @@ func computeNextSyncTime(weekDays, timePoints []int, base time.Time) time.Time {
}
nextTime = time.Date(nextTime.Year(), nextTime.Month(), nextTime.Day(), newHour, 0, 0, 0, base.Location())

if !nextTime.After(base) {
// If the calculated NextSyncTime and base are equal, add 1 hour to base and recursive processing.
return computeNextSyncTime(weekDays, timePoints, base.Add(time.Hour))
}
return nextTime
}

Expand Down
16 changes: 16 additions & 0 deletions pkg/compute/models/snapshotpolicy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,22 @@ func TestSSnapshotPolicy_ComputeNextSyncTime(t *testing.T) {
base: "2020-10-31 01:00:00",
want: "2020-10-31 02:00:00",
},
{
in: &SSnapshotPolicy{
RepeatWeekdays: SnapshotPolicyManager.RepeatWeekdaysParseIntArray([]int{6}),
TimePoints: SnapshotPolicyManager.TimePointsParseIntArray([]int{2, 4}),
},
base: "2020-10-31 05:00:00",
want: "2020-11-07 02:00:00",
},
{
in: &SSnapshotPolicy{
RepeatWeekdays: SnapshotPolicyManager.RepeatWeekdaysParseIntArray([]int{1, 2, 3, 4, 5, 6, 7}),
TimePoints: SnapshotPolicyManager.TimePointsParseIntArray([]int{0}),
},
base: "2020-10-31 05:00:00",
want: "2020-11-01 00:00:00",
},
}
for _, c := range cases {
base, _ := time.Parse(timeStr, c.base)
Expand Down

0 comments on commit 0bbe2e7

Please sign in to comment.