/**
 *
 */

package io.galeb.router.client.hostselectors;

import io.galeb.router.client.ExtendedLoadBalancingProxyClient.Host;
import io.undertow.server.HttpServerExchange;

import java.util.concurrent.atomic.AtomicInteger;

public class RoundRobinHostSelector extends ClientStatisticsMarker implements HostSelector {

    private volatile int currentHost = 0;

    @Override
    public int selectHost(final Host[] availableHosts, final HttpServerExchange exchange) {
        int pos = currentHost;
        currentHost = ++currentHost % availableHosts.length;
        stamp(availableHosts[pos], exchange);
        return pos;
    }

    // Test only
    public synchronized void reset() {
        currentHost = 0;
    }
}

@@ -329,36 +329,62 @@
 ate 
-volatile int currentHost = 0
+final AtomicInteger currentHost = new AtomicInteger(0)
 ;%0A%0A 
@@ -496,16 +496,22 @@
         
+final 
 int pos 
@@ -527,45 +527,26 @@
 Host
-;%0A        currentHost = ++currentHost
+.getAndIncrement()
  %25 a
@@ -719,12 +719,15 @@
 Host
- = 0
+.set(0)
 ;%0A  

Fix unusual sync error