Skip to content

Conversation

@cnlangzi
Copy link
Owner

@cnlangzi cnlangzi commented Jan 11, 2026

Summary by Sourcery

Change limiter construction to return errors from New instead of panicking and update callers accordingly.

Enhancements:

  • Make New return (*Limiter, error) so initialization failures are surfaced to callers instead of causing a panic.

Documentation:

  • Document the new error-returning New constructor in README examples and simplify Allow usage to a boolean check.

Tests:

  • Update tests and benchmarks to use the new New signature and fail explicitly when construction returns an error.

Chores:

  • Adjust the example program to handle construction errors from New and ensure the limiter is always closed.

knownbots.New() errors are now ignored with fallback to nil validator
instead of panicking, allowing the application to start gracefully
- New() signature changed from New() *Limiter to New() (*Limiter, error)
- knownbots.New() errors are properly returned to caller
- Updated all callers to handle error return value
- Updated README.md examples with error handling

BREAKING CHANGE: New() now returns (limiter, error), callers must check error
@sourcery-ai
Copy link

sourcery-ai bot commented Jan 11, 2026

Reviewer's Guide

Refactors Limiter construction so New returns (*Limiter, error) instead of panicking on knownbots initialization failure, and updates tests, examples, and documentation to handle the new error-aware API and slightly simplify Allow usage in the README.

Sequence diagram for Limiter creation with error-aware New

sequenceDiagram
    actor App
    participant Botrate as Botrate_New
    participant Knownbots as Knownbots_New

    App->>Botrate: New(opts...)
    activate Botrate
    Botrate->>Knownbots: New()

    alt Knownbots initialization succeeds
        Knownbots-->>Botrate: validator, nil error
        Botrate-->>App: limiter, nil error
    else Knownbots initialization fails
        Knownbots-->>Botrate: nil, error
        Botrate-->>App: nil, error
    end
    deactivate Botrate
Loading

Class diagram for updated Limiter construction via New

classDiagram
    class Config

    class Limiter {
        -cfg Config
    }

    class Botrate {
        +New(opts Option) *Limiter, error
    }

    class Knownbots {
        +New() Validator, error
    }

    Botrate ..> Limiter
    Botrate ..> Knownbots
    Limiter ..> Knownbots
Loading

File-Level Changes

Change Details Files
Make Limiter constructor error-aware and propagate knownbots initialization failures to callers.
  • Change New to return (*Limiter, error) instead of *Limiter.
  • Replace panic on knownbots.New failure with returning nil and the error.
  • Return (l, nil) on successful limiter creation.
  • Update all tests and benchmarks to capture the error from New, fail early on non-nil errors, and reuse the err variable where appropriate.
  • Update README and example code to use the new New signature, including error checks around limiter and knownbots construction, and to slightly simplify the example Allow handler to return a generic 429 response.
limiter.go
botrate_test.go
README.md
example/main.go

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@github-actions
Copy link

Benchmark Results


Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue, and left some high level feedback:

  • The tests now have a lot of repeated l, err := New(...) / if err != nil { t.Fatalf(...) } boilerplate; consider introducing a small test helper like newLimiterTB(tb testing.TB, opts ...Option) *Limiter to centralize error handling and keep individual tests focused on behavior.
  • The README example handler changed from using result.Reason to a generic 429 Too Many Requests for all failures; if this alters the intended public behavior (distinguishing fake bots vs rate limiting), consider either preserving the richer responses or adding a separate example to demonstrate the more detailed handling.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The tests now have a lot of repeated `l, err := New(...)` / `if err != nil { t.Fatalf(...) }` boilerplate; consider introducing a small test helper like `newLimiterTB(tb testing.TB, opts ...Option) *Limiter` to centralize error handling and keep individual tests focused on behavior.
- The README example handler changed from using `result.Reason` to a generic `429 Too Many Requests` for all failures; if this alters the intended public behavior (distinguishing fake bots vs rate limiting), consider either preserving the richer responses or adding a separate example to demonstrate the more detailed handling.

## Individual Comments

### Comment 1
<location> `botrate_test.go:13-15` </location>
<code_context>

 func TestLimiter_New(t *testing.T) {
-	l := New()
+	l, err := New()
+
+	if err != nil {
+		t.Fatalf("New() returned error: %v", err)
+	}
</code_context>

<issue_to_address>
**issue (testing):** Add a test that covers the failure case where New() returns a non-nil error

Current tests only cover the success path of `New()` and verify that it returns no error. To validate the new behavior when `New()` fails (e.g., when `knownbots.New()` returns an error), add a test that forces this failure and asserts `New()` returns `nil, err` instead of panicking. You may need light dependency injection or a test-only hook around the knownbots constructor to simulate the failure.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@codecov
Copy link

codecov bot commented Jan 11, 2026

Welcome to Codecov 🎉

Once you merge this PR into your default branch, you're all set! Codecov will compare coverage reports and display results in all future pull requests.

Thanks for integrating Codecov - We've got you covered ☂️

@cnlangzi cnlangzi merged commit 958646e into main Jan 11, 2026
5 checks passed
@cnlangzi cnlangzi deleted the fix/new branch January 11, 2026 07:17
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

Successfully merging this pull request may close these issues.

2 participants