Skip to content

Commit e5f3927

Browse files
committed
Merge pull request getlantern#4498 from getlantern/encodetime
Compressed encoding of asof time for bandwidth tracking
2 parents 67d94d7 + d2b8318 commit e5f3927

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

src/github.com/getlantern/bandwidth/bandwidth.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ var (
1616
quota *Quota
1717
mutex sync.RWMutex
1818

19+
epoch = time.Date(2016, 1, 1, 0, 0, 0, 0, time.UTC)
20+
1921
// Updates is a channel on which one can receive updates to the Quota
2022
Updates = make(chan *Quota, 100)
2123
)
@@ -42,7 +44,8 @@ func GetQuota() *Quota {
4244
//
4345
// <used> is the string representation of a 64-bit unsigned integer
4446
// <allowed> is the string representation of a 64-bit unsigned integer
45-
// <asof> is the 64-bit signed integer representing nanoseconds since epoch
47+
// <asof> is the 64-bit signed integer representing seconds since a custom
48+
// epoch (00:00:00 01/01/2016 UTC).
4649
func Track(resp *http.Response) {
4750
xbq := resp.Header.Get("XBQ")
4851
if xbq == "" {
@@ -71,7 +74,7 @@ func Track(resp *http.Response) {
7174
log.Debugf("Malformed XBQ header %v, can't parse as of time: %v", err)
7275
return
7376
}
74-
asof := time.Unix(0, asofInt)
77+
asof := epoch.Add(time.Duration(asofInt) * time.Second)
7578
mutex.Lock()
7679
if quota == nil || quota.AsOf.Before(asof) {
7780
quota = &Quota{allowed, used, asof}

src/github.com/getlantern/bandwidth/bandwidth_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ func TestRoundTrip(t *testing.T) {
3131
assert.EqualValues(t, 500, q2.MiBAllowed)
3232
assert.EqualValues(t, 6, q.MiBUsed)
3333
assert.EqualValues(t, 6, q2.MiBUsed)
34-
assert.EqualValues(t, time.Unix(0, 3), q.AsOf)
35-
assert.EqualValues(t, time.Unix(0, 3), q2.AsOf)
34+
asof := epoch.Add(3 * time.Second)
35+
assert.EqualValues(t, asof, q.AsOf)
36+
assert.EqualValues(t, asof, q2.AsOf)
3637
}
3738

3839
}

0 commit comments

Comments
 (0)