Skip to content

Commit 20fdcbc

Browse files
authored
ratelimits: always use a pointer for limit (#7804)
The zero value for `limit` is invalid, so returning `nil` in error cases avoids silently returning invalid limits (and means that if code makes a mistake and references an invalid limit it will be an obvious clear stack trace). This is more consistent, since the methods on `limit` use a pointer receiver. Also, since `limit` is a fairly large object, this saves some copying. Related to #7803 and #7797.
1 parent 3506f09 commit 20fdcbc

File tree

5 files changed

+22
-22
lines changed

5 files changed

+22
-22
lines changed

ratelimits/gcra_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
func TestDecide(t *testing.T) {
1313
clk := clock.NewFake()
14-
limit := limit{Burst: 10, Count: 1, Period: config.Duration{Duration: time.Second}}
14+
limit := &limit{Burst: 10, Count: 1, Period: config.Duration{Duration: time.Second}}
1515
limit.precompute()
1616

1717
// Begin by using 1 of our 10 requests.
@@ -138,7 +138,7 @@ func TestDecide(t *testing.T) {
138138

139139
func TestMaybeRefund(t *testing.T) {
140140
clk := clock.NewFake()
141-
limit := limit{Burst: 10, Count: 1, Period: config.Duration{Duration: time.Second}}
141+
limit := &limit{Burst: 10, Count: 1, Period: config.Duration{Duration: time.Second}}
142142
limit.precompute()
143143

144144
// Begin by using 1 of our 10 requests.

ratelimits/limit.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func (l *limit) precompute() {
6363
l.burstOffset = l.emissionInterval * l.Burst
6464
}
6565

66-
func validateLimit(l limit) error {
66+
func validateLimit(l *limit) error {
6767
if l.Burst <= 0 {
6868
return fmt.Errorf("invalid burst '%d', must be > 0", l.Burst)
6969
}
@@ -76,7 +76,7 @@ func validateLimit(l limit) error {
7676
return nil
7777
}
7878

79-
type limits map[string]limit
79+
type limits map[string]*limit
8080

8181
// loadDefaults marshals the defaults YAML file at path into a map of limits.
8282
func loadDefaults(path string) (limits, error) {
@@ -156,7 +156,8 @@ func loadAndParseOverrideLimits(path string) (limits, error) {
156156

157157
for _, ov := range fromFile {
158158
for k, v := range ov {
159-
err = validateLimit(v.limit)
159+
limit := &v.limit
160+
err = validateLimit(limit)
160161
if err != nil {
161162
return nil, fmt.Errorf("validating override limit %q: %w", k, err)
162163
}
@@ -167,7 +168,6 @@ func loadAndParseOverrideLimits(path string) (limits, error) {
167168
v.limit.name = name
168169

169170
for _, entry := range v.Ids {
170-
limit := v.limit
171171
id := entry.Id
172172
err = validateIdForName(name, id)
173173
if err != nil {
@@ -248,11 +248,11 @@ func newLimitRegistry(defaults, overrides string) (*limitRegistry, error) {
248248
// required, bucketKey is optional. If bucketkey is empty, the default for the
249249
// limit specified by name is returned. If no default limit exists for the
250250
// specified name, errLimitDisabled is returned.
251-
func (l *limitRegistry) getLimit(name Name, bucketKey string) (limit, error) {
251+
func (l *limitRegistry) getLimit(name Name, bucketKey string) (*limit, error) {
252252
if !name.isValid() {
253253
// This should never happen. Callers should only be specifying the limit
254254
// Name enums defined in this package.
255-
return limit{}, fmt.Errorf("specified name enum %q, is invalid", name)
255+
return nil, fmt.Errorf("specified name enum %q, is invalid", name)
256256
}
257257
if bucketKey != "" {
258258
// Check for override.
@@ -265,5 +265,5 @@ func (l *limitRegistry) getLimit(name Name, bucketKey string) (limit, error) {
265265
if ok {
266266
return dl, nil
267267
}
268-
return limit{}, errLimitDisabled
268+
return nil, errLimitDisabled
269269
}

ratelimits/limit_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ func TestParseOverrideNameId(t *testing.T) {
4242
}
4343

4444
func TestValidateLimit(t *testing.T) {
45-
err := validateLimit(limit{Burst: 1, Count: 1, Period: config.Duration{Duration: time.Second}})
45+
err := validateLimit(&limit{Burst: 1, Count: 1, Period: config.Duration{Duration: time.Second}})
4646
test.AssertNotError(t, err, "valid limit")
4747

4848
// All of the following are invalid.
49-
for _, l := range []limit{
49+
for _, l := range []*limit{
5050
{Burst: 0, Count: 1, Period: config.Duration{Duration: time.Second}},
5151
{Burst: 1, Count: 0, Period: config.Duration{Duration: time.Second}},
5252
{Burst: 1, Count: 1, Period: config.Duration{Duration: 0}},

ratelimits/limiter_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ func TestRateLimitError(t *testing.T) {
497497
allowed: false,
498498
retryIn: 5 * time.Second,
499499
transaction: Transaction{
500-
limit: limit{
500+
limit: &limit{
501501
name: NewRegistrationsPerIPAddress,
502502
Burst: 10,
503503
Period: config.Duration{Duration: time.Hour},
@@ -513,7 +513,7 @@ func TestRateLimitError(t *testing.T) {
513513
allowed: false,
514514
retryIn: 10 * time.Second,
515515
transaction: Transaction{
516-
limit: limit{
516+
limit: &limit{
517517
name: NewRegistrationsPerIPv6Range,
518518
Burst: 5,
519519
Period: config.Duration{Duration: time.Hour},
@@ -529,7 +529,7 @@ func TestRateLimitError(t *testing.T) {
529529
allowed: false,
530530
retryIn: 10 * time.Second,
531531
transaction: Transaction{
532-
limit: limit{
532+
limit: &limit{
533533
name: NewOrdersPerAccount,
534534
Burst: 2,
535535
Period: config.Duration{Duration: time.Hour},
@@ -545,7 +545,7 @@ func TestRateLimitError(t *testing.T) {
545545
allowed: false,
546546
retryIn: 15 * time.Second,
547547
transaction: Transaction{
548-
limit: limit{
548+
limit: &limit{
549549
name: FailedAuthorizationsPerDomainPerAccount,
550550
Burst: 7,
551551
Period: config.Duration{Duration: time.Hour},
@@ -562,7 +562,7 @@ func TestRateLimitError(t *testing.T) {
562562
allowed: false,
563563
retryIn: 20 * time.Second,
564564
transaction: Transaction{
565-
limit: limit{
565+
limit: &limit{
566566
name: CertificatesPerDomain,
567567
Burst: 3,
568568
Period: config.Duration{Duration: time.Hour},
@@ -579,7 +579,7 @@ func TestRateLimitError(t *testing.T) {
579579
allowed: false,
580580
retryIn: 20 * time.Second,
581581
transaction: Transaction{
582-
limit: limit{
582+
limit: &limit{
583583
name: CertificatesPerDomainPerAccount,
584584
Burst: 3,
585585
Period: config.Duration{Duration: time.Hour},
@@ -596,7 +596,7 @@ func TestRateLimitError(t *testing.T) {
596596
allowed: false,
597597
retryIn: 30 * time.Second,
598598
transaction: Transaction{
599-
limit: limit{
599+
limit: &limit{
600600
name: 9999999,
601601
},
602602
},

ratelimits/transaction.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func newFQDNSetBucketKey(name Name, orderNames []string) (string, error) { //nol
107107
// it would fail validateTransaction (for instance because cost and burst are zero).
108108
type Transaction struct {
109109
bucketKey string
110-
limit limit
110+
limit *limit
111111
cost int64
112112
check bool
113113
spend bool
@@ -143,7 +143,7 @@ func validateTransaction(txn Transaction) (Transaction, error) {
143143
return txn, nil
144144
}
145145

146-
func newTransaction(limit limit, bucketKey string, cost int64) (Transaction, error) {
146+
func newTransaction(limit *limit, bucketKey string, cost int64) (Transaction, error) {
147147
return validateTransaction(Transaction{
148148
bucketKey: bucketKey,
149149
limit: limit,
@@ -153,7 +153,7 @@ func newTransaction(limit limit, bucketKey string, cost int64) (Transaction, err
153153
})
154154
}
155155

156-
func newCheckOnlyTransaction(limit limit, bucketKey string, cost int64) (Transaction, error) {
156+
func newCheckOnlyTransaction(limit *limit, bucketKey string, cost int64) (Transaction, error) {
157157
return validateTransaction(Transaction{
158158
bucketKey: bucketKey,
159159
limit: limit,
@@ -162,7 +162,7 @@ func newCheckOnlyTransaction(limit limit, bucketKey string, cost int64) (Transac
162162
})
163163
}
164164

165-
func newSpendOnlyTransaction(limit limit, bucketKey string, cost int64) (Transaction, error) {
165+
func newSpendOnlyTransaction(limit *limit, bucketKey string, cost int64) (Transaction, error) {
166166
return validateTransaction(Transaction{
167167
bucketKey: bucketKey,
168168
limit: limit,

0 commit comments

Comments
 (0)