letsencrypt / letsencrypt/boulder

ratelimits: combine validation and precomputation into a constructor

Open
#7,880 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Go
Stars
5.8k
Forks
649
Avg merge
3d 23h
Merged PRs (30d)
24

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the linked pull request diff and locating limit, validateLimit, precompute, and LimitConfig in the repository. Trace the current initialization call site, then make the constructor own validation and precomputation; done means callers use it without partially initializing limit values.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
backend
Issue type
Refactor
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.