Aias00 opened a new pull request, #3516:
URL: https://github.com/apache/dubbo-go/pull/3516
## What
Two panic paths in the consistent-hash load balancer that are distinct from
the map race fixed in #3317 and the `toKey` panics fixed in #3430 / #3432.
## Why
1. **Nil selector cached.** `newSelector` returned `nil` when
`hash.arguments` contained a non-numeric index (`strconv.Atoi` failure). That
`nil` was stored in `selectors[key]`; the next `Select` for the same key
nil-dereferenced it (`selector.hashCode` at `loadbalance.go:81`, then
`selector.Select`).
```go
// selector.go (before)
i, err := strconv.Atoi(index)
if err != nil {
return nil // nil cached in selectors[key]
}
```
2. **Empty ring.** With `hash.nodes < 4`, `replicaNum/4 == 0`, so the ring
(`selector.keys`) was never populated. `selectForKey` then did `c.keys[idx]` on
an empty slice → index-out-of-range panic.
```go
// selector.go selectForKey (before)
if idx == len(c.keys) { idx = 0 }
return c.virtualInvokers[c.keys[idx]] // c.keys empty → panic
```
## Fix
- `newSelector` returns `(*selector, error)`; a bad `hash.arguments` index
surfaces as an error instead of `nil`.
- `Select` no longer caches a nil selector; on `newSelector` error it warns
and degrades to `invokers[0]`. It also nil-guards any cached selector (so a
stale nil from a prior build can't crash a later call).
- `newSelector` clamps `replicaNum` to `>= 4` so the ring is never empty.
- `selectForKey` guards `len(c.keys)==0` defensively.
## Tests
- `TestSelectDegradesOnBadHashArguments`: non-numeric `hash.arguments` no
longer panics, degrades to `invokers[0]`, and does not cache a nil selector.
- `TestSelectHandlesSmallHashNodes`: `hash.nodes=2` no longer panics.
- Updated the two existing `newSelector` call sites for the new signature.
Full `consistenthashing` package passes under `-race`.
Fixes #3510
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]