-
Notifications
You must be signed in to change notification settings - Fork 715
Closed
Description
Is your feature request related to a problem? Please describe.
Assume there are retries for a rpc call to a remote service, and the retry scheme is retry twice for a call. Assume there are two instances (instance A and instance B) for this service. We want the retry is made twice and each time to a different instance.
But there is a problem while using the RoundRobinLoadBalancer
. The position is a Atomic field shared across threads and there would be a case that the retry is made to the same instance to the last time:
- Thread 1: position = 0, call A;
- Thread 2: position = 1, call B;
- Thread 1: call A failed, position = 2, still retry A;
Describe the solution you'd like
Make the position a thread local field and each initialize with random value:
ThreadLocal<Long> position = ThreadLocal.withInitial(() -> ThreadLocalRandom.current().nextInt(1000));