Skip to content

"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files

fs: fix --bwlimit timetables written out of order using the wrong limit

Rohit Behera124f3bd830ed
diff --git a/fs/bwtimetable.go b/fs/bwtimetable.go
index cbb0a2f9d..b5006e623 100644
--- a/fs/bwtimetable.go
+++ b/fs/bwtimetable.go
@@ -237,12 +237,16 @@ func (x BwTimetable) LimitAt(tt time.Time) BwTimeSlot {
dayOfWeekHHMM := int(tt.Weekday())*10000 + tt.Hour()*100 + tt.Minute()
- // By default, we return the last element in the timetable. This
- // satisfies two conditions: 1) If there's only one element it
- // will always be selected, and 2) The last element of the table
- // will "wrap around" until overridden by an earlier time slot.
- // there's only one time slot in the timetable.
- ret := x[len(x)-1]
+ // By default, we return the latest time slot in the week, which
+ // "wraps around" until overridden by an earlier time slot. This is
+ // the only time slot when there is just one, and doesn't depend on
+ // the order the time slots were given in.
+ ret := x[0]
+ for _, ts := range x[1:] {
+ if (ts.DayOfTheWeek*10000)+ts.HHMM >= (ret.DayOfTheWeek*10000)+ret.HHMM {
+ ret = ts
+ }
+ }
mindif := 0
first := true
diff --git a/fs/bwtimetable_test.go b/fs/bwtimetable_test.go
index 218bb6ccb..0692d2c92 100644
--- a/fs/bwtimetable_test.go
+++ b/fs/bwtimetable_test.go
@@ -578,6 +578,14 @@ func TestBwTimetableLimitAt(t *testing.T) {
time.Date(2017, time.April, 17, 10, 59, 0, 0, time.UTC),
BwTimeSlot{DayOfTheWeek: 0, HHMM: 2300, Bandwidth: BwPair{Tx: 666 * 1024, Rx: 66 * 1024}},
},
+ {
+ BwTimetable{
+ BwTimeSlot{DayOfTheWeek: 6, HHMM: 0000, Bandwidth: BwPair{Tx: -1, Rx: -1}},
+ BwTimeSlot{DayOfTheWeek: 1, HHMM: 0000, Bandwidth: BwPair{Tx: 1024 * 1024, Rx: 1024 * 1024}},
+ },
+ time.Date(2017, time.April, 23, 10, 0, 0, 0, time.UTC),
+ BwTimeSlot{DayOfTheWeek: 6, HHMM: 0000, Bandwidth: BwPair{Tx: -1, Rx: -1}},
+ },
} {
slot := test.tt.LimitAt(test.now)
assert.Equal(t, test.want, slot)