in mantis-network/src/main/java/io/reactivex/mantis/network/push/RoundRobinRouter.java [40:71]
public void route(Set<AsyncConnection<T>> connections, List<T> chunks) {
if (chunks != null && !chunks.isEmpty()) {
numEventsProcessed.increment(chunks.size());
}
List<AsyncConnection<T>> randomOrder = new ArrayList<>(connections);
Collections.shuffle(randomOrder);
if (chunks != null && !chunks.isEmpty() && !randomOrder.isEmpty()) {
Iterator<AsyncConnection<T>> iter = loopingIterator(randomOrder);
Map<AsyncConnection<T>, List<byte[]>> writes = new HashMap<>();
// process chunks
for (T chunk : chunks) {
AsyncConnection<T> connection = iter.next();
Func1<T, Boolean> predicate = connection.getPredicate();
if (predicate == null || predicate.call(chunk)) {
List<byte[]> buffer = writes.get(connection);
if (buffer == null) {
buffer = new LinkedList<>();
writes.put(connection, buffer);
}
buffer.add(encoder.call(chunk));
}
}
if (!writes.isEmpty()) {
for (Entry<AsyncConnection<T>, List<byte[]>> entry : writes.entrySet()) {
AsyncConnection<T> connection = entry.getKey();
List<byte[]> toWrite = entry.getValue();
connection.write(toWrite);
numEventsRouted.increment(toWrite.size());
}
}
}
}