Skip to content

Commit 5e8de8b

Browse files
committed
internal/upload: deal with files that are too new or too old
Be more careful about dates, and add tests for various boundary conditions. In particular, the current time is evaluated only once. Change-Id: I23f4919d37d9a77784be2afcf9ba348ed6a223f9 Reviewed-on: https://go-review.googlesource.com/c/telemetry/+/524877 Reviewed-by: Hyang-Ah Hana Kim <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Peter Weinberger <[email protected]>
1 parent 54ea7e4 commit 5e8de8b

File tree

9 files changed

+512
-218
lines changed

9 files changed

+512
-218
lines changed

internal/counter/file.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@ func Open() func() {
355355
mainCounter.Add(1)
356356
defaultFile.rotate()
357357
return func() {
358+
// Once this has been called, the defaultFile is no longer usable.
358359
mf := defaultFile.current.Load()
359360
if mf == nil {
360361
// telemetry might have been off

internal/upload/date.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,13 @@ import (
1515

1616
// time and date handling
1717

18-
// another name for time.Now() to use for testing
19-
var now = time.Now
18+
// all the upload processing takes place (conceptually) at
19+
// a single instant. Most of the time this wouldn't matter
20+
// but it protects against time skew if time.Now
21+
// increases the day between calls, as might happen (rarely) by chance
22+
// or if there are long scheduling delays between calls.
23+
var thisInstant = time.Now().UTC()
24+
2025
var distantPast = 21 * 24 * time.Hour
2126

2227
// reports that are too old (21 days) are not uploaded
@@ -26,7 +31,8 @@ func tooOld(date string) bool {
2631
logger.Printf("tooOld: %v", err)
2732
return false
2833
}
29-
return now().Sub(t) > distantPast
34+
age := thisInstant.Sub(t)
35+
return age > distantPast
3036
}
3137

3238
// return the expiry date of a countfile in YYYY-MM-DD format
@@ -57,14 +63,13 @@ func expiry(fname string) time.Time {
5763
logger.Printf("time.Parse: %v for %s", err, fname)
5864
return farFuture // don't process it, whatever it is
5965
}
60-
// TODO(pjw): check for off-by-one-day?
6166
return expiry
6267
}
6368

6469
// stillOpen returns true if the counter file might still be active
6570
func stillOpen(fname string) bool {
6671
expiry := expiry(fname)
67-
return expiry.After(now()) // TODO(pjw): off by one day?
72+
return expiry.After(thisInstant)
6873
}
6974

7075
// avoid parsing count files multiple times

0 commit comments

Comments
 (0)