Skip to content

feat: introduce v2 module with clean API#9

Merged
basgys merged 7 commits intomainfrom
feat/v2
Mar 16, 2026
Merged

feat: introduce v2 module with clean API#9
basgys merged 7 commits intomainfrom
feat/v2

Conversation

@basgys
Copy link
Copy Markdown
Contributor

@basgys basgys commented Mar 15, 2026

What changed and why

v1 accumulated several mutable package-level globals (IsRejectedError, ClientSideRejectionError, Now) that made it impossible to configure two throttle instances differently and caused subtle races in tests. This PR introduces github.com/deixis/bulwark/v2 as a clean break: all configuration lives on the instance, there are no globals left to mutate, and the API surface is trimmed to remove the pieces that didn't pull their weight.

Breaking changes from v1

v1 v2
bulwark.IsRejectedError = func(...) {...} WithRejectedErrorFunc(fn) option on each instance
bulwark.ClientSideRejectionError (var) bulwark.ErrClientSideRejection (sentinel, compare with errors.Is)
bulwark.Now = func() time.Time {...} WithNow(fn) option on each instance
OnInvalidPriorityClamp / OnInvalidPriorityError / OnInvalidPriorityPanic (vars) ClampInvalidPriority / RejectInvalidPriority / AssertValidPriority (plain functions)
WithAdaptiveThrottle[T](...) Throttle[T](ctx, at, priority, fn) — single generic entry point
WithAcceptedErrors(fn) Removed — superseded by WithRejectedErrorFunc

Everything else (WithRejectedErrorFunc, WithClientSideRejectionError, WithNow, WithRandomSource, WithPriorityValidator, priority constants, RejectedError, fallback signatures) carries over from v1 unchanged.

Files

  • v2/adaptive.go — core throttle, defaults set at construction time, no global fallbacks
  • v2/validator.go — validators as plain functions instead of mutable vars
  • v2/priority.go, v2/context.go, v2/counter.go — carried over from v1, unchanged
  • v2/adaptive_test.go, v2/context_test.go — full test suite targeting the v2 import path
  • README.md — updated to v2 import path, errors.Is checks, and new classifier API

Note

Medium Risk
Introduces a new v2 Go module with a largely new public API surface and behavior around error classification/configuration, plus a dependency upgrade; while v1 remains, consumers migrating to v2 will need careful review and testing.

Overview
Adds a new github.com/deixis/bulwark/v2 module (Go 1.26) that removes mutable package-level configuration by making rejection classification, client-side rejection error, time source, RNG, and priority validation per-throttle options, and provides a single generic Throttle[T] entry point alongside AdaptiveThrottle.Throttle.

Updates docs to the v2 import path and new APIs (errors.Is(err, bulwark.ErrClientSideRejection), WithRejectedErrorFunc, DefaultRejectedErrorFunc), adds a comprehensive v2 test suite, upgrades github.com/deixis/faults to v1.0.1, and updates GitHub Actions to run tests for both modules with different Go versions.

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

basgys added 2 commits March 15, 2026 17:08
Remove all deprecated globals, replace mutable validator vars with plain
functions, drop WithAdaptiveThrottle[T] in favour of the generic Throttle[T],
and set per-instance defaults at construction time so v2 callers never rely
on package-level mutable state.
Comment thread v2/adaptive.go Outdated
Comment thread v2/adaptive.go
basgys added 4 commits March 16, 2026 01:40
Direct type assertion after errors.Is panics when errRejected is nested
inside a wrapping error. Replace with errors.As, which safely traverses
the chain. As a side effect, AdaptiveThrottle.Throttle now delegates to
Throttle[T] so the classification logic lives in one place.
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 v2/adaptive.go
@basgys basgys merged commit 46c7d1f into main Mar 16, 2026
3 checks passed
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.

1 participant