Skip to content

feat: custom priority validator#3

Merged
basgys merged 1 commit intomainfrom
feat/custom-priority-validator
Mar 15, 2026
Merged

feat: custom priority validator#3
basgys merged 1 commit intomainfrom
feat/custom-priority-validator

Conversation

@basgys
Copy link
Copy Markdown
Contributor

@basgys basgys commented Dec 19, 2024

This PR allows configuration of how Bulwark handles out-of-range priority values. Three built-in options cover the common use cases:

  • ClampInvalidPriority (default) — silently clamps the priority to the lowest valid level and emits a warning log. Safe for production: a misconfigured or user-supplied priority value won't crash the
    process.
  • RejectInvalidPriority — returns an error, letting the caller decide how to handle it. Suited for APIs where the caller owns priority validation.
  • AssertValidPriority — panics with a descriptive message. Useful in tests or staging to catch programming errors early.

The previous behaviour was an implicit runtime panic (index out of bounds) whenever an invalid priority was passed to Throttle or WithAdaptiveThrottle. Since priority values can come from configuration or user input, crashing the process is a disproportionate response — hence the safer clamp default.

The custom validator can be set via WithPriorityValidator(fn).


Note

Medium Risk
Changes how invalid priorities are handled across all throttle entrypoints (clamp/log vs error/panic), which can affect runtime behavior and error paths for callers.

Overview
Adaptive throttling now validates request priority values before indexing counters, avoiding the previous out-of-bounds panic behavior.

NewAdaptiveThrottle stores the configured priority count, defaults to a safe ClampInvalidPriority validator, and rejects priorities <= 0. Throttle, generic Throttle[T], and WithAdaptiveThrottle[T] now run the validator and may return an error before invoking the throttled function.

Adds WithPriorityValidator plus built-in validators (AssertValidPriority, ClampInvalidPriority with warning log, and RejectInvalidPriority) with accompanying tests.

Written by Cursor Bugbot for commit 2b38b56. This will update automatically on new commits. Configure here.

Comment thread adaptive.go Outdated
Comment thread adaptive.go
@basgys basgys force-pushed the feat/custom-priority-validator branch from 1f7a48f to 5de460b Compare March 15, 2026 13:46
@basgys basgys marked this pull request as ready for review March 15, 2026 13:48
Comment thread adaptive.go Outdated
@basgys basgys force-pushed the feat/custom-priority-validator branch 3 times, most recently from 00ed6ee to bedabb3 Compare March 15, 2026 14:07
Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Comment thread validator.go Outdated
…e how out-of-range priorities are handled. Three built-in validators are provided: OnInvalidPriorityClamp (default), OnInvalidPriorityError, and OnInvalidPriorityPanic.

The default behaviour changes from a runtime index-out-of-bounds panic to clamping the priority to the lowest valid level with a warning log, which is more appropriate for a library used in production request paths.
@basgys basgys force-pushed the feat/custom-priority-validator branch from bedabb3 to 2b38b56 Compare March 15, 2026 14:15
@basgys basgys merged commit 2aca531 into main Mar 15, 2026
2 checks passed
@basgys basgys deleted the feat/custom-priority-validator branch March 15, 2026 14:23
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