Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ratelimits: combine validation and precomputation into a constructor #7880

Open
jsha opened this issue Dec 10, 2024 · 0 comments
Open

ratelimits: combine validation and precomputation into a constructor #7880

jsha opened this issue Dec 10, 2024 · 0 comments

Comments

@jsha
Copy link
Contributor

jsha commented Dec 10, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant