Skip to content

Conversation

@dmouraneto
Copy link
Contributor

@dmouraneto dmouraneto commented Mar 29, 2025

Add support for time-based rolling window.
Add BucketPeriod to the struct Settings, as specified in https://github.com/sony/gobreaker/wiki/Draft-Specification-for-Time%E2%80%90Based-Rolling-Window

It uses a double linked list, from the standard package container/list, to store the BucketCounts.

The counts is encapsulated in

type WindowCounts struct {
	Counts
	mutex            sync.Mutex
	numBuckets       int64
	bucketCounts     *list.List
	bucketGeneration int
}

#64

@dmouraneto
Copy link
Contributor Author

@YoshiyukiMineo did you have the chance to review this PR?

@YoshiyukiMineo YoshiyukiMineo changed the base branch from master to rolling-window June 4, 2025 07:12
@YoshiyukiMineo
Copy link
Member

Thank you. I will merge this PR to the master branch after review, trial, and some modification if needed.

@YoshiyukiMineo YoshiyukiMineo merged commit ea61827 into sony:rolling-window Jul 5, 2025
4 checks passed
YoshiyukiMineo added a commit that referenced this pull request Aug 16, 2025
* Time-based Rolling window (#90)

* feat: add rolling window

* fix: new bucket expiry time

* test: add tests for rolling window circuit breaker

* chore: remove comments

---------

Co-authored-by: Yoshiyuki Mineo <Yoshiyuki.Mineo@jp.sony.com>

* Update distributed_gobreaker.go

* Refactor circuit breaker implementation to replace WindowCounts with a new structure, improving clarity and consistency in handling counts. Update related tests to reflect this change.

* Remove mutex locks from windowCounts methods to simplify the implementation and improve performance. This change enhances the clarity of the code while maintaining functionality.

* Refactor DistributedCircuitBreaker to replace BucketCounts with Buckets in SharedState and related structures. Update methods and tests accordingly to ensure consistency and correctness in handling counts.

* Update README and gobreaker.go for consistency in language. Changed "doesn't" to "does not" for clarity in CircuitBreaker documentation.

* Fix nil pointer dereference in windowCounts.rotate method by adding a check for oldBucket. This ensures safe access to oldBucket's values before performing calculations on counts.

* Refactor windowCounts initialization and bucket management in gobreaker. Update tests to reflect changes in bucket handling and ensure correct state assertions.

* Refactor toCountsArray function in distributed_gobreaker.go to use a slice instead of a list for improved performance. Update related tests in gobreaker_test.go to reflect changes in bucket handling and ensure accurate assertions.

* Implement clear method for Counts struct and update state reset logic in distributed_gobreaker_test.go. This enhances clarity and ensures proper state management during circuit breaker operations.

* Implement bucketAt method in windowCounts for circular buffer indexing and update related tests in gobreaker_test.go to ensure accurate assertions on bucket retrieval. This enhances the clarity and functionality of the rolling window circuit breaker.

* Refactor windowCounts to use age for bucket management instead of current index. Update related methods and tests in gobreaker_test.go to ensure accurate bucket access and state management in the rolling window circuit breaker.

* Enhance SharedState in DistributedCircuitBreaker by adding Age field and updating bucket management. Refactor inject and extract methods to utilize new age property and improve bucket copying logic. This change ensures better state management and clarity in the circuit breaker implementation.

* Refactor Counts struct methods in gobreaker to improve state management during requests, successes, and failures. Update windowCounts to utilize these methods for better clarity and performance in bucket handling. Adjust initialization logic for bucket management in CircuitBreaker to ensure accurate configuration based on interval and bucket period.

* Refactor gobreaker to replace windowCounts with rollingCounts for improved bucket management. Update related methods and tests to ensure accurate state handling and assertions in the CircuitBreaker implementation. This change enhances clarity and performance in managing circuit breaker states.

* Refactor bucketAt method in rollingCounts to improve index handling and prevent out-of-bounds errors. This change enhances the robustness of bucket access by ensuring proper index calculations and returning an empty Counts struct when necessary.

* Rename rotate method to roll in rollingCounts for improved clarity and update related references in gobreaker. This change enhances code readability and maintains consistent terminology across the implementation.

* Refactor CircuitBreaker methods to incorporate age parameter for improved state management during requests, successes, and failures. Update related methods to ensure accurate handling of bucket access and enhance clarity in the CircuitBreaker implementation.

* Update CircuitBreaker and rollingCounts to improve state management and bucket handling. Adjust tests to reflect changes in interval handling and expiry checks, ensuring accurate assertions in the CircuitBreaker implementation.

* Refactor age calculation in CircuitBreaker to handle negative elapsed time and improve clarity in bucket growth logic. This change enhances state management and ensures accurate handling of time intervals in the rollingCounts implementation.

* Refactor age calculation in CircuitBreaker by extracting it into a separate method for improved clarity and reusability. This change simplifies the bucket growth logic and enhances state management during circuit operations.

* Refactor age calculation in CircuitBreaker to use an intermediate variable for improved clarity and maintainability. This change ensures accurate handling of elapsed time and enhances the overall readability of the age method.

* Enhance rollingCounts logic in gobreaker to ensure accurate state management by preventing negative values for consecutive successes, failures, requests, and total counts. This refactor improves clarity and robustness in the roll method, ensuring proper handling of bucket data during state transitions.

* Add BucketPeriod to Settings and update documentation for rolling window strategy

This commit introduces a new `BucketPeriod` field in the `Settings` struct, defining the duration for each bucket in the rolling window strategy. The documentation has been updated to clarify how `BucketPeriod` interacts with `Interval`, ensuring accurate handling of internal counts and bucket updates in the CircuitBreaker implementation.

---------

Co-authored-by: Daniel Moura <dmouraneto@gmail.com>
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