@@ -132,12 +132,12 @@ func tokenBucket(ctx context.Context, s Store, c Cache, r *RateLimitReq, reqStat
132
132
}
133
133
134
134
// If our new duration means we are currently expired.
135
- requestTime := * r .RequestTime
136
- if expire <= requestTime {
135
+ createdAt := * r .CreatedAt
136
+ if expire <= createdAt {
137
137
// Renew item.
138
138
span .AddEvent ("Limit has expired" )
139
- expire = requestTime + r .Duration
140
- t .CreatedAt = requestTime
139
+ expire = createdAt + r .Duration
140
+ t .CreatedAt = createdAt
141
141
t .Remaining = t .Limit
142
142
}
143
143
@@ -204,14 +204,14 @@ func tokenBucket(ctx context.Context, s Store, c Cache, r *RateLimitReq, reqStat
204
204
205
205
// Called by tokenBucket() when adding a new item in the store.
206
206
func tokenBucketNewItem (ctx context.Context , s Store , c Cache , r * RateLimitReq , reqState RateLimitReqState ) (resp * RateLimitResp , err error ) {
207
- requestTime := * r .RequestTime
208
- expire := requestTime + r .Duration
207
+ createdAt := * r .CreatedAt
208
+ expire := createdAt + r .Duration
209
209
210
210
t := & TokenBucketItem {
211
211
Limit : r .Limit ,
212
212
Duration : r .Duration ,
213
213
Remaining : r .Limit - r .Hits ,
214
- CreatedAt : requestTime ,
214
+ CreatedAt : createdAt ,
215
215
}
216
216
217
217
// Add a new rate limit to the cache.
@@ -265,7 +265,7 @@ func leakyBucket(ctx context.Context, s Store, c Cache, r *RateLimitReq, reqStat
265
265
r .Burst = r .Limit
266
266
}
267
267
268
- requestTime := * r .RequestTime
268
+ createdAt := * r .CreatedAt
269
269
270
270
// Get rate limit from cache.
271
271
hashKey := r .HashKey ()
@@ -354,16 +354,16 @@ func leakyBucket(ctx context.Context, s Store, c Cache, r *RateLimitReq, reqStat
354
354
}
355
355
356
356
if r .Hits != 0 {
357
- c .UpdateExpiration (r .HashKey (), requestTime + duration )
357
+ c .UpdateExpiration (r .HashKey (), createdAt + duration )
358
358
}
359
359
360
360
// Calculate how much leaked out of the bucket since the last time we leaked a hit
361
- elapsed := requestTime - b .UpdatedAt
361
+ elapsed := createdAt - b .UpdatedAt
362
362
leak := float64 (elapsed ) / rate
363
363
364
364
if int64 (leak ) > 0 {
365
365
b .Remaining += leak
366
- b .UpdatedAt = requestTime
366
+ b .UpdatedAt = createdAt
367
367
}
368
368
369
369
if int64 (b .Remaining ) > b .Burst {
@@ -374,7 +374,7 @@ func leakyBucket(ctx context.Context, s Store, c Cache, r *RateLimitReq, reqStat
374
374
Limit : b .Limit ,
375
375
Remaining : int64 (b .Remaining ),
376
376
Status : Status_UNDER_LIMIT ,
377
- ResetTime : requestTime + (b .Limit - int64 (b .Remaining ))* int64 (rate ),
377
+ ResetTime : createdAt + (b .Limit - int64 (b .Remaining ))* int64 (rate ),
378
378
}
379
379
380
380
// TODO: Feature missing: check for Duration change between item/request.
@@ -398,7 +398,7 @@ func leakyBucket(ctx context.Context, s Store, c Cache, r *RateLimitReq, reqStat
398
398
if int64 (b .Remaining ) == r .Hits {
399
399
b .Remaining = 0
400
400
rl .Remaining = int64 (b .Remaining )
401
- rl .ResetTime = requestTime + (rl .Limit - rl .Remaining )* int64 (rate )
401
+ rl .ResetTime = createdAt + (rl .Limit - rl .Remaining )* int64 (rate )
402
402
return rl , nil
403
403
}
404
404
@@ -426,7 +426,7 @@ func leakyBucket(ctx context.Context, s Store, c Cache, r *RateLimitReq, reqStat
426
426
427
427
b .Remaining -= float64 (r .Hits )
428
428
rl .Remaining = int64 (b .Remaining )
429
- rl .ResetTime = requestTime + (rl .Limit - rl .Remaining )* int64 (rate )
429
+ rl .ResetTime = createdAt + (rl .Limit - rl .Remaining )* int64 (rate )
430
430
return rl , nil
431
431
}
432
432
@@ -435,7 +435,7 @@ func leakyBucket(ctx context.Context, s Store, c Cache, r *RateLimitReq, reqStat
435
435
436
436
// Called by leakyBucket() when adding a new item in the store.
437
437
func leakyBucketNewItem (ctx context.Context , s Store , c Cache , r * RateLimitReq , reqState RateLimitReqState ) (resp * RateLimitResp , err error ) {
438
- requestTime := * r .RequestTime
438
+ createdAt := * r .CreatedAt
439
439
duration := r .Duration
440
440
rate := float64 (duration ) / float64 (r .Limit )
441
441
if HasBehavior (r .Behavior , Behavior_DURATION_IS_GREGORIAN ) {
@@ -454,15 +454,15 @@ func leakyBucketNewItem(ctx context.Context, s Store, c Cache, r *RateLimitReq,
454
454
Remaining : float64 (r .Burst - r .Hits ),
455
455
Limit : r .Limit ,
456
456
Duration : duration ,
457
- UpdatedAt : requestTime ,
457
+ UpdatedAt : createdAt ,
458
458
Burst : r .Burst ,
459
459
}
460
460
461
461
rl := RateLimitResp {
462
462
Status : Status_UNDER_LIMIT ,
463
463
Limit : b .Limit ,
464
464
Remaining : r .Burst - r .Hits ,
465
- ResetTime : requestTime + (b .Limit - (r .Burst - r .Hits ))* int64 (rate ),
465
+ ResetTime : createdAt + (b .Limit - (r .Burst - r .Hits ))* int64 (rate ),
466
466
}
467
467
468
468
// Client could be requesting that we start with the bucket OVER_LIMIT
@@ -472,12 +472,12 @@ func leakyBucketNewItem(ctx context.Context, s Store, c Cache, r *RateLimitReq,
472
472
}
473
473
rl .Status = Status_OVER_LIMIT
474
474
rl .Remaining = 0
475
- rl .ResetTime = requestTime + (rl .Limit - rl .Remaining )* int64 (rate )
475
+ rl .ResetTime = createdAt + (rl .Limit - rl .Remaining )* int64 (rate )
476
476
b .Remaining = 0
477
477
}
478
478
479
479
item := & CacheItem {
480
- ExpireAt : requestTime + duration ,
480
+ ExpireAt : createdAt + duration ,
481
481
Algorithm : r .Algorithm ,
482
482
Key : r .HashKey (),
483
483
Value : & b ,
0 commit comments