Skip to content
This repository was archived by the owner on Apr 19, 2024. It is now read-only.

Commit 5ce3bc1

Browse files
committed
Rename RequestTime to CreatedAt in protos.
1 parent d55016d commit 5ce3bc1

12 files changed

+221
-193
lines changed

algorithms.go

+19-19
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,12 @@ func tokenBucket(ctx context.Context, s Store, c Cache, r *RateLimitReq, reqStat
132132
}
133133

134134
// 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 {
137137
// Renew item.
138138
span.AddEvent("Limit has expired")
139-
expire = requestTime + r.Duration
140-
t.CreatedAt = requestTime
139+
expire = createdAt + r.Duration
140+
t.CreatedAt = createdAt
141141
t.Remaining = t.Limit
142142
}
143143

@@ -204,14 +204,14 @@ func tokenBucket(ctx context.Context, s Store, c Cache, r *RateLimitReq, reqStat
204204

205205
// Called by tokenBucket() when adding a new item in the store.
206206
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
209209

210210
t := &TokenBucketItem{
211211
Limit: r.Limit,
212212
Duration: r.Duration,
213213
Remaining: r.Limit - r.Hits,
214-
CreatedAt: requestTime,
214+
CreatedAt: createdAt,
215215
}
216216

217217
// Add a new rate limit to the cache.
@@ -265,7 +265,7 @@ func leakyBucket(ctx context.Context, s Store, c Cache, r *RateLimitReq, reqStat
265265
r.Burst = r.Limit
266266
}
267267

268-
requestTime := *r.RequestTime
268+
createdAt := *r.CreatedAt
269269

270270
// Get rate limit from cache.
271271
hashKey := r.HashKey()
@@ -354,16 +354,16 @@ func leakyBucket(ctx context.Context, s Store, c Cache, r *RateLimitReq, reqStat
354354
}
355355

356356
if r.Hits != 0 {
357-
c.UpdateExpiration(r.HashKey(), requestTime+duration)
357+
c.UpdateExpiration(r.HashKey(), createdAt+duration)
358358
}
359359

360360
// 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
362362
leak := float64(elapsed) / rate
363363

364364
if int64(leak) > 0 {
365365
b.Remaining += leak
366-
b.UpdatedAt = requestTime
366+
b.UpdatedAt = createdAt
367367
}
368368

369369
if int64(b.Remaining) > b.Burst {
@@ -374,7 +374,7 @@ func leakyBucket(ctx context.Context, s Store, c Cache, r *RateLimitReq, reqStat
374374
Limit: b.Limit,
375375
Remaining: int64(b.Remaining),
376376
Status: Status_UNDER_LIMIT,
377-
ResetTime: requestTime + (b.Limit-int64(b.Remaining))*int64(rate),
377+
ResetTime: createdAt + (b.Limit-int64(b.Remaining))*int64(rate),
378378
}
379379

380380
// 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
398398
if int64(b.Remaining) == r.Hits {
399399
b.Remaining = 0
400400
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)
402402
return rl, nil
403403
}
404404

@@ -426,7 +426,7 @@ func leakyBucket(ctx context.Context, s Store, c Cache, r *RateLimitReq, reqStat
426426

427427
b.Remaining -= float64(r.Hits)
428428
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)
430430
return rl, nil
431431
}
432432

@@ -435,7 +435,7 @@ func leakyBucket(ctx context.Context, s Store, c Cache, r *RateLimitReq, reqStat
435435

436436
// Called by leakyBucket() when adding a new item in the store.
437437
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
439439
duration := r.Duration
440440
rate := float64(duration) / float64(r.Limit)
441441
if HasBehavior(r.Behavior, Behavior_DURATION_IS_GREGORIAN) {
@@ -454,15 +454,15 @@ func leakyBucketNewItem(ctx context.Context, s Store, c Cache, r *RateLimitReq,
454454
Remaining: float64(r.Burst - r.Hits),
455455
Limit: r.Limit,
456456
Duration: duration,
457-
UpdatedAt: requestTime,
457+
UpdatedAt: createdAt,
458458
Burst: r.Burst,
459459
}
460460

461461
rl := RateLimitResp{
462462
Status: Status_UNDER_LIMIT,
463463
Limit: b.Limit,
464464
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),
466466
}
467467

468468
// 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,
472472
}
473473
rl.Status = Status_OVER_LIMIT
474474
rl.Remaining = 0
475-
rl.ResetTime = requestTime + (rl.Limit-rl.Remaining)*int64(rate)
475+
rl.ResetTime = createdAt + (rl.Limit-rl.Remaining)*int64(rate)
476476
b.Remaining = 0
477477
}
478478

479479
item := &CacheItem{
480-
ExpireAt: requestTime + duration,
480+
ExpireAt: createdAt + duration,
481481
Algorithm: r.Algorithm,
482482
Key: r.HashKey(),
483483
Value: &b,

benchmark_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func BenchmarkServer(b *testing.B) {
3232
conf := guber.Config{}
3333
err := conf.SetDefaults()
3434
require.NoError(b, err, "Error in conf.SetDefaults")
35-
requestTime := epochMillis(clock.Now())
35+
createdAt := epochMillis(clock.Now())
3636

3737
b.Run("GetPeerRateLimit", func(b *testing.B) {
3838
client, err := guber.NewPeerClient(guber.PeerConfig{
@@ -49,10 +49,10 @@ func BenchmarkServer(b *testing.B) {
4949
Name: b.Name(),
5050
UniqueKey: guber.RandomString(10),
5151
// Behavior: guber.Behavior_NO_BATCHING,
52-
Limit: 10,
53-
Duration: 5,
54-
Hits: 1,
55-
RequestTime: &requestTime,
52+
Limit: 10,
53+
Duration: 5,
54+
Hits: 1,
55+
CreatedAt: &createdAt,
5656
})
5757
if err != nil {
5858
b.Errorf("Error in client.GetPeerRateLimit: %s", err)

functional_test.go

+18-18
Original file line numberDiff line numberDiff line change
@@ -1646,7 +1646,7 @@ func TestGetPeerRateLimits(t *testing.T) {
16461646
t.Run("Stable rate check request order", func(t *testing.T) {
16471647
// Ensure response order matches rate check request order.
16481648
// Try various batch sizes.
1649-
requestTime := epochMillis(clock.Now())
1649+
createdAt := epochMillis(clock.Now())
16501650
testCases := []int{1, 2, 5, 10, 100, 1000}
16511651

16521652
for _, n := range testCases {
@@ -1657,14 +1657,14 @@ func TestGetPeerRateLimits(t *testing.T) {
16571657
}
16581658
for i := 0; i < n; i++ {
16591659
req.Requests[i] = &guber.RateLimitReq{
1660-
Name: name,
1661-
UniqueKey: guber.RandomString(10),
1662-
Hits: 0,
1663-
Limit: 1000 + int64(i),
1664-
Duration: 1000,
1665-
Algorithm: guber.Algorithm_TOKEN_BUCKET,
1666-
Behavior: guber.Behavior_BATCHING,
1667-
RequestTime: &requestTime,
1660+
Name: name,
1661+
UniqueKey: guber.RandomString(10),
1662+
Hits: 0,
1663+
Limit: 1000 + int64(i),
1664+
Duration: 1000,
1665+
Algorithm: guber.Algorithm_TOKEN_BUCKET,
1666+
Behavior: guber.Behavior_BATCHING,
1667+
CreatedAt: &createdAt,
16681668
}
16691669
}
16701670

@@ -1690,18 +1690,18 @@ func TestGetPeerRateLimits(t *testing.T) {
16901690
func TestGlobalBehavior(t *testing.T) {
16911691
const limit = 1000
16921692
broadcastTimeout := 400 * time.Millisecond
1693-
requestTime := epochMillis(clock.Now())
1693+
createdAt := epochMillis(clock.Now())
16941694

16951695
makeReq := func(name, key string, hits int64) *guber.RateLimitReq {
16961696
return &guber.RateLimitReq{
1697-
Name: name,
1698-
UniqueKey: key,
1699-
Algorithm: guber.Algorithm_TOKEN_BUCKET,
1700-
Behavior: guber.Behavior_GLOBAL,
1701-
Duration: guber.Minute * 3,
1702-
Hits: hits,
1703-
Limit: limit,
1704-
RequestTime: &requestTime,
1697+
Name: name,
1698+
UniqueKey: key,
1699+
Algorithm: guber.Algorithm_TOKEN_BUCKET,
1700+
Behavior: guber.Behavior_GLOBAL,
1701+
Duration: guber.Minute * 3,
1702+
Hits: hits,
1703+
Limit: limit,
1704+
CreatedAt: &createdAt,
17051705
}
17061706
}
17071707

global.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -248,11 +248,11 @@ func (gm *globalManager) broadcastPeers(ctx context.Context, updates map[string]
248248
continue
249249
}
250250
updateReq := &UpdatePeerGlobal{
251-
Key: update.HashKey(),
252-
Algorithm: update.Algorithm,
253-
Duration: update.Duration,
254-
Status: status,
255-
RequestTime: *update.RequestTime,
251+
Key: update.HashKey(),
252+
Algorithm: update.Algorithm,
253+
Duration: update.Duration,
254+
Status: status,
255+
CreatedAt: *update.CreatedAt,
256256
}
257257
req.Globals = append(req.Globals, updateReq)
258258
}

gubernator.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ func (s *V1Instance) GetRateLimits(ctx context.Context, r *GetRateLimitsReq) (*G
192192
"Requests.RateLimits list too large; max size is '%d'", maxBatchSize)
193193
}
194194

195-
requestTime := epochMillis(clock.Now())
195+
createdAt := epochMillis(clock.Now())
196196
resp := GetRateLimitsResp{
197197
Responses: make([]*RateLimitResp, len(r.Requests)),
198198
}
@@ -215,8 +215,8 @@ func (s *V1Instance) GetRateLimits(ctx context.Context, r *GetRateLimitsReq) (*G
215215
resp.Responses[i] = &RateLimitResp{Error: "field 'namespace' cannot be empty"}
216216
continue
217217
}
218-
if req.RequestTime == nil || *req.RequestTime == 0 {
219-
req.RequestTime = &requestTime
218+
if req.CreatedAt == nil || *req.CreatedAt == 0 {
219+
req.CreatedAt = &createdAt
220220
}
221221

222222
if ctx.Err() != nil {
@@ -511,10 +511,10 @@ func (s *V1Instance) GetPeerRateLimits(ctx context.Context, r *GetPeerRateLimits
511511
SetBehavior(&rin.req.Behavior, Behavior_DRAIN_OVER_LIMIT, true)
512512
}
513513

514-
// Assign default to RequestTime for backwards compatibility.
515-
if rin.req.RequestTime == nil || *rin.req.RequestTime == 0 {
516-
requestTime := epochMillis(clock.Now())
517-
rin.req.RequestTime = &requestTime
514+
// Assign default to CreatedAt for backwards compatibility.
515+
if rin.req.CreatedAt == nil || *rin.req.CreatedAt == 0 {
516+
createdAt := epochMillis(clock.Now())
517+
rin.req.CreatedAt = &createdAt
518518
}
519519

520520
rl, err := s.getLocalRateLimit(ctx, rin.req, reqState)

0 commit comments

Comments
 (0)