boulder
boulder copied to clipboard
ratelimits: combine validation and precomputation into a constructor
Reviewing https://github.com/letsencrypt/boulder/pull/7869/files/b7189f5c31798523fe61d1be0a40363cb9e9fa30..81b616be22710d38b2193f200f63a9c2bed86637#diff-ba4749ac861bae77ee5067820eff8d269f6ede6202108c0238a1809726d066d3R224-R236, I notice that we now have a pattern of partially initializing a struct, then validating its contents, then precomputing some internal fields:
lim := &limit{
burst: v.Burst,
count: v.Count,
period: v.Period,
name: name,
}
err := validateLimit(lim)
if err != nil {
return nil, fmt.Errorf("parsing default limit %q: %w", k, err)
}
lim.precompute()
This is a great use case for a constructor that returns an error:
func newLimit(name string, config LimitConfig) (*limit, error) {
That way we can reduce the chance of winding up with a partially-initialized limit object, and also reduce boilerplate a little bit.