Skip to content

Commit 46dc4f3

Browse files
committed
RL: use unix nanotime for internal time representation
1 parent e081033 commit 46dc4f3

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

rate/rate.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ type Limiter struct {
6060
burst int64
6161
tokens float64
6262
// last is the last time the limiter's tokens field was updated
63-
last time.Time
63+
last int64
6464
// lastEvent is the latest time of a rate-limited event (past or future)
65-
lastEvent time.Time
65+
lastEvent int64
6666
}
6767

6868
// Limit returns the maximum overall event rate.
@@ -123,7 +123,7 @@ type Reservation struct {
123123
ok bool
124124
lim *Limiter
125125
tokens int64
126-
timeToAct time.Time
126+
timeToAct int64
127127
// This is the Limit at reservation time, it can change later.
128128
limit Limit
129129
}
@@ -151,7 +151,7 @@ func (r *Reservation) DelayFrom(t time.Time) time.Duration {
151151
if !r.ok {
152152
return InfDuration
153153
}
154-
delay := r.timeToAct.Sub(t)
154+
delay := time.Unix(0, r.timeToAct).Sub(t)
155155
if delay < 0 {
156156
return 0
157157
}
@@ -174,14 +174,14 @@ func (r *Reservation) CancelAt(t time.Time) {
174174
r.lim.mu.Lock()
175175
defer r.lim.mu.Unlock()
176176

177-
if r.lim.limit == Inf || r.tokens == 0 || r.timeToAct.Before(t) {
177+
if r.lim.limit == Inf || r.tokens == 0 || time.Unix(0, r.timeToAct).Before(t) {
178178
return
179179
}
180180

181181
// calculate tokens to restore
182182
// The duration between lim.lastEvent and r.timeToAct tells us how many tokens were reserved
183183
// after r was obtained. These tokens should not be restored.
184-
restoreTokens := float64(r.tokens) - r.limit.tokensFromDuration(r.lim.lastEvent.Sub(r.timeToAct))
184+
restoreTokens := float64(r.tokens) - r.limit.tokensFromDuration(time.Duration(r.lim.lastEvent - r.timeToAct))
185185
if restoreTokens <= 0 {
186186
return
187187
}
@@ -193,12 +193,12 @@ func (r *Reservation) CancelAt(t time.Time) {
193193
tokens = burst
194194
}
195195
// update state
196-
r.lim.last = t
196+
r.lim.last = t.UnixNano()
197197
r.lim.tokens = tokens
198-
if r.timeToAct.Equal(r.lim.lastEvent) {
199-
prevEvent := r.timeToAct.Add(r.limit.durationFromTokens(float64(-r.tokens)))
198+
if r.timeToAct == r.lim.lastEvent {
199+
prevEvent := time.Unix(0, r.timeToAct).Add(r.limit.durationFromTokens(float64(-r.tokens)))
200200
if !prevEvent.Before(t) {
201-
r.lim.lastEvent = prevEvent
201+
r.lim.lastEvent = prevEvent.UnixNano()
202202
}
203203
}
204204
}
@@ -309,7 +309,7 @@ func (lim *Limiter) SetLimitAt(t time.Time, newLimit Limit) {
309309

310310
tokens := lim.advance(t)
311311

312-
lim.last = t
312+
lim.last = t.UnixNano()
313313
lim.tokens = tokens
314314
lim.limit = newLimit
315315
}
@@ -326,7 +326,7 @@ func (lim *Limiter) SetBurstAt(t time.Time, newBurst int64) {
326326

327327
tokens := lim.advance(t)
328328

329-
lim.last = t
329+
lim.last = t.UnixNano()
330330
lim.tokens = tokens
331331
lim.burst = newBurst
332332
}
@@ -343,7 +343,7 @@ func (lim *Limiter) reserveN(t time.Time, n int64, maxFutureReserve time.Duratio
343343
ok: true,
344344
lim: lim,
345345
tokens: n,
346-
timeToAct: t,
346+
timeToAct: t.UnixNano(),
347347
}
348348
}
349349

@@ -369,10 +369,10 @@ func (lim *Limiter) reserveN(t time.Time, n int64, maxFutureReserve time.Duratio
369369
}
370370
if ok {
371371
r.tokens = n
372-
r.timeToAct = t.Add(waitDuration)
372+
r.timeToAct = t.Add(waitDuration).UnixNano()
373373

374374
// Update state
375-
lim.last = t
375+
lim.last = t.UnixNano()
376376
lim.tokens = tokens
377377
lim.lastEvent = r.timeToAct
378378
}
@@ -385,7 +385,7 @@ func (lim *Limiter) reserveN(t time.Time, n int64, maxFutureReserve time.Duratio
385385
// lim is not changed.
386386
// advance requires that lim.mu is held.
387387
func (lim *Limiter) advance(t time.Time) (newTokens float64) {
388-
last := lim.last
388+
last := time.Unix(0, lim.last)
389389
if t.Before(last) {
390390
last = t
391391
}

rate/rate_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,9 +333,9 @@ func runReserve(t *testing.T, lim *Limiter, req request) *Reservation {
333333
func runReserveMax(t *testing.T, lim *Limiter, req request, maxReserve time.Duration) *Reservation {
334334
t.Helper()
335335
r := lim.reserveN(req.t, req.n, maxReserve)
336-
if r.ok && (dSince(r.timeToAct) != dSince(req.act)) || r.ok != req.ok {
336+
if r.ok && (dSince(time.Unix(0, r.timeToAct)) != dSince(req.act)) || r.ok != req.ok {
337337
t.Errorf("lim.reserveN(t%d, %v, %v) = (t%d, %v) want (t%d, %v)",
338-
dSince(req.t), req.n, maxReserve, dSince(r.timeToAct), r.ok, dSince(req.act), req.ok)
338+
dSince(req.t), req.n, maxReserve, dSince(time.Unix(0, r.timeToAct)), r.ok, dSince(req.act), req.ok)
339339
}
340340
return &r
341341
}

0 commit comments

Comments
 (0)