Breaking: refactor usage to delay type specification#21
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors the hoglet circuit breaker library to decouple the Circuit from specific function types, enabling reuse of a single Circuit instance across multiple functions. The key architectural change replaces the generic Circuit[IN, OUT] type with a non-generic Circuit struct, and introduces a new Wrap function to adapt user functions to the circuit breaker pattern. This improves API flexibility and reduces type parameter complexity.
Key Changes
- Replaced generic
Circuit[IN, OUT]with non-genericCircuitstruct, moving function wrapping logic fromNewCircuitto a newWrapfunction - Updated Go version to 1.24.0 and bumped
golang.org/x/syncdependency to v0.18.0 - Updated all tests and examples to use the new API pattern with
Wrap(circuit, func)instead ofcircuit.Call(...)
Reviewed changes
Copilot reviewed 5 out of 7 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| hoglet.go | Core API refactor: removed generics from Circuit type, introduced Wrap function, renamed WrappedFunc to WrappableFunc |
| hoglet_test.go | Updated tests and benchmarks to use new Wrap API, improved benchmark setup by caching wrapped functions, removed obsolete tt := tt pattern |
| options_test.go | Changed package to hoglet_test for better test isolation, updated tests to use new Wrap API |
| example_test.go | Updated all examples to demonstrate new Wrap API pattern |
| go.mod | Updated Go version to 1.24.0 and golang.org/x/sync to v0.18.0, removed unused protobuf dependency |
| go.sum | Updated checksums for dependency changes |
| .gitignore | Added *.profile pattern for profiling artifacts |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
0b4fb83 to
f99c9ae
Compare
1d6f33f to
48214fb
Compare
This makes the circuit itself type-agnostic, allowing reuse of circuits for multiple functions with different types. BREAKING CHANGE: circuit initialization now does not require the function and does not provide the Call() method anymore, instead relying on the package-level Wrap().
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
48214fb to
929e49a
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request introduces a significant refactor to the
hogletcircuit breaker library, focusing on simplifying the API, improving type safety, and modernizing the codebase. The most notable change is the removal of the genericCircuittype in favor of a non-genericCircuitstruct, with a newWrapfunction to adapt user functions to the circuit breaker. The tests and examples have been updated to use the new API. Additionally, dependencies and the Go version have been updated.❌ Old
🟢 New
This allows reusing the same circuit for multiple different functions.
Details:
API Refactor and Simplification:
Replaced the generic
Circuit[IN, OUT]type with a non-genericCircuitstruct, and removed the function parameter fromNewCircuit. Instead, user functions are now wrapped using a new genericWrapfunction, which applies the circuit breaker to any compatible function. (hoglet.go,example_test.go,hoglet_test.go,options_test.go) [1] [2] [3] [4] [5] [6] [7] [8]Updated all usages in tests and examples to use
Wrap(circuit, func)instead of the previouscircuit.Call(...)method, ensuring consistency and demonstrating the new API. (example_test.go,hoglet_test.go,options_test.go) [1] [2] [3] [4] [5] [6] [7]Type and Naming Improvements:
WrappableFuncas the new type for functions that can be wrapped by aCircuit, replacing the oldWrappedFunctype. (hoglet.go)Dependency and Go Version Updates:
golang.org/x/syncto v0.18.0 ingo.mod. Also removed an unused indirect dependency. (go.mod)Testing Improvements:
options_test.gotohoglet_testfor improved test isolation and updated imports accordingly. (options_test.go)The change also brings some improvements to micro-benchmarks: