pjfanning opened a new pull request, #819:
URL: https://github.com/apache/pekko-grpc/pull/819
Changes Made
File:
runtime/src/main/scala/org/apache/pekko/grpc/internal/PekkoHttpClientUtils.scala
Fix 1: Race condition → AtomicInteger
Before:
```
var roundRobin: Int = 0
// ...inside closure:
roundRobin += 1
val target = resolved.addresses(roundRobin % resolved.addresses.size)
```
After:
```
val roundRobin = new AtomicInteger(0)
// ...inside closure:
val idx = roundRobin.getAndIncrement()
val target = resolved.addresses(math.abs(idx % resolved.addresses.size))
```
getAndIncrement() is atomic — two concurrent threads will always get
different indices. The math.abs handles integer overflow wraparound (when idx
wraps past Int.MaxValue to negative).
Fix 2: Division by zero guard
Before:
```
val target = resolved.addresses(roundRobin % resolved.addresses.size)
// ArithmeticException if addresses is empty
```
After:
```
if (resolved.addresses.isEmpty)
throw new IllegalStateException(
s"Service discovery for '${settings.serviceName}' returned no addresses")
val idx = roundRobin.getAndIncrement()
val target = resolved.addresses(math.abs(idx % resolved.addresses.size))
```
Fails with a clear, descriptive error instead of an opaque
ArithmeticException.
Fix 3 (bonus): Hardcoded timeout → configured timeout
Before:
settings.serviceDiscovery.lookup(settings.serviceName, 10.seconds)
After:
settings.serviceDiscovery.lookup(settings.serviceName,
settings.resolveTimeout)
Uses the user-configured resolveTimeout from reference.conf (default 1s)
instead of ignoring it with a hardcoded 10s.
--
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]