fix(load-balancing): avoid panic when all weighted backends have zero weight#891
Open
elysia090 wants to merge 1 commit into
Open
fix(load-balancing): avoid panic when all weighted backends have zero weight#891elysia090 wants to merge 1 commit into
elysia090 wants to merge 1 commit into
Conversation
2812244 to
636b087
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.
Summary
This fixes a panic in the weighted backend selector when the backend set is non-empty but every backend has weight
0.No issue is filed for this small, self-contained bug fix.
Weighted::build()builds the weighted index list by pushing each backend indexweighttimes. If all backend weights are zero,self.backend.backendsis non-empty butself.backend.weightedis empty.The first call to
WeightedIterator::next()only checks whether the backend list is empty, then usesself.backend.weighted.len()as the modulo divisor. When that length is zero, this panics.Fix
Return
Nonefrom the initial weighted selection path when the weighted index list is empty.This keeps the change limited to the all-zero-weight panic case. It does not change fallback behavior for mixed
weight = 0/weight > 0backend sets, and it does not rejectweight = 0inBackend::new_with_weight().Test
Added a regression test covering two backends created with
Backend::new_with_weight(..., 0).Before this change, the test panics with:
After this change, selection returns
None, and repeated calls on the same iterator continue returningNone.cargo test -p pingora-load-balancing weighted_all_zero_weight_returns_none_instead_of_panickingResult: